Highlights of common PHP session Problems and Solutions

Source: Internet
Author: User
Tags php session php print

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: temp

Session. cookie_path = C: temp

Create a temp directory under the C: directory.

 

3.

Error Message

Warning: trying to destroy uninitializedsession 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.

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:

<? PHP

Session_start ();

OK = 'Love you ';

Session_register ('OK ');

Header ("Location: Next. php ");

?>

 

Next. php

<? 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, 3, 4 );

 

The method is to first register and then assign a value.

 

9. Question 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:

Code: <? PHP

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?

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 should be noted that he cannot clear the global session or the session that uses the session cookie. Therefore, before using session_destroy, it is best not to use H

Reference: http://blog.sina.com.cn/s/blog_54e38bdc0100i9yk.html

Ajax verification code: PHP + Ajax session verification code is inconsistent with the generated one!

Http://zhidao.baidu.com/question/233920191.html as described on this page

Log on as an administrator in the form of PHP + verification code. The file that generates the verification code is YZ. PHP: The verification code is saved in the session. the problem is that the verification code generated on the front-end page is always later than that on the back-end. The last verification code is displayed after refresh. After debugging for a long time, the verification code defined by global cannot be called, please advise and attach the code: [omitted some irrelevant code, limited words, saving the Ajax verification function] logon page: login. php <? PHP session_start ();?> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Answer:

Because you have performed an asynchronous operation. 1. loading the verification code -- displaying the image -- saving it to Session2. loading the page -- Reading the session is not sequential. Therefore, you can only refresh the last 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.