PHP exit () function related _php tutorial

Source: Internet
Author: User
Today encountered a problem, run the following code times wrong.

 
  

The error message is:

    Warning:session_start () [Function.session-start]: Cannot send session cache Limiter-headers already sent (output Starte D at D:\phpprojects\agilelite\index.php:1) in D:\PHPProjects\agilelite\index.php on line 2warning:cannot Modify header I Nformation-headers already sent by (output started at d:\phpprojects\agilelite\index.php:1) in D:\PHPProjects\agilelite \index.php on line 6

To solve this problem, consider the use of the Exit function.

Return, break, and contiue are language constructs, just like an if statement, but exit is a function. The function of the exit function is to output a message and terminate the current script.

If a text contains multiple scripts to end, exit exits the current script.

For example, a PHP text includes the code, the output is nowamagic.

 
  
 
  

The Exit function has the following syntax format (void means no return value):

void exit ([string $status]) void exit (int $status)

If status is a string, this function prints the status before the script exits.

If the status is an integer, the integer will be used as the exit state. Exit status should be from 0 to 254, exit status 255 is reserved by PHP and is forbidden to use. Status 0 is used to indicate a successful termination program.

Use exit after header jump

The header of PHP can output HTTP header information, if not before the header output anything, because all the content as HTTP body output to the client, once the body content, it is impossible to add any head part of the information.

A common way of header functions is to do the steering, redirect.

For example, I need to go to an address, just execute the following code:

 
  

The browser will receive a 302 HTTP status code telling him that the content has been transferred.

The important thing is that PHP will continue to execute the following code after calling the header function, and you can experiment with my Code yourself:

 
  

Will certainly execute to the Fwrite function, open the Header.txt file, check the time!

The workaround is to add exit after each header function to ensure that the current page stops executing and then to the address specified by location.

To avoid exit everywhere, you can write a function that is specifically used for steering, such as:

 
  

In addition, in order to avoid output body content before the header, many PHP frameworks use the PHP page footer does not write?> way, because some people always used to add a line after?>, this is really a bad habit.

http://www.bkjia.com/PHPjc/752451.html www.bkjia.com true http://www.bkjia.com/PHPjc/752451.html techarticle today encountered a problem, run the following code times wrong. Phpsession_start (); if (!isset ($_session[' user ') | | null = = $_session[' user ']) {header (' Location:login.php '); Exit;}? > Error ...

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