简体   繁体   English

在PHP中共享会话实例

[英]Sharing session instances in PHP

I want to use session in PHP. 我想在PHP中使用session。 But its showing some problems in my scenario. 但它在我的场景中显示出一些问题。

I want to share same session in 3 different PHP files. 我想在3个不同的PHP文件中共享同一个会话。

./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 如果我将一些信息存储在testing1.php $ _SESSION中,我就无法访问其他2个文件中的相同信息

what should i do to make these 3 files share the same session instance? 我该怎么做才能使这3个文件共享同一个会话实例?
Is there any other(except cookie) to make this possible? 还有其他(除了cookie)使这成为可能吗?

PS These 3 files are executed by different calls, cant include one file into another using include() or require() functions. PS这3个文件由不同的调用执行,不能使用include()或require()函数将一个文件包含到另一个文件中。

Added session_start() at the top but still doesnt share the same session. 在顶部添加了session_start(),但仍然没有共享同一个会话。

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. 获取MSIE的iehttpheaders或Firefox的Web开发人员工具栏/ firebug,并检查您的PHP代码/呈现的cookie是否被删除。 Also check the path and flags on the setcookie header. 还要检查setcookie标头上的路径和标志。

Are PHP errors disabled? PHP错误是否被禁用? 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. 当然,没有php错误显示它并不明显为什么变量为null。

Just an update as I was having some issues here. 只是一个更新,因为我在这里遇到了一些问题。 Using session_name() can be helpful if the site has multiple sessions/cookies. 如果站点有多个会话/ cookie,则使用session_name()会很有帮助。 Look in your browser preferences to see what cookies the browser is storing. 查看浏览器首选项以查看浏览器存储的cookie。 I've found you have to flush these cookies a lot to see what is going on if you are using different sessions. 我发现如果你使用不同的会话,你必须经常刷这些cookie才能看到发生了什么。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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