0

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

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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>

2 Comments

and what if javascript is disabled :)
Then the OP's question is moot.
1

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)

1 Comment

+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.
1

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

4 Comments

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
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.
Also, as per my comment above, make sure you haven't echo'd any text, code, or anything else before calling header.
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:-)
0

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

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.