2016-06-28 89 views
-1

我處理POST請求,設置cookie,然後重定向,像這樣的用戶:返回PHP的setcookie不工作,儘管頭尋找合適的

// (handle post request) 

// all fine so set cookie 
$ciphertext = Crypto::encrypt($_POST['soulmates_member_id'], Key::loadFromAsciiSafeString($this->encryption_key)); 
$expires = 60 * 60 * 24 * 30; 
setcookie('soulmates_member_id', $ciphertext, $expires, '/', $_SERVER['HTTP_HOST']); 

// redirect 
header("Location: ".$_POST['soulmates_redirect']); 

以下回應:

HTTP/1.1 302 Found 
Date: Tue, 28 Jun 2016 10:53:21 GMT 
Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.21 
X-Powered-By: PHP/5.6.21 
Expires: Wed, 11 Jan 1984 05:00:00 GMT 
Cache-Control: no-cache, must-revalidate, max-age=0 
Pragma: no-cache 
Access-Control-Allow-Origin: http://local.wordpress.com 
Access-Control-Allow-Credentials: true 
X-Robots-Tag: noindex 
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN 
Set-Cookie: soulmates_member_id=def5020032ce3903334d3564b22303993dc3bd5923256632200d94785aa9cd09a44091a124848bd4476768eb5027082b01ec4036c4fa366ba41613157d548285d8cbee1b1115b0fc3ec454127e62732db13fb72b4ff385eceeae1b7af7c1; expires=Sat, 31-Jan-1970 00:00:00 GMT; Max-Age=-1464519202; path=/; domain=local.wordpress.com 
Location: http://local.wordpress.com/another-page/ 
Content-Length: 0 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Content-Type: text/html; charset=UTF-8 

但該cookie不會被設置。我已經在Chrome和Firefox中嘗試了這一點,並且由於某種原因,Cookie並未設置。

+0

'$到期= 60 * 60 * 24 * 30;',你需要包括'時間()+ ...'。 – Legionar

回答

3

我解決了它!這是因爲到期時間需要是相對於現在,這樣的到期日期和時間是在將來,像這樣:

$expires = time() + 60 * 60 * 24 * 30;