Tips for Using headers in PHP _ PHP Tutorial

Source: Internet
Author: User
Tips for Using headers in PHP. We cannot have spaces between 1, location, and:. Otherwise, an error will occur. 2. there cannot be any output before using the header. 3. the PHP code after using the header in PHP will also be executed. Below is1. there must be no space between location and:. Otherwise, an error will occur.

2. there cannot be any output before using the header.

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

The following is a comparison with response. redirect in asp:

Example 1:

Response. redirect ".../test. asp"

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

Differences:

The redirect function of asp can work after the header file is sent to the customer.

For example

 
 
  1. <html><head> head><body>
  2. <%response.redirect "../test.asp"%>
  3. body> html>

The following code in the header in PHP reports an error:

 
 
  1. <html><head> head><body>
  2. header("location:../test.php");
  3. ?>
  4. body> html>

You can only do this:

 
 
  1. header("location:../test.php");
  2. ?>
  3. <html><head> head><body>... body> html>

That is, no data can be sent to the customer before the header function is used in PHP.

Example 2:

Asp

 
 
  1. <html><head> head><body>
  2. <%
  3. response.redirect "../a.asp"
  4. response.redirect "../b.asp"
  5. %>
  6. body> html>

The result is to redirect the. 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 that it redirects B. php.

It turns out that after redirect is executed in asp, subsequent code will not be executed.

Php continues to execute the following code after executing the header.

In this regard, the header redirection in php is not as good as the redirection in asp. sometimes, after we want to redirect, we cannot execute the following code:

Generally, we use

 
 
  1. if(...)
  2. header("...");
  3. else
  4. {
  5. ...
  6. }

However, we can simply use the following method:

 
 
  1. if(...)
  2. { header("...");exit();}

In PHP, the header should also be noted that if Unicode (UTF-8) encoding will also occur, you need to adjust the 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>

When 1, there cannot be spaces between location and:; otherwise, an error will occur. 2. there cannot be any output before using the header. 3. the PHP code after using the header in PHP will also be executed. Below is...

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.