Session errors in php

Source: Internet
Author: User
Session in php often has some strange problems. Many of these problems are minor details. it may be difficult for php beginners to find out the cause, today, we have collected almost all session problems

Session in php often has some strange problems. Many of these problems are minor details. it may be difficult for php beginners to find out the cause, today, we have collected all the problems or problems caused by Sessions and provided solutions one by one.

1> error message

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

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

Set session. save_path and session. cookie_path

Session_save_path = c: temp

Create a temp directory under the c: Directory.

3> error message

Error message: Warning: Trying to destroy uninitialized session in

This is generally because you directly call the session_destroy () function. many friends think that the session_destroy () function can be run independently, 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 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 to other pages?

Solution:

  1. Session_start ();
  2. If (! Session_registered ('login') │ login! = True ){
  3. Echo "you have not logged on ";
  4. Exit;
  5. }

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:

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.