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).
Related
I don't know if $_SESSION[]; creates a new session I think it does but I don't know;
If it does, should I put session_set_cookie_params(0); right before my session variable? like this
session_set_cookie_params(0);
$_SESSION['name'];
Thanks
session_start(); creates a session. $_SESSION just a global dictionary to store necessary values.
http://php.net/manual/en/function.session-start.php
session_set_cookie_params(0); gives cookies 0 seconds lifetime. It's simply cookies timeout definition. But there are more optional parameters.
http://php.net/manual/en/function.session-set-cookie-params.php
You need to put this at the start
session_start();
$_SESSION['name'] = 'Bob';
See http://php.net/manual/en/function.session-start.php
session_start(); starts / creates session
$_SESSION["sessionname"]=$value; assigns a value
echo $_SESSION["sessionname"]; - returns the value of the session
session_destroy(); -session destroy ends a session and revoves values
session_set_cookie_params(); - allows you to set other parameters for the session such as lifetime
either cookies or sessions can be used to make data available globally but session is more secure as it is stored server side while cookie is stored client side and can accessed by user. Even session uses a cookie but it only contains an id not the actual value which is stored on the server so session_set_cookie_params() is optional depending on if you want to change other parameters
I have this php code
if (isset($_SESSION['error']))
{
echo "<span id='error'><p>" . $_SESSION['error'] . "</p></span>";
unset($_SESSION['error']);
}
What Does $_SESSION['error'] means
and unset one please explain
$_SESSION contains all session variables (user ID of logged in user for example). You can set a session variable like this:
$_SESSION['foo'] = 'bar';
And access it later (after multiple page loads still, that's why it's so useful) using:
$foo = $_SESSION['foo'];
echo $foo; //Displays 'bar'
Your code checks if there is a session variable set, and if so display an error and unset the session variable. So I would guess this is to show a one-time error when something went wrong. Using a session variable means you can execute this code on a different page then where the error happened.
$_SESSION['error'] means a an array key of $_SESSION which was generated by you.
It looks in the session, which is a temporary storage that is unique to the user and more often than not is mean to last for the time a user stays on a page in a single "visit session", for a variable (actually rather an array key) called error; it then injects whatever that contains (likely an error message) into a piece of HTML, and after that, it deletes the error from the session storage with unset().
Take a look on the documentation for $_SESSION.
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
I have this query in mysql in a php page:
mysql_query("INSERT INTO tz_todo SET text='".$text."',
position = ".$position.",
user_id=".$_SESSION['user_id'].",
view_stat=0");
I tried to echo the query and the result is this:
INSERT INTO tz_todo SET text='trial text', position = 21, user_id=, view_stat=0
it seems that it can't get the session value of user_id.
And $_SESSION['user_id'] is not working in social engine. How to correct this? I also made a localhost version in my xampp and everything is fine but when I converted it into social engine, session is not working.
In any page where you are using session objects, place this code at the beginning of the file:
if(!isset($_SESSION)){session_start();}
This way if the session is not already started, it starts it; otherwise it ignores the session start if the sesion is already started.
This is important because calling session_start() if session is started already can sometimes cause errors.
That's how I get my user id through session
session_start();
$userID = $viewer->getIdentity();
$_SESSION['user_id'] = $userID;
echo $_SESSION['user_id'];
Using session to store the user_id is totally wrong. To gain a user_id try
$viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity(); (or $user->getIdentity if you have another user's object).
If you still need to use session for storing this data, use Zend-approach.
session_start();
$_SESSION["test"] = "hello world";
session_start();
echo $_SESSION["test"];
does above code work ? if not, check your session.save_path in the php.ini
NOTE: to retain this variable remember to call session_start() on each php script/page before calling for the variable from the session.
Yoy might be forget to start your session at the top of the page
<?php if(!isset($_SESSION)){ session_start(); } ?>
$_SESSION['user_id'] might not stored a value. check your login page (Basically after login session variables will set) or after register weather you assigned a value to that session variable..
setting a value to a session variable :
$_SESSION['user_id'] = "1234567";
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.