When one page is accessed, I would like to start a session and store a session variable:
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Then from another page, I would like to check if that session variable has been stored:
<?php
session_start();
echo("1");
if(isset($_SESSION['myvar']))
{
echo("2");
if($_SESSION['myvar'] == 'myvalue')
{
echo("3");
exit;
}
}
?>
This code does not work for me.
All you want to do is write --- session_start(); ----- on both pages..
Every time you start a session (applies to PHP version 5.2.54),
session_start()
creates a new session id.Here is the fix that worked for me.
File1.php
File2.php
Sessions Step By Step
Defining session before everything, No output should be before that, NO OUTPUT
Set your session inside a page and then you have access in that page. For example this is page 1.php
Using and Getting session in 2.php
NOTE: Comments don't have output.
In the possibility that the second page doesn't have shared access to the session cookie, you'll need to set the session cookie path using session_set_cookie_params:
And
Try this:
Reasoning from the comments to this question, it appears a lack of an adjusted session.save_path causes this misbehavior of PHP’s session handler. Just specify a directory (outside your document root directory) that exists and is both readable and writeable by PHP to fix this.