Why does an error occur when php uses $ _ SESSION? Solution

Source: Internet
Author: User
Why does an error occur when php uses $ _ SESSION? Session1.php: & lt ;? Phpsession1.php & nbsp; session_start (); & nbsp; echo & quot; welcome: page 1 & lt; br & gt; & quot; & nbsp; $ _ SESSION [myword] & quot; I am zhang san & quot; & Why does an error occur when I use $ _ SESSION in nb php?
// Session1.php:
// Session1.php
Session_start ();
Echo "welcome: page 1
";
$ _ SESSION ['myword'] = "I am zhang san ";
$ _ SESSION ["myage"] = "18 ";
$ _ SESSION ["mysex"] = "male ";
Echo "the value of myword in Cookie is:". $ _ SESSION ['myword']."
";
Echo "next page ";
?>

// Session2.php
// Session2.php
Session_start ();
Echo "welcome: page 2
";
Echo "name:". $ _ SESSION ['myword']."
";
Echo "age:". $ _ SESSION ["myage"]."
";
Echo "gender:". $ _ SESSION ["mysex"]."
";
Echo "next page ";
?>
Open session1.php and output
------------------------------------
Welcome: page 1
The myword value in the Cookie is: I am Michael
Next page
------------------------------------

Click "next page" to open session2.php output

------------------------------------
Welcome: page 2

Notice: Undefined index: myword in R: \ Program Files \ AppServ \ www \ session \ session2.php on line 5
Name:

Notice: Undefined index: myage in R: \ Program Files \ AppServ \ www \ session \ session2.php on line 6
Age:

Notice: Undefined index: mysex in R: \ Program Files \ AppServ \ www \ session \ session2.php on line 7
Gender:
Next page
------------------------------------
Is it a configuration problem? The session configuration of my php. ini is as follows:

Session. save_handler = files

Session. save_path = "R:/Program Files/AppServ/php5/session_tmp"

; Session. save_path = "N; MODE;/path"

Session. save_path = "R:/Program Files/AppServ/php5/session_tmp"

(There are three sessions. save_path)

Session. cookie_domain = Cookies

(The others are not listed one by one)

------ Solution --------------------
Session. cookie_domain =
Modify it to this way or comment it out, and then restart the server.
I guess this is caused by not reading the cookie PHPSESSID. you can use print_r ($ _ COOKIE) to check whether PHPSESSID exists.
------ Solution --------------------
For details, see # 1st floor. do not set session. cookie_domain in disorder. how to set your own search? what do you do locally test the binding domain name.
------ Solution --------------------
Session issue summary:

For the PHP session function, there is always no suitable answer, especially some errors, and there are some no error results. The most terrible thing is the latter, which 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:/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 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.

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.