Session highlights Solutions

Source: Internet
Author: User
Tags php session

Warning cannot send session cookie-headers already sent... Problem Solving (problem caused by UTF-8 BOM of PHP)

I am used to using edit plus for PHP programming, so sometimes some unknown errors may occur, which is very troublesome;
Recently, the following problems always occur on some pages during project development:

Warning: session_start () [function. session-start]: cannot send session cookie-headers already sent by (output started at E:/web/apache2/htdocs/index. PHP: 1) in E:/web/apache2/htdocs/functions/sessions. PHP on line 67

Warning: session_start () [function. session-start]: cannot send session cache limiter-headers already sent (output started at E:/web/apache2/htdocs/index. PHP: 1) in E:/web/apache2/htdocs/functions/sessions. PHP on line 67
After detailed search, the following causes are obtained:
In my edit plus settings, the default encoding is UTF-8, And the utf_8 signature is: always add a signature;
Therefore, try the following operations:
In the edit plus tool> parameter> File> utf_8 signature, change the option "always add signature" to "always remove signature" and open index. PHP file, save it as a new one, and re-run the script, and finally it will work properly;

In addition, I found two useful articles on the Internet. I hope someone can solve this problem perfectly!

A UTF-8 BOM caused by PHP strange problem I,

// --- A. php
<? PHP
Header ("Content-Type: image/BMP ");
Session_start ();
................
?>
Save a. php as UTF-8. the following error occurs when you access the PHP file in a browser:
Warning: session_start () [function. Session-start]: cannot send session cache limiter-headers already sent (output started

At ×××. php: 1) In ××× on line 2

This problem is common, most of which are output before session_start! For laruence, this error basically does not happen, but if you use DW or editplus

This error may occur even if you are a programmer writing code!

As shown in the preceding figure, there are no output statements in line 2 of the XXX file and line 2 of the XXX file, it's strange. Why?
Originally:

Unicode signature (BOM) can include the byte sequence mark (BOM) in the document ). Bom is two to four bytes at the beginning of a text file. It can be identified as Unicode.

The byte sequence of the subsequent bytes. Because the UTF-8 does not have a byte order, you can choose to add a UTF-8 Bom. This is required for UTF-16 and UTF-32.
No! If this option is selected, 2 to 4 bytes will be output at the beginning of the page!

Session_start () requires no output to the client browser before.

II,

Another error may occur, for example:
/-- A. php --
?>
Empty row
Empty row

This problem also occurs when you include a. php files. We recommend that you do not include the files that are often included at the end?>

Another example:
No output is allowed before session_start () is called. For example, the following is an error.
Incorrect.
========================================================== =
1 line
2 rows <? PHP
3 rows session_start (); // output already exists in the first row
4 rows .....
5 rows?>
========================================================== =

It is indeed so strange that it has been tested.
III,

Session_start ()
Set_cookie ()
Header ()
The preceding @ should be used to suppress this warning.

IV,

In the editplus Editor, If you first convert the. php file of UTF-8 to gb2312 or another file, and then convert it to UTF-8, the file can be accessed successfully, that is

Bom is removed, at this time the UTF-8 is no Bom type

Php-session_start () error caused by UTF-8 encoding

14:55:01

Large, medium, and small
The default gb2312 encoding is compatible with ANSI encoding. the file header does not have any additional information. In this case, session_start () can work properly.
When UTF Encoding is used, most editors will append a BOM block to the file header. My editplus appends FF Fe and uses a hexadecimal editor.
You can see clearly. In this way, when session_start () is called, two bytes are actually output to the browser, except that the hidden characters are
The following warning is displayed in the browser:
Warning: session_start () [function. session-start]: cannot send session cookie-headers already sent by (output started ......................

Solution:
1. Manually remove the BOM block. You can edit it in a hexadecimal Editor, such as ultraedit, or use the built-in functions of the editor. A good editor generally provides the option of removing the BOM block.
2. Write a script to correct it. For different editors, the BOM header is defined as follows:
UTF-8 EF BB BF
UTF-16 big endian Fe FF
Little endian FF Fe UTF-16
UTF-32 big endian 00 00 Fe FF
UTF-32 little endian FF Fe 00

Solution

During the PHP session operation, if the following error occurs: Warning: cannot send session cookie-headers already sent... "or" cannot add header information-headers already sent ..."

You only need to set output_buffering in PHP. ini. The default value is no. Change this value to 1.

 

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:/689/PHP/sessiondata/sess_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 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.
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:
<?
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. 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, 4 );

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

9. Question 9: Can I access the session value in the way of $ 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:
<? PHP
Session_start ();
$ Username = 'stangly. Wrong ';
Session_register ('username ');

Echo $ http_session_vars ['username'];
Echo '<br> ';
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, you must note that it cannot clear sessions in global or sessions that use session cookies. therefore, before using session_destroy, it is best not to use $ http_session_vars $ _ Session to access the session. (translated from php.net)

Routine:
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.