PHP session HTTP to HTTPS 问题
我有一个 (HTTPS) login.php 页面,它仍然是 HTTPS(即用户登录后转到帐户仪表板).现在的问题是说用户在登录到安全仪表板时点击了一个非敏感页面,如(HTTP)about-us.php 页面,会话不会通过 HTTP 传输,因为我有 session.cookie_secure=1,这意味着用户在 HTTP 页面上显示为已注销.
I have a (HTTPS) login.php page which remains HTTPS (ie once user logged in goes to account dashboard). Now the problem is say the user whilst logged on to the secure dashboard clicks onto a non-sensitive page like (HTTP) about-us.php page, the session is not transmitted over HTTP as I have session.cookie_secure=1, meaning the user appears logged out on HTTP pages.
但是,当用户返回仪表板页面或任何敏感帐户页面时,我被告知他仍应登录(即从 HTTP 返回到 HTTPS)?然而事实并非如此,他似乎也在 HTTPS 连接上注销了?
However when the user goes back to dashboard page or any sensitive account page I have been told he should still be logged in (ie from HTTP back to HTTPS)? However this is not the case and he appears logged out on the HTTPS connection too?
我相信我遗漏了导致此问题的某些内容.这是我的代码:
I believe I am missing something which is causing this problem. Here is my code:
这是在 login.php 页面上调用以启动会话的 PHP 头文件:
This is PHP header file which is called to start session on login.php page:
session_start();
session_regenerate_id(true); /*avoid session fixation attempt*/
/*Create and check how long session has been started (over 5 mins) regenerate id - avoid session hijack*/
if(!isset($_SESSION['CREATED']))
{
$_SESSION['CREATED'] = time();/*time created session, ie from login/contact advertiser/email_confirm only ways for new session to start*/
}
elseif(time() - $_SESSION['CREATED'] > 300)
{
/*session started more than 5 mins(300 secs) ago*/
session_regenerate_id(true); /*change session ID for the current session and invalidate old session ID*/
$_SESSION['CREATED'] = time(); /*update creation time*/
}
/*Check if user is logged in*/
if(!isset($_SESSION['loggedin']))
{
$_SESSION['loggedin']=1;/*used to track if user is logged in on pages*/
}
/*if return false browser supports standard ob_start();*/
if(ob_start("ob_gzhandler")){ob_start();}
这是每个页面都需要的 PHP 头文件,用于检查会话是否已经启动:
This is PHP header file required on every page to check if session initiated already:
session_start();
$session_errors=0;/* if>0 user not logged in*/
/*check if session is already initiated*/
if(isset($_SESSION['CREATED']))
{
if(time() - $_SESSION['CREATED'] > 300)
{
/*session started more than 5 mins(300 secs) ago*/
session_regenerate_id(true); /*change session ID for the current session and invalidate old session ID*/
$_SESSION['CREATED'] = time(); /*update creation time*/
}
}
elseif(!isset($_SESSION['CREATED'])){$session_errors++;}/*user not logged in*/
/*Check if user is logged in*/
if(!isset($_SESSION['loggedin'])){$session_errors++;}/*user not logged in*/
if(ob_start("ob_gzhandler")){ob_start();}
此外,如果有任何用途,这是在非敏感页面(例如 about-us.php)上打开 HTTPS 的代码
Also if any use this is the code to turn HTTPS of on non-sensitive pages such as about-us.php
if ($_SERVER['SERVER_PORT']!=80)
{
$url = "http://". $_SERVER['SERVER_NAME'] . ":80".$_SERVER['REQUEST_URI'];
header("Location: $url");
}
我的 php.ini 文件 cookie 设置
My php.ini file cookie settings
session.cookie_secure=1
session.cookie_httponly=1
session.use_only_cookies=1
session.cookie_lifetime = 0
session.save_path = /tmp
session.save_handler = files
推荐答案
已回答以帮助可能偶然发现此问题的人
作为 切换时会话丢失的答案PHP 中从 HTTP 到 HTTPS 已经结束,因为您使用的是 session.cookie_secure = 1
包含会话 ID 的 cookie 在连接从 HTTPS 切换到 HTTP 时不会传输.在 HTTP 连接时,当您 session_start()
时,PHP 会创建一个新的会话 ID,该 ID 会替换之前的会话 ID.
As the the answer at Session lost when switching from HTTP to HTTPS in PHP has concluded, since you are using session.cookie_secure = 1
the cookie that contains the session ID is not transferred when the connection switches from HTTPS to HTTP. At HTTP connection, when you session_start()
, PHP creates a new session id, which replaces the previous session id.
答案还提出了一个解决方案,使用查询字符串传递会话 id,然后由页面获取.这闻起来有安全漏洞的坏处.别忘了我们最初使用 HTTPS 的原因!
The answer also suggests a solution, pass the session id using query string, which is then picked up by the page. This smells of bad of security flaw. Don't forget the reason why we used HTTPS in the first place!
因此,我建议您的解决方案是将所有 http 请求重定向到 https 对应请求.对站点中的所有内容使用 HTTPS,从 css、图像到普通的静态 html 页面.这实际上是每个认真对待安全性的应用程序都会做的事情.比如使用HTTP访问github页面会返回:
So the solution I suggest to you is that you redirect all http request to https counterparts. Use HTTPS for everything in your site, from css, images, to mundane static html pages. This is actually something that every application that is serious about security does. For example, visiting github page using HTTP will return:
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.67
Date: Sun, 08 May 2011 15:43:01 GMT
Content-Type: text/html
Content-Length: 185
Connection: close
Location: https://github.com/
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.67</center>
</body>
</html>
记住您最初使用 HTTPS 的原因,如果您想完全安全,请在所有情况下都使用 HTTPS.
Remember why you used HTTPS in the first place, if you want to be totally secure, use HTTPS for everything.
检测请求是否为 HTTPS (见这个问题)在引导.
Detect if the request is HTTPS or not (See this question) at bootstrap.
如果请求是 HTTP,要么将所有请求重定向到 HTTPS 主页,或者您可以尝试解析 $_SERVER['REQUEST_URI']
并使用 parse_url 将 HTTP 请求重定向到对应的 HTTPS 请求
和 http_build_url
.
If the request is HTTP, either redirect all requests to HTTPS home page, or you can try parsing $_SERVER['REQUEST_URI']
and redirecting HTTP request to their HTTPS counterpart using parse_url
and http_build_url
.
第二种替代方案
如果您真的不想对所有内容都使用 HTTPS,那么请不要在使用 HTTP 访问的页面上session_start()
.当您这样做时,安全 cookie 将被保留.
If you really really don't want to use HTTPS for everything, then don't session_start()
on pages that are accessed with HTTP. Secure cookies will be retained when you do this.
第三种替代方案
另一种解决方案是尝试通过 IP 地址和用户代理检测用户.这不能保证是准确的,所以我的建议是对所有事情都使用 HTTPS.例如,即使是普通的静态页面,Paypal 也始终使用 HTTPS.
The other solution is to try and detect the user by IP addresses and user agent. This is not guaranteed to be accurate, so what I suggest is just use HTTPS for everything. Paypal, for example, always use HTTPS even for mundane static pages.
相关文章