简体   繁体   English

php 访问 HTTPS 和 Z293C9EA246FF9985DC6F62A650ZF7 之间的 session 数据

[英]php access session data between HTTPS and HTTP

Thanks for your replies.感谢您的回复。 I have updated my PHP session code.我已经更新了我的 PHP session 代码。

I have a (HTTPS)-login.php which remains HTTPS ie once user logged in goes to account dashboard.我有一个(HTTPS)-login.php,它仍然是 HTTPS,即一旦用户登录到帐户仪表板。 Now the problem is say the user whilst logged on to the dashboard clicks on the (HTTP)-about-us.php page the session is not transmitted over HTTP as I have session.cookie_secure=1, thats fine user appears logged out. Now the problem is say the user whilst logged on to the dashboard clicks on the (HTTP)-about-us.php page the session is not transmitted over HTTP as I have session.cookie_secure=1, thats fine user appears logged out. However when the user goes back to dashboard page he is logged out on HTTPS as well?但是,当用户返回仪表板页面时,他是否也会在 HTTPS 上注销?

I believe I am missing something which is causing this problem.我相信我错过了导致这个问题的东西。 Here is my code:这是我的代码:

This is PHP header file require()ed to start session ie on login.php page:这是 PHP header 文件需要()启动 session 即登录页面。ZE1BFDZ462321E4063E

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();}

This is PHP header file require()ed on every page to check if session initiated already:这是 PHP header 文件在每个页面上都需要()检查 session 是否已经启动:

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();}

Also if any use this is the code to turn HTTPS of on non-sensitive pages such as about-us.php此外,如果有任何用途,这是在非敏感页面上打开 HTTPS 的代码,例如 about-us.php

if ($_SERVER['SERVER_PORT']!=80)
{
$url = "http://". $_SERVER['SERVER_NAME'] . ":80".$_SERVER['REQUEST_URI'];
header("Location: $url");
}

Thanks again for any help guys, daza166再次感谢您的帮助,daza166

It looks like you're asking a few different questions here, but to address this:看起来你在这里问了几个不同的问题,但要解决这个问题:

I was thinking is there any reason really to check user agent/IP etc as although it will lessen the chances of hijack it is simply comparing $_SESSION==$_SESSION ie (www.domain.com/login.php?hacker=no)我在想是否有任何理由真正检查用户代理/IP 等,因为它会减少劫持的机会,它只是比较 $_SESSION==$_SESSION 即(www.domain.com/login.php?hacker=no)

If you're asking why people compare session variables to what is being submitted, the answer is because the variables stored in $_SESSION were defined at the beginning of the session, ie when the user logged in, presumably before the hijacking took place.如果你问为什么人们将 session 变量与提交的变量进行比较,答案是因为存储在$_SESSION中的变量是在 session 的开头定义的,即用户登录时,大概是在劫持发生之前。 (The hijacker can only hijack an existing session, and that session may have started without the hijacker having been involved.) Because of that, if we regularly compare the user agent string or IP address provided with the page request to the one we have stored from the beginning of our session, we may be able to detect a hijacking (assuming the hijacker has a different user agent string/IP address). (The hijacker can only hijack an existing session, and that session may have started without the hijacker having been involved.) Because of that, if we regularly compare the user agent string or IP address provided with the page request to the one we have stored从我们的 session 开始,我们可能能够检测到劫持(假设劫持者具有不同的用户代理字符串/IP 地址)。

I don't know the answer to your HTTPS question.我不知道您的 HTTPS 问题的答案。

If you use ini_set('session.cookie_secure',1);如果你使用ini_set('session.cookie_secure',1); , the cookie with the session-id will only be transfered to the server if the connection is encrypted. , 带有 session-id 的 cookie 只有在连接被加密的情况下才会被传输到服务器。 So if you force the user to access about-us.php via an unsecure http-connection, your script won't receive the cookie and he will appear as logedout user on the page.因此,如果您强制用户通过不安全的 http 连接访问 about-us.php,您的脚本将不会收到 cookie,并且他将在页面上显示为已注销的用户。 You won't be able to access any session-variables.您将无法访问任何会话变量。

However, neither the cookie on the client nor the session data on the server is deleted.但是,客户端上的 cookie 和服务器上的 session 数据都不会被删除。 So, if the user visits an encrypted page of your site later (within the lifetime of the session and the cookie), the still existing cookie with the session-id is transfered and he won't have to login again.因此,如果用户稍后访问您网站的加密页面(在 session 和 cookie 的生命周期内),带有 session-id 的仍然存在的 cookie 将被转移,他不必再次登录。 In short, going from HTTPS to HTTP and back again won't log out the user.简而言之,从 HTTPS 到 HTTP 再返回不会注销用户。 If you don't need to check the user's login-status on an unencrypted page, setting cookie_secure is a good idea.如果您不需要在未加密的页面上检查用户的登录状态,那么设置 cookie_secure 是个好主意。

To your other questions: In my opinion, checking the user agent does not significantly increase the level of security, because a hacker who is able to retrieve someone's session-id won't have many problems retrieving also his user-agent-string.对于您的其他问题:在我看来,检查用户代理不会显着提高安全级别,因为能够检索某人的会话 ID 的黑客在检索他的用户代理字符串时也不会遇到很多问题。 Checking the id makes sense, but can cause problems if the users ip changes often due to reconnects or changing proxies.检查 id 是有意义的,但如果用户 ip 由于重新连接或更改代理而经常更改,则可能会导致问题。

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

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