简体   繁体   English

在https和http页面之间维护PHP会话

[英]Maintain PHP session between https and http pages

On my secure site (https), I set a PHP $_SESSION variable. 在我的安全站点(https)上,设置一个PHP $ _SESSION变量。 I then use header("location: http://...page.php") to send the user to a php page on my http site, which is on the same server. 然后,我使用header("location: http://...page.php")将用户发送到我的http站点上的php页面,该站点位于同一服务器上。 The session variable is lost, because of the http:// URL (I assume) in the header statement. 由于标头语句中的http:// URL(我假设),因此会话变量丢失了。 I can't get the header("location: ...") to work without using the full URL. 如果不使用完整的URL,就无法使header(“ location:...”)正常工作。 Thus I tried the following tip from stackoverflow - php session lost when switching , which several other posts reference, but I ended up with numerous error_log warning entries and once I clicked to another page that required $_SESSION['loginUser'], the session was gone. 因此,我尝试了以下来自stackoverflow的技巧-切换时丢失了php会话 ,这是其他几篇文章所引用的,但是最终我收到了许多error_log警告条目,并且当我单击到需要$ _SESSION ['loginUser']的另一页时,该会话不见了。

PHP Warning: session_start(): The session id is too long or contains illegal characters PHP警告:session_start():会话ID太长或包含非法字符

Sample session ID passed: dlouenopfi3edoep3dlvne8bn1 传递的示例会话ID:dlouenopfi3edoep3dlvne8bn1

Code that creates the session on https php page (note for this post header location is not real) 在https php页面上创建会话的代码(此帖子标题位置的注释不是真实的)

session_start();
$currentSessionID = session_id();
$_SESSION['loginUser'] = $username; 

header("location: http://www.test.com/path/to/page/off-campus/cat_index.php?session=$currentSessionID");

Code that receives the session on http php pages 在HTTP PHP页面上接收会话的代码

// Retrieve the session ID as passed via the GET method.
$currentSessionID = $_GET['session'];
echo "sid: " . $currentSessionID;//a session id like above is displayed

// Set a cookie for the session ID.
session_id($currentSessionID);

session_start();

if(isset($_SESSION['loginUser'])){
  $username = $_SESSION['loginUser'];
  echo "Welcome: $username<br />";
 } else {
    require_once($_SERVER["DOCUMENT_ROOT"] . "/_includes/CASwrap.php");
}

I've exhausted my searching. 我已经筋疲力尽了。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

I solved my two questions. 我解决了两个问题。

To prevent the numerous error_log warning entries all I needed was an "exit" statement after the "header" statement. 为了防止出现许多error_log警告条目,我所需要做的只是在“ header”语句之后使用“ exit”语句。

To maintain the current session, I used an if statement to test for a current session id stored in the variable $currentSessionID. 为了维持当前会话,我使用了if语句来测试存储在变量$ currentSessionID中的当前会话ID。 If yes then set the session_id with the value of $currentSessionID. 如果是,则将session_id设置为$ currentSessionID的值。 If no, then don't set the session_id with the $currentSessionID variable, since it has no value. 如果否,则不要使用$ currentSessionID变量设置session_id,因为它没有值。

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

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