在PHP中从HTTP切换到HTTPS时会话丢失 [英] Session lost when switching from HTTP to HTTPS in PHP
问题描述
将用户发送到结帐页面时,会将其从 http://sitename.com
切换为 https://sitename.com
。
When sending the user to a checkout page, they are switched from http://sitename.com
to https://sitename.com
.
因此, $ _ SESSION
变量将丢失。
该网站有一个有效的SSL证书,可能会或可能没有用。
The site has a valid SSL certificate which may or may not be of some use.
推荐答案
在同一服务器上切换HTTP和HTTPS服务时,您的HTTP会话ID不会传递到HTTPS会话。您可以通过以下三种方式之一将会话ID从HTTP页面传递到HTTPS页面来设置它:
When you switch between the HTTP and HTTPS services on the same server, your HTTP session ID is not being passed to the HTTPS session. You can set it by passing the session ID from the HTTP page to the HTTPS page in one of three possible ways:
来自 PHP:session_start :
session_start ()
创建会话或恢复当前通过请求传递的会话ID的
,例如
GET,POST或cookie
session_start()
creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie
当您使用会话时,通常会使用 session_start()
启动脚本。如果浏览器设置了会话ID cookie, session_start()
将使用该会话ID。如果浏览器没有设置会话ID cookie, session_start()
将创建一个新的。
When you are using sessions, you will normally start your script with session_start()
. If the browser has a session ID cookie set, session_start()
will use that session ID. If the browser does not have a session ID cookie set, session_start()
will create a new one.
如果未设置会话ID(在您的示例中,浏览器正在为HTTPS会话创建新的会话ID cookie),您可以使用 session_id()
功能设置它。 session_id()
还可以方便地将会话ID作为字符串返回。所以
If the session ID is not set(in your example, the browser is creating a new session ID cookie for the HTTPS session), you can set it using the session_id()
function. session_id()
also conveniently returns the session ID as a string. So
...
$currentSessionID = session_id();
...
设置 $ currentSessionID
变量等于当前会话ID,
sets the $currentSessionID
variable equal to the current session ID, and
...
session_id($aSessionID);
...
将浏览器中的sessionID cookie设置为 $ aSessionID
。来自 PHP:session_id
sets the sessionID cookie in the browser to $aSessionID
. from PHP: session_id
以下是两个脚本的示例。一个通过HTTP访问,另一个通过HTTPS访问。它们必须位于同一台服务器上才能维护会话数据。
Here's an example with two scripts. One is accessed via HTTP and the other is accessed via HTTPS. They must be on the same server to maintain session data.
脚本1(HTTP):
<?php
// This script will create a session and display a link to your secure server address
// to transfer your session ID. In this example, the secure page to receive the session
// ID is located at http://www.yoursite.com/safePages/securePage.php
// Start a session using the current session ID stored in a cookie, or create
// a new session if none is set.
session_start();
$currentSessionID = session_id();
// Set a variable that will be retrieved with the HTTPS script.
$_SESSION['testvariable'] = 'It worked';
// $secureServerDomain is the domain of your secure server
$secureServerDomain = 'www.yoursite.com';
// $securePagePath is the path to the page that will receive and set the session ID.
$securePagePath = '/safePages/securePage.php'
echo '<a href="https://' . $secureServerDomain . $securePagePath . '?session="' . $currentSessionID . '">Click here to transfer your session to the secure server</a>';
?>
脚本2(HTTPS):
<?php
// Retrieve the session ID as passed via the GET method.
$currentSessionID = $_GET['session'];
// Set a cookie for the session ID.
session_id($currentSessionID);
// Start a session.
session_start();
// Test retrieval of variable set when using HTTP.
if (!empty($_SESSION['testvariable'])) {
echo $_SESSION['testvariable'];
} else {
echo 'It did not work.';
}
?>
为了使其工作,HTTP和HTTPS服务器必须使用相同的会话数据存储基板(即对于默认文件处理程序,在具有相同php.ini的同一物理机器上运行)。这里存在一些安全漏洞,所以我不会使用此代码来传输敏感信息它只是一个可行的例子。
For this to work the HTTP and HTTPS servers must use the same session data storage substrate (i.e. for the default files handler, run on the same physical machine with the same php.ini). There are some security flaws here, so I would not use this code to transfer sensitive information. It is just meant as a workable example.
当我遇到这个问题之前,我想出了上面的快速修复,但我记得问题的原因。我是从 http://www.example.com/page.php 转到 https://example.com/page.php (请注意缺少www)。确保 http://www.example.com/page.php 链接到 https://www.example.com/page.php 和 http://example.com 将链接到 https:/ /example.com/page.php 。
When I ran into this problem before, I came up with the above as a quick fix, but I just remembered the original cause of the problem. I was going from http://www.example.com/page.php to https://example.com/page.php (notice the lack of "www"). Make sure that http://www.example.com/page.php will link to https://www.example.com/page.php and http://example.com will link to https://example.com/page.php.
PS,我实际上并没有运行这些脚本,所以可能会有一两个错字阻止它们按原样正常运行。
这篇关于在PHP中从HTTP切换到HTTPS时会话丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!