php 在服务器运行不起,PHP Cookies在localhost上运行良好,但在实时服务器上不起作用...
Note: This issue is already solved,
finally I found that it's not cookies
problem, the problem is on
unserialize() function. The serialized
cookie which being the parameter of
that function must be stripslash-ed
first.
嗨,我在這里有關于PHP Cookies的問題。我正在使用PHP Cookies來保存用戶首選項。我已經在本地計算機(使用XAMPP的本地主機)上測試了我的代碼。一切正常,包括cookie。但是,當我將其上傳到實時服務器時,cookie根本無法工作。似乎setcookie()函數未寫入cookie值。我已經通過在本地主機和實時服務器上回顯cookie值進行了測試。本地主機上的$ _COOKIE []值正在顯示,但沒有顯示在實時服務器中。
我認為這可能與$ expire時區有關,就像本文中的那個一樣:http://anupraj.com.np/index.php/php-cookies-not-working-php-cookie-tutorial-and-scirpt/14 。但是后來我意識到,我已經將Cookie設置為在1個月后過期,而不僅僅是在該博客文章中的一個小時內過期。所以我認為并非如此。
這是setting.php的內容
$defaultSettings['default_post_to'] = 'both';
$defaultSettings['timesince_style'] = 'simplify';
...
$defaultSettings['display_geo_info'] = 'true';
$defaultSettings['enable_javascript'] = 'true';
if(!isset($_COOKIE['settings'])){
setcookie("settings", serialize($defaultSettings), time()+3600*24*30);
header('Location: index.php');
}
$setting = unserialize($_COOKIE['settings']);
?>
這是index.php的內容
/*
ini_set ("display_errors","1");
error_reporting(E_ALL);
*/
session_start();
require_once("settings.php"); // Settings files
require_once('varlib.php'); // Get all possible passed variable
require_once('auth.php'); // Check for channel login status
// If inputbar form submitted
if( $_POST['inputbox'] ){
...
}
else{
echo"SETTING COOKIE:
";
// This print_r is only showing the $_COOKIE value (which is stored on $setting) on localhost but no on live server
print_r($setting);
switch( $com ){
...
}
}
?>
我到處都在搜索它(谷歌,stackoverflow,在twiiter / FB上問朋友)仍然沒有解決方案
我希望有人能給我解決方案
謝謝 :)
您是否嘗試過setcookie()的path和domain參數?
如果啟用error_reporting,您不會收到任何錯誤消息嗎?
@Kamal:比較本地服務器和實時服務器phpinfo()輸出的session部分(還顯示了cookie設置)。
@rik:還沒有,我會盡快嘗試的
@ Dr.Molle:我的代碼的另一部分實際上給了很多警告:p,但是沒有一個與此Cookie問題有關
@stealthyninja:我已經比較了,有一些區別:
您怎么能確定?如果警告/錯誤導致任何輸出,則無法設置您的cookie。
實時服務器:注冊的序列化處理程序:php php_binary; session.hash_bits_per_character:4; session.use_only_cookies:開;
LOCALHOST:注冊的序列化處理程序:php php_binary wddx; session.hash_bits_per_character:5; session.use_only_cookies:關閉;
@ Dr.Molle:嗯...實際上不確定,但是我在那些警告消息上看不到任何與cookie相關的錯誤/警告消息
警告說什么?
@ Dr.Molle:只是很多未定義的索引,像這樣:注意:未定義的索引:第23行的D: xampp htdocs Litwe litwe varlib.php中的頁面
實際上,在Stack Overflow上,當您解決了自己的問題時,應將解決方案作為答案發布,并將其標記為已接受,而不是在問題標題之前加[solved]
@Yi Jiang:呵呵...感謝您的信息,我是新手在這里:)
查看setcookie函數的路徑和域參數。
參考:setcookie @ PHP文檔http://php.net/manual/zh/function.setcookie.php
試試這個來設置你的cookie:
if ($on_localhost) { // change this
$domain = '.localhost';
} else {
$domain = '.webhoster.com'; // change this
}
setcookie(
'settings',
serialize($defaultSettings),
time()+3600*24*30,
'/', ? ? ? ? ?// this is the path
$domain ? ? ? // this is the domain
);
祝好運!
在應用解決方案時,我們忘記了Cookie的基本知識。
Cookie就像標題一樣。像標題一樣,應在生成任何輸出之前發送它。那么只有它設置成功。我為此問題付出了很多努力,但是當我掌握了基礎知識后,這個問題很快就解決了。
這種語法足以解決這個問題...
setcookie(
'settings',
serialize($defaultSettings),
time()+3600*24*30,
'/' ? ? ? ? ?// this is the path
);
可能您的服務器時間不正確,因此Cookeis無法在服務器上工作。
嘗試這個:
setcookie("settings", serialize($defaultSettings), 0);
將到期時間設置為零將在這種情況下解決您的問題。或更新您的服務器時間。
在位置標題后嘗試exit()。
位置標頭不會阻止PHP腳本執行進一步的指令,也許標頭后執行了某些操作會導致行為不當。
嘗試這個:
setcookie("settings", serialize($defaultSettings), time()+3600*24*30, '/'); // added path
另外,可能是serialize($defaultSettings)結果太大嗎?
嘿,謝謝你的提示。 我發現似乎問題出在于serialize()/ unserialize()函數。 serialize函數確實起作用,但是unserialize()函數似乎什么也不返回。 真的是因為它太大了嗎?
Cookie的大小通常限制為4 KB
總結
以上是生活随笔為你收集整理的php 在服务器运行不起,PHP Cookies在localhost上运行良好,但在实时服务器上不起作用...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 目前国际上存在的主要安全标准
- 下一篇: PHP字符串函数stripslashes