Tips for using headers in PHP _php tutorials

Source: Internet
Author: User
We are1, location and ":" No space between, otherwise it will be wrong.

2, before using the header can not have any output.

3, PHP code after using the header will also be executed.

Here is a comparison with the redirect Response.Redirect in asp:

Example 1:

Response.Redirect ". /test.asp "

Header ("Location:.. /test.php ");

The difference between the two:

The redirect function of an ASP can work after sending a header file to the customer.

Such as

 
  
  
  1. < html > < head > !-- head > < body >
  2. < % Response.Redirect ". /test.asp "% ;
  3. body > span class= "tag" >!-- html > strong>

Check is in PHP with the header in the next example code will be error:

 
 
  1. <html><head> head > < Body >
  2. Header ("Location:.. /test.php ");
  3. ?>
  4. body> html>

This is the only way:

 
 
  1. Header ("Location:.. /test.php ");
  2. ?>
  3. < HTML > < Head > head><body> ... body> html>

That is, PHP cannot send any data to the client before using the header function.

Example 2:

in ASP

 
 
  1. <html><head> Head ><body>
  2. < %
  3. Response.Redirect ". /a.asp "
  4. Response.Redirect ". /b.asp "
  5. % >
  6. body> html>

The result is a redirected a.asp file.

What about PHP?

 
 
  1. Header ("Location:.. /a.php ");
  2. Header ("Location:.. /b.php ");
  3. ?>
  4. <html><head> head > < Body > body> html>

We found it redirected b.php.

The subsequent code is no longer executed after executing redirect in ASP.

PHP continues to execute the following code after the header is executed.

In this regard, header redirection in PHP is not as good as redirection in ASP. Sometimes after we redirect, we cannot execute the following code:

Generally, we use

 
  
  
  1. Header ("...");
  2. Else
  3. {
  4. ...
  5. }

But we can simply use the following method:

 
  
  
  1. {Header ("..."); exit ();}

It is also important to note in PHP that if you are coding with Unicode (UTF-8), you will need to adjust your cache settings.

 
 
  1. < [email=%@]%@ LANGUAGE = "Vbscript[/email]" CODEPAGE="936"%>
  2. < %if request.servervariables ("server_name") = "S.jb51.net" Then
  3. Response.Redirect "News/index.htm"
  4. else% >
  5. < %end if% >
  6. <script>
  7. var URL = Location . href;
  8. if (url.indexof (' http://www.jb51.net/')!=-1) Location.href = '/index/index.htm ' ;
  9. if (url.indexof (' http://www.kanshule.com/')!=-1) Location.href = '/index1/index.htm ' ;
  10. if (url.indexof (' http://www.shouji17.com/')!=-1) Location.href = '/cn/index.asp ' ;
  11. if (url.indexof (' http://www.baidu.com/')!=-1) Location.href = '/cn/index.asp ' ;
  12. script>

http://www.bkjia.com/phpjc/446416. HTML www.bkjia.com true http://www.bkjia.com/phpjc/446416.html techarticle We cannot have spaces between 1, location and ":", Otherwise, there will be an error. 2, before using the header can not have any output. 3, PHP code after using the header will also be executed. Here is the ...

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