Use PHP header for page jump problems troubleshoot _php tutorial

Source: Internet
Author: User
When we go on page jumps, we usually use

In PHP with the header ("location:test.php") to jump to pay attention to the following points, to help solve some of the problems novice often encountered

1, location and ":" No space between, otherwise it will be wrong.
2, before using the header can not have any output.
3. The PHP code after 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 > </ HTML >

Check that the following code in PHP will report an error:

 
 
  1. < html> < head>< /head > < body >
  2. Header ("Location:.. /test.php ");
  3. ?>
  4. < /body > < /html >
  5. This is the only way:
  6. < ?
  7. Header ("Location:.. /test.php ");
  8. ?>
  9. < html> < head>< / Head>< body>... < /body > < /html >

It is not possible to send any data to the client before the PHP header.
Example 2:

in ASP

 
 
  1. < html> < head>< /head > < body >
  2. < %
  3. Response.Redirect ". /a.asp "
  4. Response.Redirect ". /b.asp "
  5. % >
  6. < /body > < /html >
  7. The result is a redirected a.asp file.
  8. What about PHP?
  9. < ?
  10. Header ("Location:.. /a.php ");
  11. Header ("Location:.. /b.php ");
  12. ?>
  13. < 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. if (...)
  2. Header ("...");
  3. Else
  4. {
  5. ...
  6. }

But we can simply use the following PHP header method:

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

Also note that if there is a problem with Unicode (UTF-8) encoding, you need to adjust the cache settings for the PHP header.

 
 
  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 >

These are some of the issues that you should be aware of when using PHP headers.


http://www.bkjia.com/PHPjc/446162.html www.bkjia.com true http://www.bkjia.com/PHPjc/446162.html techarticle when we go to the page, we usually use the header (location:test.php) to jump in PHP to pay attention to the following points, to help solve some of the problems novice often encountered 1, l ...

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