简体   繁体   中英

Sharing session instances in PHP

I want to use session in PHP. But its showing some problems in my scenario.

I want to share same session in 3 different PHP files.

./sessionTest/testing1.php
./testing2.php
./testing3.php

if i store some information in $_SESSION in testing1.php , i cant access the same information in other 2 files

what should i do to make these 3 files share the same session instance?
Is there any other(except cookie) to make this possible?

PS These 3 files are executed by different calls, cant include one file into another using include() or require() functions.

Added session_start() at the top but still doesnt share the same session.

Like so :-)

//<-- testing1.php -->
<?php
session_start();

$_SESSION['value'] = "Text!";
?>


//<-- testing2.php -->
<?php
session_start();

echo $_SESSION['value']; //Text!

?>

请参阅本教程: PHP会话也许它可以帮助您理解使用会话

Get hold of iehttpheaders for MSIE or web developer toolbar/firebug for Firefox and check to see if a cookie is being dropped by your PHP code / presented. Also check the path and flags on the setcookie header.

Are PHP errors disabled? If so, you could have a problem in code and just not seeing it? I've had this happen where I had some white-space in the output stream before starting the session, meaning that the session broke because the session header wasn't sent first. Of course not having php errors displaying it wasn't obvious as to why the variables were null.

Just an update as I was having some issues here. Using session_name() can be helpful if the site has multiple sessions/cookies. Look in your browser preferences to see what cookies the browser is storing. I've found you have to flush these cookies a lot to see what is going on if you are using different sessions.

<?php
  session_name('mySession');
  session_start();
  $_SESSION['value'] = "Text!";
?>

<?php
  session_name('mySession');
  session_start();
  echo $_SESSION['value'];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM