Some problems needing attention when PHP header function jumps

Source: Internet
Author: User
Tags php file php code

When you jump through the header of PHP (' location:http://www.111cn.net '). Sometimes it doesn't work. There are several reasons to follow:

Wrong spelling

location and ":" No spaces between the number, or there will be an error.

• You cannot have any output before using header ().

The PHP code after the header is also executed, so it is best to add exit () before the function to stop the execution of the program.

There is another problem, which is relatively low-level.


Coding an error

The code is as follows Copy Code

<?php
Header ("location:http://www.111cn.net/");
?>

There is no output before the header, or an error is expected.

When we encode in ANSI format (most of the default), PHP jumps can be implemented normally.

The code is as follows Copy Code
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

Sometimes we need to judge the jump conditions in advance in the Web page, if the match will normally display the Web page, and the page is encoded in the UTF-8 format, the header will be used at the error.
The reason for this is that the UTF-8 encoding secretly prints three characters (BOM) at the beginning of the text, and is invisible to us by default. Therefore, the "header does not have any output before" condition, thus the error.


The solution is to use a text editor such as notepad++ to open the file, to UTF-8 no BOM encoding format, save exit.

PHP Header default 302 temporary redirection, if you need to use 301 permanent redirection, you can use the following code.

The code is as follows Copy Code
<?php
Header ("http/1.1 moved Permanently");
Header ("location:http://feitsui.net/");
?>

If you are not concerned about HTTP status, you can use JavaScript to implement jumps.

The code is as follows Copy Code

<script type= "Text/javascript" >
Window.location.href= "http://www.111cn.net/";
</script>

Third, the order problem

The code is as follows Copy Code

<?php
if (1) {
Header ("location:http://www.baidu.com/");
}
Header ("location:01.php");
Header ("location:03.php");
Header ("location:http://www.sina.com");
echo "111";
Header ("location:http://www.g.cn");
echo "222";
Header ("location:http://www.163.cn");
echo "333";
?>

Why don't the code jump to Header ("location:http://www.baidu.com/")?

Another problem the server read PHP file, is to read a point sent to the client, or once read, before sending to the customer. How does JavaScript read the law?

PHP scripts are read from top to bottom, and then sent to the client after a one-time read on the server, which should have sent the header ("location:http://www.baidu.com/") when it was read to if; Location:01.php ");
Header ("location:03.php");
Header ("location:http://www.sina.com"); you'll continue to read the last one. The echo has a lower precedence than the header, and when many headers are tied, the last one is displayed, like an array, $forum [], When you write echo $forum only the last record in the array will be displayed. JavaScript is a client-side script, and when your page is downloaded, all of the JavaScript script files are already in your computer and are only responding when they are operated on.

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.