Jump to content

PHP if statement not working.


jattsurma

Recommended Posts

Hello Everyone,

 

From time to time, I get into a situation where my if statements do not give the desired results. I always have to play around with them to get the things working. I'm in same situation right now. I'm guessing that I do not completely understand the logic. If someone can take a look at the code below and tell me what's wrong, it will be greatly appreciated.


if($_SESSION['account_type'] != 'internal admin'){
die('<font color="red"><b>Error:</b> This page can only be accessed by internal or super admin.</font>');
break;
}
elseif($_SESSION['account_type'] != 'super admin'){
die('<font color="red"><b>Error:</b> This page can only be accessed by internal or super admin.</font>');
break;
}

 

I have echoed out the $_SESSION['account_type'] and I'm getting "internal admin" but for some reason, I'm getting the error "Error: This page can only be accessed by internal or super admin."

 

Thanks in advance for your help.

 

Jatt Surma

Link to comment
Share on other sites

if($_SESSION['account_type'] != 'internal admin'){

 

 

 

die('<font color="red"><b>Error:</b> This page can only be accessed by internal admin.</font>');

 

 

 

break;

}

elseif($_SESSION['account_type'] != 'super admin'){

 

 

 

die('<font color="red"><b>Error:</b> This page can only be accessed by super admin.</font>');

 

 

 

break;

}

 

try this , coz you are displaying the same message for both condition

 

Link to comment
Share on other sites

hello ansharma,

 

Thanks for your help. I understand why I'm getting that message. It's because I'm logged in as "internal admin" and than I have same statement saying that "If account type is not super admin" show that error.

 

What I'm trying to do is that only the "Super admins" and "internal admins" have access to that page. So customers or anyone else can not view it. How should I do that?

 

Thanks again for your time.

 

Jatt Surma

Link to comment
Share on other sites

You could try this:

$accountTypes = array("internal admin","super admin");
if(!in_array($_SESSION['account_type'], $accountTypes)) {
die('<font color="red"><b>Error:</b> This page can only be accessed by internal or super admin.</font>');
break;
}

 

That should effectively do the same thing. Let us know if it works, please? :)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.