tags:

views:

73

answers:

6

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 $alpha[$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

+2  A: 

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

Sarfraz
A: 

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

NAVEED
Yes, thats what I thought, but when i do this I get this error message:Warning: Cannot modify header information - headers already sent by (output started at /home/schoolvo/domains/schoolvoorontwikkeling.nl/public_html/newscore3.php:7) in /home/schoolvo/domains/schoolvoorontwikkeling.nl/public_html/newscore3.php on line 252
Jelmar
NAVEED
**Above link says:** There are many editors seem to add additional spaces and/or empty lines at the end of a file when you edit it. This so-called whitespace is then sent to the browser when the file is loaded. The fix is, obviously, to remove that whitespace from the file. Read the error message carefully. It says "output started at ..." followed by a file name and a line number. That is the file (and line) that you need to edit. Ignore the second file name - that is only a file that included the file that has the whitespace. The first file is the one you have to edit, not the second one.
NAVEED
Also, as per my comment above, make sure you haven't echo'd any text, code, or anything else before calling header.
Dutchie432
Ah this must be it then, Before calling it I echo a generic introduction to the personality description. Thanks, I may figure this out yet:-)
Jelmar
+1  A: 

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)

Ankit Jain
+1 for explaining the need for `die;` Note, being a language construct and not a function means that `die;` or `exit;` are sufficient if a reason isn't being provided.
George Marian
A: 

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

Flash84x
Why wouldn't `echo "<script>window.location('http://destination.com/page.html');</script>";` work? Not that I disagree that PHP's header() function is the way to go here...
kander
I suppose that might work, fair enough.
Flash84x
A: 

echo a meta redirect

thomas
+2  A: 

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>
Dutchie432
and what if javascript is disabled :)
Sarfraz
Then the OP's question is moot.
Dutchie432