If do nothing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rabastan
Forum Newbie
Posts: 6
Joined: Thu May 24, 2012 12:14 pm

If do nothing

Post by Rabastan »

how would i actually make this work??

Code: Select all

if($login->isLoggedIn())
   "do nothing"
else
  header('location: admin_log.php');
meaning i want it to do nothing if its logged in

Rab
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: If do nothing

Post by Benjamin »

First, start using braces.

Code: Select all

if($login->isLoggedIn()) {

} else {
    header('location: admin_log.php');
}
Second, negate the condition with an !:

Code: Select all

if(!$login->isLoggedIn()) {
    header('location: admin_log.php');
}
Third, header redirects require an absolute path:

Code: Select all

if(!$login->isLoggedIn()) {
    header('Location: http://domain.com/admin_log.php');
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: If do nothing

Post by requinix »

And fourth, a header() redirect won't terminate your script - it will keep running. Assuming you don't want that, exit; or die; immediately after.

Code: Select all

if(!$login->isLoggedIn()) {
    header('Location: http://domain.com/admin_log.php');
    exit;
}
Rabastan
Forum Newbie
Posts: 6
Joined: Thu May 24, 2012 12:14 pm

Re: If do nothing

Post by Rabastan »

First of all, Thank you both for your help. I am rather new to all this, would you please explain what you meant by "Start using braces"? If I am doing something wrong I would like to fix it.

Rab
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: If do nothing

Post by Live24x7 »

start using braces means use curly braces

Code: Select all

if () 
{
//this within curly brace
}

else
{
this within curly brace
}
you have ommited the curly brace in your code
Post Reply