問題描述
我有一個使用會話變量的應用程序,沒問題.我在使用 when 的每個頁面上的標題之前啟動會話,它一直很好然后似乎突然我在導航到設置會話變量的頁面以外的頁面時收到未定義的索引錯誤.但僅限于某些瀏覽器.有時會維護會話,有時則不會.
I have an application that has been working with session variables no problem. I start the session before the headers on every page that uses when, it has been fine then it seems all of a sudden I'm getting an undefined index error when I navigate to a page other than the one that sets up the session variables. But only on some browsers. Sometimes sessions are maintained and sometimes they aren't.
似乎有時不會存儲 cookie.我使用不同的瀏覽器進行了檢查,有時會存儲 cookie,有時不會.
It seems that cookies aren't being stored some of the time. I've done checks using different browsers and sometimes cookies are stored and sometimes not.
我做了一個實驗.我正在使用 Firefox 來使用應用程序,并且我一直在關注存儲會話的 tmp 文件夾.我把它清理干凈了.使用 firefox 我開始使用該應用程序,使用會話正在使用的所有頁面,最后我檢查了 tmp 文件夾,其中有一個會話文件.
I did an experiment. I was using firefox to use to app and I was keeping an eye on the tmp folder where the sessions are stored. I cleaned it out. Using firefox I started using the app, using all the pages that sessions were in use and at the end I checked the tmp folder and it had one session file in there.
與 Internet Explorer 完全相同,現在有 7 個不同的會話文件.
Did the exact same with internet explorer and there are now 7 different session files.
我將 PHP 5.3.0 與 WAMP 堆棧一起使用.阿帕奇 2.2.11.在我的 phpinfo() 中啟用了會話支持.
I'm using PHP 5.3.0 with the WAMP stack. Apache 2.2.11. Session support is enabled in my phpinfo().
我在第一頁調用 var dump 并打印出會話數據.在任何后續頁面上,會話變量為空.
I call a var dump on the first page and it prints out the session data. On any subsequent pages the session variable is empty.
<?php var_dump($_SESSION); ?>
array(0){}
誰能幫我想辦法解決這個問題?
Can anyone help me figure out a solution to this?
更新 - PHP INI 會話設置
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain 82.68.26.169 82.68.26.169
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 5 5
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path c:/wamp/tmp c:/wamp/tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies On On
session.use_trans_sid 0 0
更新 - 解決方案
因為我的應用程序使用 iframe 從另一個域(我創建的)拉入頁面,所以我試圖設置的 cookie 被阻止.設置一個P3P頭,問題似乎解決了!
Because my app was using iframes pulling in pages from another domain (which i created) the cookies i was trying to set were being blocked. Setup a P3P header and the problem seems to be solved!
推薦答案
我的建議來自 您之前的問題 仍然有效:請比較會話 ID.
My suggestion from your previous question still stands: please compare session ids.
解決方案可能就像您的瀏覽器不接受會話 cookie 一樣簡單.
The solution might be as simple as your browser not accepting session cookies.
您可以通過調用 session_id()
來檢索會話 ID.在 session_start()
之后立即執行它應該給你一個常量值 如果 會話是相同的.否則,對于每個請求,都會實例化一個新會話.
You retrieve the session id by calling session_id()
. Do that right after session_start()
it should give you a constant value if the session is the same. Otherwise for every request a new session is instantiated.
同時檢查C:wamp mp
.此目錄中的無數文件可能表示每個請求的新會話.
Also check C:wamp mp
. A gazillion files in this directory might indicate fresh sessions for each request.
EDIT 由于我們已經確認了每個請求的新會話,現在是時候確定是否接受會話 cookie.檢查瀏覽器的設置并確認可以找到名稱為 PHPSESSID
的域的 cookie(我猜是localhost").
EDIT Since we've confirmed new sessions per request, it's time to find out whether session cookies are accepted. Check the settings of your browser and confirm that a cookie for your domain (I guess it's "localhost") with the name PHPSESSID
can be found.
這篇關于未維護 PHP 會話變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!