簡體   English   中英

在https和http頁面之間維護PHP會話

[英]Maintain PHP session between https and http pages

在我的安全站點(https)上,設置一個PHP $ _SESSION變量。 然后,我使用header("location: http://...page.php")將用戶發送到我的http站點上的php頁面,該站點位於同一服務器上。 由於標頭語句中的http:// URL(我假設),因此會話變量丟失了。 如果不使用完整的URL,就無法使header(“ location:...”)正常工作。 因此,我嘗試了以下來自stackoverflow的技巧-切換時丟失了php會話 ,這是其他幾篇文章所引用的,但是最終我收到了許多error_log警告條目,並且當我單擊到需要$ _SESSION ['loginUser']的另一頁時,該會話不見了。

PHP警告:session_start():會話ID太長或包含非法字符

傳遞的示例會話ID:dlouenopfi3edoep3dlvne8bn1

在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");

在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");
}

我已經筋疲力盡了。 任何幫助將不勝感激。 謝謝。

我解決了兩個問題。

為了防止出現許多error_log警告條目,我所需要做的只是在“ header”語句之后使用“ exit”語句。

為了維持當前會話,我使用了if語句來測試存儲在變量$ currentSessionID中的當前會話ID。 如果是,則將session_id設置為$ currentSessionID的值。 如果否,則不要使用$ currentSessionID變量設置session_id,因為它沒有值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM