为什么php Session不适合我

时间:2017-07-08 08:52:27

标签: php session localhost

page1.php中

<?php 
  session_start();
  $_SESSION['test'] = 'abc';
  echo "the Session are set = ".$_SESSION['test'];
?>

结果:会话设置= abc

使page2.php

<?php 
  session_start();

  if(isset($_SESSION['test'])){
    echo "Session = ";
    echo $_SESSION['test'];
  }else{
    echo "Session are not set";
  }
?>

结果:未设置会话

1 个答案:

答案 0 :(得分:-1)

在page1.php

<?php 
  session_start();
  $_SESSION['test'] = 'abc';
  header("Location: page2.php");//Well we know that this session here works so lets try redirecting...
?>

在page2.php上请尝试

<?php 
  if( !session_id() )
  {
    session_start();
    session_regenerate_id(true);
  }    
  if(isset($_SESSION['test'])){
    echo "Session = ";
    echo $_SESSION['test'];
  }else{
    echo "Session are not set";
  }
?>