PHP 세션 데이터가 저장되지 않음
"서버에 손대지 않았다고 맹세합니다"상황 중 하나가 있습니다. 솔직히 PHP 스크립트를 건드리지 않았습니다. 내가 가진 문제는 PHP 데이터가 다른 페이지 또는 페이지 새로 고침에 저장되지 않는다는 것입니다. 세션 변수 (예 : $ _SESSION [ 'foo'] = "foo")를 설정하고 동일한 페이지에 다시 인쇄 할 수 있기 때문에 새 세션이 올바르게 생성되고 있음을 알고 있습니다.하지만 동일한 변수를 사용하려고 할 때 다른 페이지에서는 설정되어 있지 않습니다! 내 호스트 서버에서 무슨 일이 일어나고 있는지 확인하기 위해 사용할 수있는 PHP 기능이나 정보가 있습니까?
다음은 현재 내 호스트의 서버에서 작동하지 않는 예제 스크립트입니다.
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;
echo "views = ". $_SESSION['views'];
echo '<p><a href="page1.php">Refresh</a></p>';
?>
페이지 새로 고침을 수행 한 후 'views'변수가 증가하지 않습니다. 나는 이것이 그들의 편에서 문제라고 생각하고 있지만 나는 먼저 내가 완전한 바보가 아닌지 확인하고 싶었다.
다음은 내 호스트 서버 (PHP 버전 4.4.7)에 대한 phpinfo ()입니다.
모든 유용한 정보에 감사드립니다. 내 호스트가 서버를 변경하고 더 이상 존재하지 않는 / var / php_sessions 이외의 다른 세션 저장 경로를 사용하기 시작했습니다. 해결책은 ini_set(' session.save_path','SOME WRITABLE PATH');
내 모든 스크립트 파일에서 선언 하는 것이었지만 고통 스러웠을 것입니다. 나는 호스트와 이야기했고 그들은 세션 경로를 존재했던 실제 경로로 명시 적으로 설정했습니다. 이것이 세션 경로 문제가있는 모든 사람에게 도움이되기를 바랍니다.
https : //와 http : //를 혼합하지 않았는지 확인하십시오. 세션 변수는 보안 세션과 비보안 세션간에 흐르지 않습니다.
같은 문제가 발생했습니다. 서버 관리자가 session.cookie_secure 부울을 On으로 변경하여 쿠키가 보안 연결을 통해서만 전송된다는 것을 의미합니다. 쿠키가 발견되지 않았기 때문에 PHP는 매번 새로운 세션을 생성하고 있었기 때문에 세션 변수가 보이지 않았습니다.
설정을 사용 phpinfo()
하고 확인하십시오 session.*
.
정보가 쿠키에 저장되고 브라우저가 쿠키를 허용하지 않을 수 있습니다.
먼저 확인하고 결과를 다시 확인하십시오.
당신은 또한 print_r($_SESSION);
이 변수의 덤프를 가지고 내용을 볼 수 있습니다 ....
당신에 관해서 phpinfo()
는은 session.save_path
유효 하나? 웹 서버에이 디렉토리에 대한 쓰기 권한이 있습니까?
도움이 되었기를 바랍니다.
나는 다음과 같은 문제가 있었다
index.php
<?
session_start();
$_SESSION['a'] = 123;
header('location:index2.php');
?>
index2.php
<?
session_start();
echo $_SESSION['a'];
?>
The variable $_SESSION['a']
was not set correctly. Then I have changed the index.php
acordingly
<?
session_start();
$_SESSION['a'] = 123;
session_write_close();
header('location:index2.php');
?>
I dont know what this internally means, I just explain it to myself that the session variable change was not quick enough :)
Check to see if the session save path is writable by the web server.
Make sure you have cookies turned on.. (I forget when I turn them off to test something)
Use firefox with the firebug extension to see if the cookie is being set and transmitted back.
And on a unrelated note, start looking at php5, because php 4.4.9 is the last of the php4 series.
Check who the group and owner are of the folder where the script runs. If the group id or user id are wrong, for example, set to root, it will cause sessions to not be saved properly.
Check the value of "views" when before you increment it. If, for some bizarre reason, it's getting set to a string, then when you add 1 to it, it'll always return 1.
if (isset($_SESSION['views'])) {
if (!is_numeric($_SESSION['views'])) {
echo "CRAP!";
}
++$_SESSION['views'];
} else {
$_SESSION['views'] = 1;
}
Well, we can eliminate code error because I tested the code on my own server (PHP 5).
Here's what to check for:
Are you calling session_unset() or session_destroy() anywhere? These functions will delete the session data immediately. If I put these at the end of my script, it begins behaving exactly like you describe.
Does it act the same in all browsers? If it works on one browser and not another, you may have a configuration problem on the nonfunctioning browser (i.e. you turned off cookies and forgot to turn them on, or are blocking cookies by mistake).
Is the session folder writable? You can't test this with is_writable(), so you'll need to go to the folder (from phpinfo() it looks like /var/php_sessions) and make sure sessions are actually getting created.
If you set a session in php5, then try to read it on a php4 page, it might not look in the correct place! Make the pages the same php version or set the session_path.
I spent ages looking for the answer for a similar problem. It wasn't an issue with the code or the setup, as a very similar code worked perfectly in another .php on the same server. Turned out the problem was caused by a very large amount of data being saved into the session in this page. In one place we had a line like this:$_SESSION['full_list'] = $full_list
where $full_list
was an array of data loaded from the database; each row was an array of about 150 elements. When the code was initially written a couple of years ago, the DB only contained about 1000 rows, so the $full_list
contained about 100 elements, each being an array of about 20 elements. With time, the 20 elements turned into 150 and 1000 rows turned into 17000, so the code was storing close to 64 meg of data into the session. Apparently, with this amount of data being stored, it refused to store anything else. Once we changed the code to deal with data locally without saving it into the session, everything worked perfectly.
I know one solution I found (OSX with Apache 1 and just switched to PHP5) when I had a similar problem was that unsetting 1 specific key (ie unset($_SESSION['key']);) was causing it not to save. As soon as I didn't unset that key any more it saved. I have never seen this again, except on that server on another site, but then it was a different variable. Neither were anything special.
Thanks for this one Darryl. This helped me out. I was deleting a session variable, and for some reason it was keeping the session from committing. now i'm just setting it to null instead (which is fine for my app), and it works.
I know one solution I found (OSX with Apache 1 and just switched to PHP5) when I had a similar problem was that unsetting 1 specific key (ie unset($_SESSION['key']);) was causing it not to save. As soon as I didn't unset that key any more it saved. I have never seen this again, except on that server on another site, but then it was a different variable. Neither were anything special.
Here is one common problem I haven't seen addressed in the other comments: is your host running a cache of some sort? If they are automatically caching results in some fashion you would get this sort of behavior.
Just wanted to add a little note that this can also occur if you accidentally miss the session_start() statement on your pages.
I had session cookie path set to "//" instead of "/". Firebug is awesome. Hope it helps somebody.
I had this problem when using secure pages where I was coming from www.domain.com/auth.php that redirected to domain.com/destpage.php. I removed the www from the auth.php link and it worked. This threw me because everything worked otherwise; the session was not set when I arrived at the destination though.
A common issue often overlooked is also that there must be NO other code or extra spacing before the session_start() command.
I've had this issue before where I had a blank line before session_start() which caused it not to work properly.
Adding my solution:
Check if you access the correct domain. I was using www.mysite.com
to start the session, and tried to receive it from mysite.com
(without the www
).
I have solved this by adding a htaccess rewrite of all domains to www to be on the safe side/site.
Also check if you use http or https.
Edit your php.ini.
I think the value of session.gc_probability is 1, so set it to 0.
session.gc_probability=0
Check if you are using session_write_close(); anywhere, I was using this right after another session and then trying to write to the session again and it wasn't working.. so just comment that sh*t out
Another few things I had to do (I had same problem: no sesson retention after PHP upgrade to 5.4). You many not need these, depending on what your server's php.ini contains (check phpinfio());
session.use_trans_sid=0 ; Do not add session id to URI (osc does this)
session.use_cookies=0; ; ensure cookies are not used
session.use_only_cookies=0 ; ensure sessions are OK to use IMPORTANT
session.save_path=~/tmp/osc; ; Set to same as admin setting
session.auto_start = off; Tell PHP not to start sessions, osc code will do this
Basically, your php.ini should be set to no cookies, and session parameters must be consistent with what osc wants.
You may also need to change a few session code snippets in application_top.php - creating objects where none exist in the tep_session_is_registered(...) calls (e eg. navigation object), set $HTTP_ variables to the newer $_SERVER ones and a few other isset tests for empty objects (google for info). I ended up being able to use the original sessions.php files (includes/classes and includes/functions) with a slightly modified application_top.php to get things going again. The php.ini settings were the main problem, but this of course depends on what your server company has installed as the defaults.
ReferenceURL : https://stackoverflow.com/questions/155920/php-session-data-not-being-saved
'program story' 카테고리의 다른 글
Selenium을 사용하여 인증서를 처리하는 방법은 무엇입니까? (0) | 2020.12.30 |
---|---|
Django REST Framework 사용자 정의 필드 유효성 검사 (0) | 2020.12.30 |
패턴 매칭 대 스위치 설명 (0) | 2020.12.30 |
불변성이란 무엇이며 왜 걱정해야합니까? (0) | 2020.12.30 |
GitHub 저장소 백업 (0) | 2020.12.30 |