php – 如何在没有标题重定向的情况下将用户发送到另一个URL?

前端之家收集整理的这篇文章主要介绍了php – 如何在没有标题重定向的情况下将用户发送到另一个URL?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在学习 PHP并且一直在寻找这个.我想要实现的是这样的:
if (true/false) {
    go to this url;
}

每次我搜索PHP重定向PHP链接等术语时,99%的时间我会得到一些“标题”.我读到头重定向可以实现这一点,但没有代码可以在它之前,它必须首先在页面上,否则它不会工作.

如果是这样,那么我该如何实现呢?

i read that header redirects can achieve this but no code can go before it. that it must be first on the page else it wont work.

那是错的.在此之前必须没有输出.因此,你必须确保,你之前没有回声,打印,?>某事<?PHP(或其他).

if (true)  {
  header('Location: ' . $url,false,302);
  exit; // Ensures,that there is no code _after_ the redirect executed
}

你可以准备好一切about it in the official manual.特别是:

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.

猜你在找的PHP相关文章