Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP条件-Javascript重定向_Php_Javascript - Fatal编程技术网

PHP条件-Javascript重定向

PHP条件-Javascript重定向,php,javascript,Php,Javascript,我有一个PHP的“if”条件,如果为true,我希望使用javascript将用户重定向到其他地方。然而,它似乎不起作用,因为它总是重定向我,无论条件是否正确 <?php if (isset($_SESSION['lol'])) { ?><script type="text/javascript"> <!-- window.location = "http://www.test.org" //--> </script><?php }

我有一个PHP的“if”条件,如果为true,我希望使用javascript将用户重定向到其他地方。然而,它似乎不起作用,因为它总是重定向我,无论条件是否正确

<?php
if (isset($_SESSION['lol'])) {
    ?><script type="text/javascript">
<!--
window.location = "http://www.test.org"
//-->
</script><?php
}
?>


怎么可能呢?或者我如何才能让它工作?

只需在PHP中重定向即可:

<?
    if (isset($_SESSION['lol']) {
        header("Location: test.org");
    }
?>

无论Javascript方法看起来多么简单,您都应该在PHP中执行重定向:

<?php
    header('Location: http://test.org');
    exit;
?>

首先,可能存在javascript不工作的情况(可能是故意关闭的,用户可能使用不兼容的浏览器,用户可能安装了NoScript等浏览器插件)


此外,您应该在重定向后停止代码执行(这就是为什么在我的示例中存在
exit
),这样恶意访问者就不能忽略重定向而只是读取页面内容。

您有一个语法错误(isset($\u SESSION['lol'])-缺少一个结束括号为什么使用JS重定向而不使用
头(“地点:http://www.test.org”;
抱歉,这只是一个打字错误。请编辑。另外,为什么不执行php标题重定向?
标题(“位置:http://www.test.org“
@Jon我想获取HTTP_头信息,以包含我从何处重定向的信息。正如我看到的头(”)没有给我标题信息。我希望我的浏览器将HTTP_标题传递到登录页。(test.org)我明白了。如果Javascript会这样做(我不确定),那么您可以使用我的第二段代码。
<?php
    header('Location: http://test.org');
    exit;
?>