PHP 会话未设置 [英] PHP sessions getting unset

查看:44
本文介绍了PHP 会话未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用会话变量来存储登录数据.在本地,一切都很顺利,它可以很好地登录,并且不会在页面之间注销.

I'm using a session variable to store login data. Locally, everything works smooth, it logs in nicely and doesn't log out inbetween pages.

但是当我将相同的文件放在服务器上时,由于某种原因我被注销了.我已经找到 this 帖子,并尝试使用以下代码防止缓存动态页面:

But when I put the same files on the server, for some reason I get logged out. I've already found this post, and tried using following code to prevent caching of dynamic pages:

header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

虽然这似乎不起作用.这个问题还有其他可能的解决方案吗?

Though that doesn't seem to work. Are there any other possible solutions to this problem?

请注意 session_start() 在每个页面上.

Note that session_start() is on every page.

编辑 2:我做了一些进一步的调查,在 mysite.com<上时,我的一些链接似乎链接到 www.mysite.com/代码>.转到这个新页面会使 $_SESSION[] 变量未设置,当使用后退按钮返回上一页时,您可以看到 $_SESSION[] 仍然设置.

Edit 2: I did a little further investigation and it appears some of my links link to www.mysite.com when on the mysite.com. Going to this new page makes that the $_SESSION[] vars aren't set, and when returning to the previous page with the back button you can see the $_SESSION[] is still set.

推荐答案

会话使用 cookie,因此当您更改域(www.site.com 到 site.com)时,这些 cookie 不会被传输.

Sessions use cookies, so when you change domains (www.site.com to site.com) those cookies aren't being transmitted.

将您的网络服务器设置为具有一个规范网址,并将所有人重定向到该网址(例如,在 site.com 上进行标准化,将 www.site.com 重定向到 site.com).

Set up your webserver to have one canonical url, and redirect everyone to that (for example, standardize on site.com, redirect www.site.com to site.com).

这就是我在 apache 中处理重定向的方式:

This is how I handle redirects in apache:

ServerAlias www.site.com my.site.com site.net www.site.net sites.com www.sites.com site.com
RewriteEngine On
RewriteCond %{SERVER_NAME} !=site.com
RewriteRule ^(.*) https://site.com/$1 [R=301]

与 www.site.com 相比,我更喜欢 site.com,除非您对这两个网站做了一些不同的事情,让人们输入额外的四个字符似乎毫无价值.我对 SEO 的(蹩脚)理解是,您最好选择一个而不是在两个网址(site.com/stuff 和 www.site.com/stuff)上有重复的内容

I prefer site.com over www.site.com, unless you're doing something different on the two of them making people type an extra four characters seems worthless. My (lame) understanding of SEO is that you're better off picking one than having duplicate content at two urls (site.com/stuff and www.site.com/stuff)

这篇关于PHP 会话未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆