我正在学习
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.