简体   繁体   English

如何在php if语句中进行JavaScript重定向

[英]How to make a javascript redirect within a php if statement

I am developing a personality test, and php result script now just echoes a personality description based on an if statement: 我正在开发一个人格测试,并且php结果脚本现在仅基于if语句回显人格描述:

if ($i = 5 AND $alpha[$i] >= 9 ) {
    echo "  Description ";
}

However, instead of this description, I would want to make a redirect to another url. 但是,除了此描述之外,我还想重定向到另一个URL。 Can't seem to figure this one out. 似乎无法弄清楚这一点。 Cheers, Jelmar 干杯,耶尔玛

Use: 采用:

header("LOCATION: url/path"); exit();

Examples: 例子:

header("LOCATION: www.google.com"); exit();
header("LOCATION: mypage.php"); exit();

More Info: 更多信息:

http://php.net/manual/en/function.header.php http://php.net/manual/zh/function.header.php

If you're just looking to redirect from your PHP script, use HEADER (as others have suggested). 如果您只是想从PHP脚本重定向,请使用HEADER(如其他人所建议的那样)。 The only thing to watch out for is that if you've already echo'd any text, HEADER will not work properly. 唯一需要注意的是,如果您已经回显了任何文本,则HEADER将无法正常工作。 In other words, you must call HEADER before you ECHO any HTML code or anything else. 换句话说,在回显任何HTML代码或其他任何内容之前,必须调用HEADER。 If you have already sent text by the time you want to redirect in your code, you might want to use javascript at that time, by echo'ing the following code. 如果您想在代码中进行重定向时已经发送了文本,则可能需要通过回显以下代码来使用javascript。

<script type="text/javascript">
    <!--
        window.location = "http://www.google.com/"
    //-->
</script>

use header 使用标题

if ($i = 5 AND $alpha[$i] >= 9 ) {
    header('Location: http://www.example.com/');
    exit();
}

OR 要么

JS redirect: http://www.tizag.com/javascriptT/javascriptredirect.php JS重定向: http : //www.tizag.com/javascriptT/javascriptredirect.php

Use: 采用:

header("LOCATION: url-here/page here"); header(“ LOCATION:url-here / page here”);

die(''); 死('');

Die is important, otherwise PHP keeps executing and sends HTML too. 模具很重要,否则PHP会继续执行并发送HTML。

Edit: The URL should be an absolute URL. 编辑:URL应该是一个绝对URL。 (though relative works for most of the browsers) (尽管相对功能适用于大多数浏览器)

PHP is executed on the server, having client code (Javascript) wrapped in PHP to do a redirect like this isn't going to work. PHP是在服务器上执行的,将客户代码(Javascript)封装在PHP中以进行重定向,这样是行不通的。

Read up on http://php.net/manual/en/function.header.php http://php.net/manual/en/function.header.php上阅读

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

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