Heim >Backend-Entwicklung >PHP-Tutorial >PHP函数header()查询单部件的具体方法解析_PHP教程
我们今天为大家讲解的具体内容时如何运用下面是一个叫做 calling.php 的脚本:
<ol class="dp-xml"> <li class="alt"><span><span>nbsp;HTML PUBLIC </span></span></li> <li class=""><span>"-//W3C//DTD HTML 4.0 Transitional//EN" </span></li> <li class="alt"> <span>"http://www.w3.org/TR/html4/loose.dtd" </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></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>Calling page example</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">a</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"action.php"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Click here!</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span></font></strong><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></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
当用户点击上面的连接时,PHP函数header()就去调用 action.php。下面是 action.php 的源码:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>// 数据库功能 </span></li> <li class="alt"><span> </span></li> <li class=""><span>// 重定向 </span></li> <li class="alt"><span>header("Location: $HTTP_REFERER"); </span></li> <li class=""><span>exit; </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
这里有两个常见的错误需要提醒一下:
调用PHP函数header()后要包含一个 exit 语句让脚本停止,否则后续的脚本可能会在头发送前输出。
header() 函数常见的一个错误是:
Warning: Cannot add header information - headers already sent...
PHP函数header()只能在 HTML 输出之前被调用,因此你需要检查 php 前面可能存在的空行,空格等等。