简体   繁体   English

PHP Header()函数未重定向

[英]PHP Header() function not redirecting

I've got a problem with the header function 我的标头功能有问题

if ( ! isset($_SESSION['user']) ) {

   header('Location: login.php');

}

There is no echo before the if, the page is empty! if之前没有回显,页面为空!

it doesn't redirect me. 它不会重定向我。 I've var_dump(); 我有var_dump(); 'ed something in the If statement and it worked so the if is correct. 在If语句中添加了某些内容,因此该操作正确了。

The php version is 5.6 的PHP版本是5.6

You might have outputted something to the browser before header call. 在调用标头之前,您可能已经向浏览器输出了一些内容。 First check: 首先检查:

  • is there any html before php opening tag 在php开头标记之前是否有任何html

  • is there any whitespace before php opening tag php开头标记前是否有空格

  • is another php script included with echo before that script 是该脚本之前echo中包含的另一个php脚本

  • is the coding utf8 with BOM? BOM编码的utf8是什么?

Those are possible reasons of your error. 这些可能是导致您出错的原因。

Try this code :- 试试这个代码:-

<?php
if ( ! isset($_SESSION['user']) ) {
?>
<script>
    window.location = 'login.php';
</script>
<?php
}
?>

您还可以执行以下操作:

echo "<script>window.location = 'login.php';</script>";

white space may cause the redirect change it to something like 空白可能会导致重定向将其更改为类似

if ( ! isset($_SESSION['user']) ) {

   header('location:login.php');

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM