how do I convert a cookie into a variable in php? - php

Let's say the user logs in, a cookie is sent and the user is redirected to another page.
How do I make it where I can convert the cookie value into a variable in PHP (on the page they are redirected to)?
I want to write the cookie value to a .txt file.
Thanks.

From the docs:
Any cookies sent to you from the client will automatically be included into a $_COOKIE auto-global array if variables_order contains "C".

Well cookies live in the $_COOKIE global. (When received.)
And writing them to a file would be as boring as:
file_put_contents("var/cookie.txt", $_COOKIE["cookiename"]);

You don't need to convert them just retrieve into a php variable.
<?php
if(isset($_COOKIE['lastVisit']))
$visit = $_COOKIE['lastVisit'];
else
echo "You've got some stale cookies!";
echo "Your last visit was - ". $visit;
?>
see here tutorial

Cookies stored in the $_COOKIE array. So if you want to use it, just assign an appropriate cookie key value to your variable, for example:
$name = $_COOKIE['name'];

Cookies can be accessed in the global $_COOKIE array.

Use this:
$newvar = $_COOKIE['cookiename'];

Cookie is available in $_COOKIE as $_COOKIE['key']; get that value and store it in a variable, which can be output to a file.

Related

Why session doesn't recognise manual change of cookie?

I am trying to fully understand sessions, and I conducted this test:
my code is:
test.php
<?
#session_start();
if(isset($_SESSION['test'])) {
echo "test success";
}
?>
When I enter my cookie manually into my browser using an addon as:
PHPSESSID test
It does not recognise it.
$_SESSION is a "superglobal" (available everywhere) array that is tied with a cookie using a unique session id.
If you're wanting to reference cookie values you've set you'll need to use the $_COOKIE superglobal array.
You can read more about superglobals here: http://php.net/manual/en/language.variables.superglobals.php
And how $_SESSION and $_COOKIE works here:
http://php.net/manual/en/reserved.variables.cookies.php
http://php.net/manual/en/reserved.variables.session.php
You cannot set values in the SESSION by using the browser like that. PHP is the only place you'll be able to set the 'test' key to a value, like true or false.
session_start();
// You could assign this based on the value of a cookie
$_SESSION['test'] = true;
if ($_SESSION['test']) {
// this is a test session
}
Hope that helps.
To see the result of your cookie change, do:
<?
#session_start();
if(session_id() == 'test') {
echo "test success";
}
The cookie contains the session ID, individual session variables are stored on the server, using this ID and the variable name as keys (the default configuration uses a file named after the session ID).

Telling a php session's name?

I installed a pre-built forum on my website and I want (in a diffrent page) to check if the forum's session is active.
Something like :
if (isset($_SESSION['forum'])) { echo "Session is active!"; }
Problem is - I don't know the sessions name...
Tried downloading some chrome add-ons for session managing but I can't get the name of the session.
Whats the right way of doing this?
Thanks ahead!
You can see the dump of $_SESSION variable
var_dump($_SESSION);
session_name() will give you the session name, that usually is defined in php.ini. By default it is always: PHPSESSID. This name is used as cookie name or as POST/GET variable name.
session_id() will give you the identifier for the current session. It will be the contents of the cookie or POST/GET variable.
Then you have $_SESSION that will contain all your session data. use print_r() to see what you have stored in it so far.
To know if session vars are set you can also just do if(isset($_SESSION)&&count($_SESSION))
try
print_r ($_SESSION);
taht way you'll see all sessions
<?php
session_start();
print_r($_SESSION);
?>
Use this to see which session variables are currently set.
You need to check that the session is currently active, and then that the forum key is defined
if ( ! ($sid = session_id()) {
session_start(); // open session if not yet opened
$sid = session_id(); // get sid as session ID
}
// $sid contains the session ID (in cookie)
if (isset($_SESSION['forum'])) {
// forum is defined
}
See also the answer from this page

How to pass and GET session through URL?

I've one variable called numbers which I'm use within a session to identify a user. The link should be like index.php?number=1234567890, where number is the session name to be set and 123456789 is a user id. Can you explain how to do it "Pass through URL and SET as a var"?
index.php?number=1234567890
GET[???]
You can get it from $_GET as follows in your index.php
$number=$_GET['number'];
The variable is accessible in your code as $_GET['number']
To access any GET variables, use $_GET['varname']
Rather than pass a GET variable everytime you redirect, consider using a cookie (setcookie function and $_COOKIE variable) or session variables (start_session function and $_SESSION variable)

PHP Session Not Restoring from Cookies

When a user returns to my website, it attempts to restore their last session from the $_COOKIE associative array. It's not working as expected. I can look in my browser's cookie manager and see that the cookies are there, but they don't seem to be getting saved to the $_SESSION associative array.
This is essentially the program flow when a user returns to my site:
foreach ( $_COOKIE as $name => $val )
{
$_SESSION[$name] = $val;
}
session_start();
...
$some_var = $_SESSION[$var_name];
Do I have things out of order, or should I not be overwriting PHPSESSID? Any insight as to what I'm doing wrong would be appreciated. Thanks.
You're getting sessions and cookies mixed up. You don't need to put things into the $_COOKIE array. Just use session_start() and then put things into $_SESSION. PHP will automatically then manage the session/cookie for you.
$_COOKIE variables are stored on the users browser, so they aren't secure and can be manipulated by the user => security risk.
$_SESSION variables are stored only on the server. The only thing stored in the cookie is a session_id, so $_SESSION variable can't be manipulated.
Does that make sense?
Put session_start() before anything else; this function initializes the session data that you will be accessing in $_SESSION.
Not exactly sure what you're trying to achieve with the rest of it all, but session_start() first is a starting point...

PHP: Over-writing session variables

Question related to PHP memory-handling from someone not yet very experienced in PHP:
If I set a PHP session variable of a particular name, and then set a session variable of the exact same name elsewhere (during the same session), is the original variable over-written, or does junk accumulate in the session?
In other words, should I be destroying a previous session variable before creating a new one of the same name?
Thank you.
$_SESSION works just like any other array, so if you use the same key each time, the value is overwritten.
Tom,
It depends on how you use the session variable, but it generally means "erasing" that variable (replacing the old value by the new value, to be exact).
A session variable can store a string, a number or even an object.
<?php
# file1.php
session_start();
$_SESSION['favcolor'] = 'green';
$_SESSION['favfood'] = array('sushi', 'sashimi');
?>
After this, the $_SESSION['favcolor'] variable and the $_SESSION['favfood'] variable is stored on the server side (as a file by default). If the same user visit another page, the page can get the data out from, or write to the same storage, thus giving the user an illusion that the server "remembers" him/her.
<?php
# file2.php
session_start();
echo $_SESSION['favcolor'], '<br />';
foreach ($_SESSION['favfood'] as $value) {
echo $value, '<br />';
}
?>
Of course, you may modify the $_SESSION variable in the way you want: you may unset() any variable, append the array in the example by $_SESSION['favfood'][] = 'hamburger'; and so on. It will all be stored to the session file (a file by default, but could be a database). But please beware that the $_SESSION variable acts magically only after a call to session_start(). That means in general, if you use sessions, you have to call session_start() at the beginning of every page of your site. Otherwise, $_SESSION is just a normal variable and no magic happens :-).
Please see the PHP reference here for more information.

Categories