user705260
user705260

Reputation: 278

PHP continue statement?

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.

Upvotes: 0

Views: 303

Answers (7)

slmd
slmd

Reputation: 41

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.

Upvotes: 0

The Mask
The Mask

Reputation: 17437

Try this:

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

Upvotes: 0

Justin Lucas
Justin Lucas

Reputation: 2321

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');

Upvotes: 0

Marc B
Marc B

Reputation: 360762

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.

Upvotes: 1

Matthew Scharley
Matthew Scharley

Reputation: 132394

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.

Upvotes: 0

Gary Willoughby
Gary Willoughby

Reputation: 52588

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

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 82058

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.

Upvotes: 8

Related Questions