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.

feeela
  • 29,399
  • 7
  • 59
  • 71
user705260
  • 278
  • 1
  • 4
  • 13

7 Answers7

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.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • Not trying to. die() or exit in this circumstance is far more obvious than wrapping the rest of the file in an else statement – cwallenpoole Jul 20 '11 at 22:04
  • 1
    @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). – James Allardice Jul 20 '11 at 22:08
  • 4
    @yi_H maybe you should read this question: http://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php – cwallenpoole Jul 20 '11 at 22:09
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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0
if (!is_user_logged_in())
{
        header("Location: http://www.****.com/non-member.php");
        exit;
}
//rest of content.
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
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.

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
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');

Justin Lucas
  • 2,301
  • 14
  • 22
  • 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. – feeela Jul 20 '11 at 22:21
0

Try this:

<?php
if (!is_user_logged_in() ) {
      die(header("Location: http://www.****.com/non-member.php"));
}
?>
The Mask
  • 17,007
  • 37
  • 111
  • 185
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.

hakre
  • 193,403
  • 52
  • 435
  • 836
slmd
  • 41
  • 1
  • 7