简体   繁体   English

为什么在某些页面(不是全部)之间丢失会话?

[英]Why is session lost between SOME pages, not all?

I'm pretty new to PHP and I'm struggling with my webshop. 我对PHP还是很陌生,并且在我的网上商店中挣扎。

I have a drop down menu on my site with one menu option containing all product categories in the webshop. 我的网站上有一个下拉菜单,其中一个菜单选项包含网上商店中的所有产品类别。 As long as I'm navigating between these categories my session is persistent and working fine (I'm putting products in a shopping bag and I can move to the cart and go through with the whole order). 只要我在这些类别之间导航,我的会话就会持续进行并且工作正常(我将产品放在购物袋中,可以移动到购物车并完成整个订单)。 But if I go to a page in another drop down menu option "outside" the webshop (like the contact page) my session is lost. 但是,如果我转到网店外部另一个下拉菜单选项中的某个页面(如联系页面),则我的会话将会丢失。 I have used pretty much the same template to create these pages, but they are of course more simple with mostly text content (apart from the cart that is always accessible from the top menu). 我使用了几乎相同的模板来创建这些页面,但是对于大多数文本内容来说,它们当然要更简单(除了可以从顶部菜单访问的购物车之外)。

The first page outside the webshop drop down menu option is ok with the correct session id, but when I move to the second page the session is gone. Webshop下拉菜单选项之外的第一页可以使用正确的会话ID,但是当我移至第二页时,该会话已消失。 It doesn't matter in which order I visit the pages. 我按什么顺序访问页面都没有关系。 The first one is always working but the following ones are not. 第一个始终在工作,而第二个则不能。

What on earth can be causing this behaviour? 到底是什么导致这种行为? On every page I start with this piece of code: 在每一页上,我都从以下代码开始:

<?php
session_start();
?>

<!DOCTYPE HTML>
...

Further down I include the cart in the top menu: 再往下,我在顶部菜单中包含购物车:

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/right-cart.inc";
include_once($path);

And in the included cart code I use this line for the session id: 在随附的购物车代码中,我将以下行用作会话ID:

$currSession = session_id();

Any ideas? 有任何想法吗?

EDIT 1: 编辑1:

I tried to add some error logging and noticed something interesting. 我尝试添加一些错误日志记录,并发现了一些有趣的东西。 I now start my files with this (just to find out some more information): 现在,我以此开始文件(只是为了查找更多信息):

<?php
phpinfo();
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
session_start();
// create session variable if it doesn't exist yet
if (isset($_SESSION['counter']))
  $_SESSION['counter'] ++;
else
  $_SESSION['counter'] = 1;

var_dump($_SESSION); echo "<br/>\n";
var_dump(session_id()); echo "<br/>\n";
var_dump(session_name()); echo "<br/>\n";
var_dump(session_get_cookie_params()); echo "<br/>\n";
?>

As long as I have the first row, phpinfo();, the session seems to be the same. 只要我有第一行phpinfo();,会话就好像一样。 But if I remove that line the session keeps renewing when refreshing the page... 但是,如果我删除该行,则会话在刷新页面时会不断更新...

EDIT 2 编辑2

After a suggestion I tried to use a cookie to store a "session id" instead of a regular session. 提出建议后,我尝试使用Cookie来存储“会话ID”,而不是常规会话。 The code at the top of my page now seems like this: 现在,页面顶部的代码如下所示:

<?php
session_start();
$currSession = "";
$cookie_name = "sessionId";
$cookie_value = "";
if(!isset($_COOKIE[$cookie_name])) {
    $cookie_value = session_id();
    setcookie(  $cookie_name, $cookie_value, time() + (60*60*24*2), "/");
}
if(count($_COOKIE) > 0) {
    $currSession = $_COOKIE[$cookie_name];
} else {
    $currSession = session_id();
}
?>

But everytime I reload the page also the cookie value seems to change. 但是每次我重新加载页面时,cookie的值似乎也会改变。 I have tried different echo statements in the code to verify what happens but everything looks right (the cookie is created successfully, the isset function tells me that the cookie actually is set etc), but still the value in the cookie changes. 我曾尝试在代码中使用不同的echo语句来验证会发生什么,但一切看起来都正确(cookie创建成功,isset函数告诉我cookie实际上已设置,等等),但cookie中的值仍然会更改。 Any ideas? 有任何想法吗?

I finally solved the question so I thought I'll post the answer here in case someone else is having trouble. 我终于解决了这个问题,所以我想如果其他人遇到麻烦了,我会在这里发布答案。

I changed this line ("/"): 我更改了这一行(“ /”):

setcookie($cookie_name, $cookie_value, time() + (60*60*24*2), "/");

to ('/') 至 ('/')

setcookie($cookie_name, $cookie_value, time() + (60*60*24*2), '/');

and suddenly it works! 突然间它起作用了! :-) :-)

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

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