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.
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.
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.
if (!is_user_logged_in())
{
header("Location: http://www.****.com/non-member.php");
exit;
}
//rest of content.
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.
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');
Try this:
<?php
if (!is_user_logged_in() ) {
die(header("Location: http://www.****.com/non-member.php"));
}
?>
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.
精彩评论