参见英文答案 > How do I make a redirect in PHP? 31个
PHP可以在执行函数后进行重定向调用吗?我正在创建一个函数,我希望它能够重定向到位于同一根文件夹中的文件.可以吗?
if {
//i am using echo here
}
else if ($_SESSION['qnum'] > 10) {
session_destroy();
echo "Some error occured.";
//redirect to user.php
}
解决方法:
是的,您将使用header功能.
/* Redirect browser */
header("Location: http://www.yourwebsite.com/user.php");
exit();
在它之后调用exit()是一个好习惯,这样它下面的代码就不会被执行.
另外,从文档:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
这意味着你不应该在header()函数之前回显任何东西,因为这样做很可能会抛出错误.此外,您还需要验证此代码是否在任何其他输出之前运行.
标签:php,redirect,header
来源: https://codeday.me/bug/20190915/1805960.html