PHP session highlights solutions

Source: Internet
Author: User
The session function of PHP has always been difficult for many beginners. Some veterans are sometimes confused. This article provides a brief summary of these issues for your reference. 1. error message: Warning: Cannotsendsessioncookie solution

The session function of PHP has always been difficult for many beginners. Some veterans are sometimes confused. This article provides a brief summary of these issues for your reference.

1.
Error message
Warning: Cannot send session cookie-headers already sent
Warning: Cannot send session cache limiter-headers already sent
Analysis and solutions
The reason for this problem is that when you use session_start () in the program, the actual html content is output. Maybe you said, I didn't. I just echo or print a message. Sorry, the output produced by your echo or print statement is the actual html content output. The solution to this problem is to tune 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 occurs generally because the session. save_path item in your php. ini is not set. the solution is to set session. save_path and session. cookie_path
Session_save_path = c: \ temp
Session. cookie_path = c: \ temp
Create a temp directory under the c: \ directory.

3.
Error message
Warning: Trying to destroy uninitialized session in
Analysis and solution
Generally, you directly call the session_destroy () function. Many friends think that the session_destroy () function can run independently, but it is not. The solution is to enable the session function with session_start () before you call the session_destroy () function.

4. question: how can I obtain the id of the current session?
The simplest method is:
Echo SID;
You will find.

5. problem: My program has no output before calling the header function, although I include a config. php file, but in config. there is no output in the PHP file. Why does the session still report the same error as Question 1? is it because I used session_start () before the header?
A: Maybe you have carefully checked your php program. there is no output before header () is referenced, and there is no output in your include file! But are you using the mouse key?> What about the mobile check after the conclusion of this PHP code? Then you will find in?> There is a blank line or space behind this. if you delete these blank lines or spaces, the problem is solved.
Note: If this problem occurs, php 4.1.2 is later than PHP4.1.2 and has not been tested.

6. Q: How can I use session to restrict logon to other pages after I use session to log on to the home page...
A: The simplest method is
Session_start ();
If (! Session_registered (\ 'login \ ') │ $ login! = True ){
Echo \ "you have not logged on \";
Exit;
}

7. q: I used session_register () to register the session variable. However, when I use the header or javascript redirection statement, on the following page, but I cannot access the variable value registered by the session. How can this problem be solved?
Program segment of the problem:
Session_start ();
$ OK = \ 'love you \';
Session_register (\ 'OK \');
Header (\ "location: next. php \");
?>

Next. php
Session_start ();
Echo $ OK;
?>

Solution:
When you use the header function or window. after the location function, the session variables registered on the previous page will be easily lost. there is still no detailed answer to this question.
But there are solutions. As shown below
Header (\ "Location: next. php \".\"? \ ". SID );
When you jump to the next page, use the current session id as a parameter and upload it to the next page.

8. how to pass an array in a session
Session_register (\ 'data \');
$ Data = array (1, 2, 4 );

The method is to first register and then assign a value.

9. Question 9: can I access the session value in the way of $ HTTP_GET_VARS?

A: Yes. you can use the following global array to access sessions to enhance web page security.
$ HTTP_SESSION_VARS
$ _ SESSION
Routine:
Session_start ();
$ Username = \ 'stangly. wrong \';
Session_register (\ 'username \');

Echo $ HTTP_SESSION_VARS [\ 'username \ '];
Echo \'
\';
Echo $ _ SESSION [\ 'username \ '];
?>
Refer to this routine to modify your own program.

Question 10: What is the difference between session_unregister () and session_destroy?
The session_unregister () function is used to describe the current session variable. However, if you use $ HTTP_SESSION_VARS or $ _ SESSION to reference the session variable on the current page, you may need to work with unset () to cancel the session variable.
Session_destroy () clears the current session environment. That is to say, when you use the session_destroy () function, you cannot use session_is_registered () to detect session variables. However, you must note that it cannot clear sessions in global or sessions that use session cookies. therefore, before using session_destroy, it is best not to use $ HTTP_SESSION_VARS $ _ SESSION to access the session. (translated from php.net)

Routine:
If (isset ($ _ COOKIE [session_name ()]) {
Session_start ();
Session_destroy ();
Unset ($ _ COOKIE [session_name ()]);
}


The above is a common problem for new users. The details may not be clear, and errors may inevitably occur. please give them some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.