PHP's session function has been difficult for many beginners. Even some veteran, sometimes they are inexplicably. In this article, these questions will be a simple summary so that you can check it out.
1.
Error message
Warning: Cannot Send Session Cookie - Headers Already SENT
WARNING: Cannot Send Session Cache Limiter - Headers Already SENT
Analysis and solution
This type of problem is that you have previously have actual HTML content output in the program. Maybe you said, I didn't, I just a message of Echo or Print. Sorry, the output generated by your Echo or Print statement is the actual HTML content output. The way to solve this problem is to adjust your session_start () to the first line of the program.
2.
Error message
Warning: Open (f: / 689 / php / sessiondata / sess_66a39376b873f4daecf239891edc98b5, o_rdwr) failed
Analysis and solution
This error statement is generally because of your php.ini about session.save_path, the solution is set to set the session.save_path and session.cookie_path settings to
Session_save_path = c: / temp
Session.cookie_path = C: / Temp
Then build a TEMP directory in the C: / directory,
3.
Error message
Warning: Trying to Destroy Uninitialized session in
Analysis and solution
Out of this prompt, the general situation is caused by the SESSION_DESTROY () function. Many friends think that the session_destroy () function can run independently, is there. The solution is to open the session_start () before you adjust the session_destroy () function.
4. Question: How to get the ID value of the current session?
The easiest way is:
Echo SID;
You will find it.
5. Question: My program, there is no output before calling the Header function, although I include a config.php file, but there is no output in the config.php file, why SESSION is still reported to question 1 The same mistake, is it because I used session_start () before header?
A: Maybe you really checked your PHP program, and there is no output before reference to Header (), and there is no output in your include file! But do you use the cursor keys?> This PHP code ends the statement after moving check? Then you will find it?> This back, there is a blank line or several spaces, you delete these spaces or spaces, then the problem is solved.
Note: This issue, will be pHP4.1.2, higher, no test.
6. Q: After using the session to do the login master page, other pages are logged in with session restrictions. . .
A: The easiest way is
session_start ();
IF (! session_registered (/ 'login /') ││ $ login! = TRUE) {
echo / "You don't log in /";
EXIT;
}
7. Q: I registered with session_register (), but when I use Header or redirect statement with JavaScript, then in the page, I have access to the variable value registered by Session. How to solve it? Problem segment:
session_start ();
$ OK = / 'love you /';
Session_register (/ 'ok /');
HEADER (/ "location: next.php /");
?>
NEXT.PHP
session_start ();
Echo $ OK;
?>
the solution:
When you use the Header function or Window.location, you will be easier to lose, and there is still no detailed answer to this problem.
However, there is a solution. As follows
Header (/ "location: next.php /" ./"/ ". sID);
When jumping to the next page, the current ID of the session is used as a parameter, and it is passed to the latter page.
8. How to send anger group
Session_register (/ 'data /');
$ DATA = Array (1, 2, 3, 4);
The method is to assign a value after registration.
9. Question 9: Can I access the session value like $ http_get_vars [/ ']?
Answer: Yes, you can use the following Global array to access the session to enhance the security of the web page.
$ Http_session_vars
$ _SESSION
Routine:
session_start ();
$ usrname = /'Stangly.wrong/ ';
Session_register (/ 'username /');
Echo $ http_session_vars [/ 'username /'];
ECHO / '
/ ';
Echo $ _SESSION [/ 'UserName /'];
?>
Please refer to this routine to modify the programs that meet your own.
Question 10: What is the difference between session_unregister () and session_destroy ()?
The main role of the session_unregister () function is to cancel the current session variable. However, pay attention is that if you use $ http_session_vars or $ _SESSION to reference the session variable in the current page, you may need to cooperate with the unset () to cancel the session variable.
SESSION_DESTROY () is to clear the current Session environment. It means that when you use the session_destroy () function, then you can't use session_isregistered () to detect the variable of the session. But what you need to pay attention is that he cannot clear the session in Global or use the session in Session cookie. So before using session_destroy, it is best not to use $ http_session_vars $ _Session to access session. (Translated from php.net)
Routine:
IF (isset ($ _ cookie [session_name ()])) {
session_start ();
session_destroy ();
UNSET ($ _ cookie [session_name ()]);
}