开发者

How to make a javascript redirect within a php if statement

开发者 https://www.devze.com 2023-01-09 11:22 出处:网络
I am developing a personality test, and php result script now just echoes a personality description based on an if statement:

I am developing a personality test, and php result script now just echoes a personality description based on an if statement:

if ($i = 5 AND $alph开发者_JAVA技巧a[$i] >= 9 ) {
    echo "  Description ";
}

However, instead of this description, I would want to make a redirect to another 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


If you're just looking to redirect from your PHP script, use HEADER (as others have suggested). The only thing to watch out for is that if you've already echo'd any text, HEADER will not work properly. In other words, you must call HEADER before you ECHO any HTML code or anything else. 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.

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


Use:

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

die('');

Die is important, otherwise PHP keeps executing and sends HTML too.

Edit: The URL should be an absolute URL. (though relative works for most of the browsers)


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


PHP is executed on the server, having client code (Javascript) wrapped in PHP to do a redirect like this isn't going to work.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消