How to use store and use session variables across

2019-01-01 04:19发布

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.

8条回答
高级女魔头
2楼-- · 2019-01-01 04:35

All you want to do is write --- session_start(); ----- on both pages..

<!-- first page -->
<?php
  session_start(); 
  $_SESSION['myvar'] = 'hello';
?>

<!-- second page -->
<?php
    session_start();
    echo $_SESSION['myvar']; // it will print hello 

?>
查看更多
ら面具成の殇う
3楼-- · 2019-01-01 04:38

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

session_id('mySessionID'); //SET id first before calling  session start
session_start();

$name = "Nitin Hurkadli";
$_SESSION['username'] = $name;

File2.php

session_id('mySessionID'); 
session_start();

$name = $_SESSION['username'];
echo "Hello  " . $name;
查看更多
与君花间醉酒
4楼-- · 2019-01-01 04:41

Sessions Step By Step

  1. Defining session before everything, No output should be before that, NO OUTPUT

    <?php
    session_start();
    ?>
    
  2. Set your session inside a page and then you have access in that page. For example this is page 1.php

    <?php
       //This is page 1 and then we will use session that defined from this page:
        session_start();
        $_SESSION['email']='email@example.com';
    ?>
    
  3. Using and Getting session in 2.php

     <?php
    
    //In this page I am going to use session:
    
      session_start();
      if($_SESSION['email']){
      echo 'Your Email Is Here!  :) ';
      }
     ?>
    

NOTE: Comments don't have output.

查看更多
临风纵饮
5楼-- · 2019-01-01 04:43

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:

<?php
session_set_cookie_params( $lifetime, '/shared/path/to/files/' );
session_start();
$_SESSION['myvar']='myvalue';

And

<?php
session_set_cookie_params( $lifetime, '/shared/path/to/files/' );
session_start();
echo("1");
if(isset($_SESSION['myvar']))
{
    echo("2");
   if($_SESSION['myvar'] == 'myvalue')
   {
       echo("3");
       exit;
   }
}
查看更多
牵手、夕阳
6楼-- · 2019-01-01 04:46

Try this:

<!-- first page -->
<?php
  session_start(); 
  session_register('myvar');
  $_SESSION['myvar'] == 'myvalue';
?>

<!-- second page -->
<?php
    session_start();
    echo("1");
    if(session_is_registered('myvar'))
    {
        echo("2");
       if($_SESSION['myvar'] == 'myvalue')
       {
           echo("3");
           exit;
       }
    }
    ?>
查看更多
若你有天会懂
7楼-- · 2019-01-01 04:47

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.

查看更多
登录 后发表回答