Use PHPheader to troubleshoot page jumps _ PHP Tutorial

Source: Internet
Author: User
Use PHPheader to troubleshoot page jumps. We usually use header (location: test. php) for jump, pay attention to the following points, which helps solve some problems that beginners often encounter. 1. l we usually use

When using header ("location: test. PHP") in php for redirection, pay attention to the following points, which helps solve some problems frequently encountered by new users.

1. 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 the header is 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 php reports an error:

 
 
  1. <Html>
  2. Header ("location: ../test. php ");
  3. ?>
  4. </Body>
  5. You can only do this:
  6. <?
  7. Header ("location: ../test. php ");
  8. ?>
  9. <Html>

That is, the PHP header cannot send any data to the customer.
Example 2:

Asp

 
 
  1. <Html>
  2. <%
  3. Response. redirect ".../a. asp"
  4. Response. redirect ".../B. asp"
  5. %>
  6. </Body>
  7. The result is to redirect the. asp file.
  8. What about php?
  9. <?
  10. Header ("location: ../a. php ");
  11. Header ("location: ../B. php ");
  12. ?>
  13. <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 PHP header method:

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

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

The above are some notes when using PHP headers.


When using the header (location: test. PHP) in php for redirect, pay attention to the following points, which helps solve some problems that beginners often encounter. 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.