Specific tips for using headers in PHP_PHP Tutorial
We cannot have spaces between 1, location and the ":" sign, otherwise an error will occur.
2. There cannot be any output before using the header.
3. The PHP code after using header in PHP will still be executed.
The following is a comparison with response.redirect in ASP:
Example 1:
response.redirect "../test.asp"
header("location:../test.php ");
The difference between the two:
ASP's redirect function can work after sending the header file to the client.
Such as
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></span></span></font></strong><span> </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>%response.redirect "../test.asp"%</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
Check if the following code in header is used in PHP, an error will be reported:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></span></span></font></strong><span> </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span> </li> <li class="alt"> <span></span><span class="tag-name"><strong><font color="#006699">header</font></strong></span><span>("location:../test.php"); </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
It can only be like this:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span></span></li> <li class=""> <span></span><span class="tag-name"><strong><font color="#006699">header</font></strong></span><span>("location:../test.php"); </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></span></span></font></strong><span>...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
That is, no data can be sent to the client before using the header function in PHP.
Example 2:
asp
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></span></span></font></strong><span> </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>% </span> </li> <li class="alt"><span>response.redirect "../a.asp" </span></li> <li class=""><span>response.redirect "../b.asp" </span></li> <li class="alt"> <span>%</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
The result is to redirect the a.asp file.
What about php?
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span></span></li> <li class=""> <span></span><span class="tag-name"><strong><font color="#006699">header</font></strong></span><span>("location:../a.php"); </span> </li> <li class="alt"><span>header("location:../b.php"); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></span></span></span></font></strong><span> </span> </li> </ol>
We found that it redirects b.php.
It turns out that redirect is performed in asp The following code will not be executed.
After executing the header, php will continue to execute the following code.
In this regard, the header redirection in php is not as good as the redirection in asp. Sometimes we have to redirect After that, the following code cannot be executed:
Generally we use >
But we can simply use the following method:
<ol class="dp-xml"> <li class="alt"><span><span>if(...) </span></span></li> <li class=""><span>header("..."); </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>... </span></li> <li class=""><span>} </span></li> </ol>One thing to note when using headers in PHP is that problems will also occur if you use Unicode (UTF-8) encoding, and you need to adjust the cache settings.
<ol class="dp-xml"> <li class="alt"><span><span>if(...) </span></span></li> <li class=""><span>{ header("...");exit();} </span></li> </ol>
http://www.bkjia.com/PHPjc/446416.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446416.html
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span>[</span><span class="attribute"><font color="#ff0000">email</font></span><span>=%@]%@</span><span class="attribute"><font color="#ff0000">LANGUAGE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"VBSCRIPT[/email]"</font></span><span> </span><span class="attribute"><font color="#ff0000">CODEPAGE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"936"</font></span><span>%</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>%if Request.ServerVariables("SERVER_NAME")="s.jb51.net" then </span> </li> <li class="alt"><span>response.redirect "news/index.htm" </span></li> <li class=""> <span>else%</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>%end if%</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">script</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span>var </span><span class="attribute"><font color="#ff0000">url</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">location</font></span><span>.href; </span> </li> <li class=""> <span>if(url.indexOf('http://www.jb51.net/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/index/index.htm'</font></span><span>; </span> </li> <li class="alt"> <span>if(url.indexOf('http://www.kanshule.com/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/index1/index.htm'</font></span><span>; </span> </li> <li class=""> <span>if(url.indexOf('http://www.shouji17.com/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/cn/index.asp'</font></span><span>; </span> </li> <li class="alt"> <span>if(url.indexOf('http://www.baidu.com/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/cn/index.asp'</font></span><span>; </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">script</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
TechArticle
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 header in PHP will still be executed. Below is...

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
