0

Im trying to create a redirect if the user is not logged in and i am havin trouble

Here is the method im using

<?php
if ( is_user_logged_in() ) {
?>

/*My Content*/

<?php
    } else {
        echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.*****.com/non-member.php">';
    }
?>

This is working fine but it is delayed and im able to see the page for a few seconds before it redirects.

What i would like to do is something like this.

<?php
if ( is_user_logged_in() ) {
     /*IGNORE NEXT STATEMENT*/
} else {
        echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.****.com/non-member.php">';
    }
?>

Im not sure if this is possable but i would assume there is a mehod out there somehow.

0

7 Answers 7

8

For that type of behavior, you're really better off using header:

<?php
// place at the top, before anything (including whitespace).
if( !is_user_logged_in() ) {
    header("Location: http://www.****.com/non-member.php");
    die(); // you don't want the rest of the page showing.
}
?>

That will do what you're trying without letting the person who isn't logged in see the page first.

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

3 Comments

Not trying to. die() or exit in this circumstance is far more obvious than wrapping the rest of the file in an else statement
@yi_H - what's wrong with using die here? (Or exit for that matter, which I tend to prefer over die for no particular reason).
@yi_H maybe you should read this question: stackoverflow.com/questions/1795025/…
1

Try

if (!is_user_logged_in()) {
    header("Location: http://www.*****.com/non-member.php");
}

instead. ! is a boolean 'not', which inverse the results of the if() test, and the header() call is a less verbose/trustier method than issuing a meta header embedded in HTML.

Comments

0
if (!is_user_logged_in())
{
        header("Location: http://www.****.com/non-member.php");
        exit;
}
//rest of content.

Comments

0

Use the Location header for redirects:

<?php
if ( is_user_logged_in() ) {
     /* snip */
} else {
    header("Location: http://www.****.com/non-member.php");
}
?>

Using this method will trigger a redirect as soon as the HTTP headers are recieved and the HTML page itself will be completely ignored.

Comments

0

If is_user_logged_in() is false then the code inside that if statement does not in fact run and is pretty much ignored (it is still parsed for errors). Your delay is probably coming from your redirect. Try using header() to redirect. For example:

header('Location:http://www.google.com');

1 Comment

The delay comes from using a META-redirect, which is only 'executed' after the DOM was loaded completely, thats why most people here suggest using HTTP-header-redirects.
0

Try this:

<?php
if (!is_user_logged_in() ) {
      die(header("Location: http://www.****.com/non-member.php"));
}
?>

Comments

0

Why dont you try this

<?php 
if (!is_user_logged_in() ) {
    echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.****.com/non-member.php">';
} else {
    Do your thing
}
?>

The "!" placed at the beginning of the condition will check if the condition is not verified.

Comments

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.