Tim C
Tim C

Reputation: 5714

<meta http-equiv="refresh" content="0; url=http://www.xxxxxxl.com/index.php" /> is not refreshing

I had a (very) simple login script which was working beautifully, and somehow, without much, if any, changes on the script page I have the following error:

The Problem

<meta http-equiv="refresh" content="0; url=http://www.xxxxxx.com/index.php" />

The above code is not refreshing, which means the user is not getting redirected after login. He/she has to manually refresh the page to start his/her session:

Enter image description here

As you can see from above image, the login was successful, but the page isn't refreshing.

Please note using header("location:") is not an option here:

Also note I'm using Ajax with the form submission for what that is worth...

Part of the login script:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Do stuff
    if(mysqli_num_rows($result) = 1) {
        while
        {
            // GET session variables
        }//while
        echo 'Success! Logging In....';
        echo '<meta http-equiv="refresh" content="0; url=http://www.xxxxx.com/index.php" />'//now refresh page
    } //if
} //if

Strangely, this was working for me above, but it has since yesterday stopped working.

Solution

Is there a reason why this is not working?

Is there anything I can do to fix this?

Is there any alternative approach?

Upvotes: 0

Views: 23616

Answers (3)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

You seem to be confusing all the technologies you are using to build your site. In the end you have nothing but plain HTML and it should look like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="refresh" content="5; url=http://www.google.com/">
    </head>

    <body>
        <p>Redirecting in 5 seconds...</p>
    </body>
</html>

How you generate this code is irrelevant from the browser point of view.

Browsers are designed to be very permissive about invalid HTML and you've pushed it to the limit. Yet both Firefox and Chrome see to handle even this just fine:

Not redirecting
<meta http-equiv="refresh" content="5; url=http://www.google.com/"></head>

So there must be something else you haven't shared.

Having said that, this is the weirdest way to redirect. I didn't even know people were still using it in 2015. Since you have PHP, leverage it:

header('Location: http://www.xxxxxx.com/index.php');
exit;

Upvotes: 1

NaijaProgrammer
NaijaProgrammer

Reputation: 2957

The way you are trying to do it will not work, because the <meta> tag is supposed to come in the <head> section of your page, not in the body. Plus, you can't use <meta http-equiv="refresh"> or any other header directives once output is sent.

Try replacing this part:

echo '<meta http-equiv="refresh" content="0; url=http://www.xxxxx.com/index.php" />'

with this:

echo '<script>location.href="http://www.xxxxx.com/index.php"</script>';

Upvotes: 3

MattieTK
MattieTK

Reputation: 521

A better way to do this would be to use PHP to change the headers:

header( "refresh:0;url=index.php" ); instead of your echo for the meta tag

Upvotes: 0

Related Questions