_php tutorial on common problems in PHP session

Source: Internet
Author: User
Tags php session
PHP session function, has been difficult for many beginners. Even some veteran, sometimes confused. In this article, we will make a simple summary of these questions so that we can consult them.

1. Error message


Reference

The code is as follows Copy Code

Warning:cannot send session Cookie-headers already sent
Warning:cannot Send session cache Limiter-headers already sent

Analysis and Solutions
The reason for this type of problem is that when you use Session_Start () in your program, you already have the actual HTML content output. Maybe you said, I'm not, I'm just echo or print a message. Unfortunately, the output generated by your echo or print statement is the actual output of the HTML content. The solution to this type of problem is to transfer your session_start () to the first line of the program.

2. Error message


Reference

The code is as follows Copy Code

Warning:open (F:/689\PHP\SESSIONDATA\SESS_66A39376B873F4DAECF239891EDC98B5, O_RDWR) failed

Analysis and Solving methods
This error statement usually occurs because your php.ini is not set up for Session.save_path, and the workaround is to set the Session.save_path and Session.cookie_path settings to


Reference

The code is as follows Copy Code

Session_save_path = C:\Temp
Session.cookie_path = C:\Temp

Then create a temp directory under the C \ directory to

3. Error message


Reference

The code is as follows Copy Code

Warning:trying to destroy uninitialized session in

Analysis and Solving methods
Out of class such a hint, the general situation is you directly adjust the Session_destroy () function caused. Many friends think that the Session_destroy () function can run independently, but it is not. The solution is to use Session_Start () to open the session function before you Session_destroy () function.

4. Question: How do I get the ID value of the current session?
The simplest way is to:
Echo SID;
You're going to find out.

5. Problem: My program does not have any output before calling the header function, Although I include a config.php file, but there is no output in the config.php file, why does the session still report the same error as problem 1, is it because I used the session_start before the header () The reason for it?

A: Perhaps you really carefully check your PHP program, before the reference header () does not have any output, and in your include file does not have any output! But do you use the cursor key in? > This PHP code concluding sentence after the move check? So you're going to find out? > After this, there is a blank line or a few spaces, you delete the empty lines or spaces, then the problem is resolved.
Note: This issue will appear in PHP4.1.2, later versions, not tested.

6. Q: After using the session to log on the main page, other pages how to use the session limit login
Answer: The simplest way is to

The code is as follows Copy Code
Session_Start ();
if (!session_registered (' login ') ││ $login! = True) {
echo "You did not log in";
Exit
}
Session_Start ();
if (!session_registered (' login ') ││ $login! = True) {
echo "You did not log in";
Exit
}


7. Question: I registered the session variable with Session_register (), but when I use the header or the Javs script redirect, on the page I do not have access to the variable value registered by the session. How can I resolve this?


The program fragment of the problem:

The code is as follows Copy Code
Session_Start ();
$ok = ' Love you ';
Session_register (' OK ');
Header ("location:next.php");
? >
Session_Start ();
$ok = ' Love you ';
Session_register (' OK ');
Header ("location:next.php");
? >


next.php

The code is as follows Copy Code

Session_Start ();
Echo $ok;
? >
Session_Start ();
Echo $ok;
? >


The workaround:
When you use the header function or the Window.location function, the session variable that you registered on the previous page is easily lost, and there is still no detailed answer to the reason for this problem.
But there is a way out. As shown below

The code is as follows Copy Code


Header ("Location:next.php". "?". SID);
Header ("Location:next.php". "?". SID);


In the jump to the next page, the session's current ID as a parameter, upload to the latter page.

8.session How to pass an array

The code is as follows Copy Code

Session_register (' data ');
$data =array (1,2,3,4);
Session_register (' data ');
$data =array (1,2,3,4);


The method is to register and assign the value first

9. Question: Can I access session values in a way like $http_get_vars[' * * '?

Answer: Yes, you can use the following global array to visit the session to enhance the security of the Web page
$HTTP _session_vars
$_session
Routines:

The code is as follows Copy Code

Session_Start ();
$username = ' Stangly.wrong ';
Session_register (' username ');

echo $HTTP _session_vars[' username '];
Echo '
';
echo $_session[' username '];
? >
Session_Start ();
$username = ' Stangly.wrong ';
Session_register (' username ');

echo $HTTP _session_vars[' username '];
Echo '
';
echo $_session[' username '];
? >


Please refer to this routine to modify the program that matches your own.

What is the difference between the question 10:session_unregister () and the Session_destroy ()?
The main function of the Session_unregister () function is to eliminate the current session variable. Note, however, that if you use $http_session_vars or $_session to refer to the session variable in the current page, you may need to match the session variable with the unset ().
The Session_destroy () clears the current session environment. This means that when you use the Session_destroy () function, you can no longer use session_is_registered () to detect the variables in the session. However, it is important to note that he cannot clear the session in global or the session using the session cookie. So before using Session_destroy, it's best not to use $http_session_vars $_ Session to access the session.

Routines:

The code is as follows Copy Code
if (Isset ($_cookie[session_name ())) {
Session_Start ();
Session_destroy ();
Unset ($_cookie[session_name ());
}

http://www.bkjia.com/PHPjc/632221.html www.bkjia.com true http://www.bkjia.com/PHPjc/632221.html techarticle PHP session function, has been difficult for many beginners. Even some veteran, sometimes confused. In this article, we will make a simple summary of these questions, so that everyone can check ...

  • 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.