PHPsession_start () problem solving (details) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHPsession_start ). This article provides a brief summary of these issues for your reference. 1. the error message is "Warning: Cannotsendsessioncookie-headersalreadysentWarning: Cannotsendsession". in this article, we will summarize these questions 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 PHP 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:/689phpsessiondatasess_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: emp
Session. cookie_path = c: emp
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 in PHP 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 issue 1?Is it because I used PHP 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 empty 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?
Session_unregister () function is mainly used to describe the current sion. (translated from php.net)
Routine:
If (isset ($ _ COOKIE [session_name ()]) {
Session_start ();
Session_destroy ();
Unset ($ _ COOKIE [session_name ()]);
}
The above is a common PHP session_start () problem for beginners. The details may not be clear, and errors may inevitably occur. please give them some advice.

Bytes. 1. error message: Warning: Cannot send session cookie-headers already sent Warning: Cannot send session...

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.