Session errors in php highlights and solutions

Source: Internet
Author: User
Tags session id php file php code


1> error message

The code is as follows: Copy code
Warning: Cannot send session cookie-headers already sent
Warning: Cannot send session cache limiter-headers already sent

The cause is that when session_start () is used in the program, actual html content is output. Even if a message is sent by echo or print, the output produced by the echo or print statement is the actual html content output. To solve this problem, set session_start () to the first line of the program.
2> error message

The code is as follows: Copy code
Warning: open (F:/webphpsessiondatasess_76666aecf239891edc98b5, O_RDWR) failed

This error occurs generally because the session. save_path item in your php. ini is not set. The solution is

The code is as follows: Copy code
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>

The code is as follows: Copy code
Error message: Warning: Trying to destroy uninitialized session in

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> How to obtain the id of the current session?
The simplest method is:

The code is as follows: Copy code
Echo SID;

5> there is 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?
After carefully checking your php program, there is indeed 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: This problem occurs in PHP4.1.2.
6> after using session to log on to the home page, how can I use session to limit logon on other pages...
Solution:

The code is as follows: Copy code
Session_start ();
If (! Session_registered ('login') │ login! = True ){
Echo "you have not logged on ";
Exit;
}

7> The session variable is registered with session_register (). However, when the header or javascript redirection statement is used, the variable value registered by the session cannot be accessed on the following page.
Program segment of the problem:

The code is as follows: Copy code
<?
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. location function, the session variables registered on the previous page will be easily lost.
Solution.

The code is as follows: Copy code
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

The code is as follows: Copy code
Session_register ('data ';);
Data = array (1, 2, 3, 4 );

The method is to first register and then assign a value.
9> can I access the session value in a way like 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:

The code is as follows: Copy code
<? Php
Session_start ();
$ Username = '; stangly. wrong ';;
Session_register ('; username ';);
Echo $ HTTP_SESSION_VARS ['username'];
Echo '; <br> ';;
Echo _ SESSION ['username'];
?>
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, it must be noted that the global session cannot be cleared or the session in which the session cookie is used. Therefore, it is recommended that you do not use HTTP_SESSION_VARS _ SESSION to access the session before using session_destroy.
Routine:

The code is as follows: Copy code
If (isset (_ COOKIE [session_name ()]) {
Session_start ();
Session_destroy ();
Unset (_ COOKIE [session_name ()]);
}

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.