• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Php If Statment with "or" clause

Experts, I'm having a syntax issue.

This code works... it checks to see if the logged in user is an admin.  If they are not an admin, it redirects the user correctly...

if ($row['admin_id'] !== $_SESSION['UserID'])
{
	header("Location: noaccess.php"); /* Redirect browser */
	exit;
}

Open in new window


I now want to modify that code to also allow a superuser access, but I'm having an issue with the OR statement...  Let's say the Superuser's ID was 100.

I've tried the following examples with no luck:

if ($row['admin_id'] !== $_SESSION['UserID'] OR '100')
{
	header("Location: noaccess.php"); /* Redirect browser */
	exit;
}

Open in new window



if ($row['admin_id'] !== $_SESSION['UserID'] || $row['admin_id'] !== '100')
{
	header("Location: noaccess.php"); /* Redirect browser */
	exit;
}

Open in new window


Any suggestions?

-dsg
0
dsg138
Asked:
dsg138
  • 5
  • 3
2 Solutions
 
GaryCommented:
Shouldn't it be &&, OR will always return true if one of the condition is not met.

if ($row['admin_id'] != $_SESSION['UserID'] && $row['admin_id'] != '100')
{
      header("Location: noaccess.php"); /* Redirect browser */
      exit;
}

Though if there is only one admin id why would you need two checks.
0
 
Marco GasiFreelancerCommented:
Your first code is wrong: OR is not inm php syntax. For the second one, try this:

if ($row['admin_id'] != $_SESSION['UserID'] || $row['admin_id'] != '100')
{
      header("Location: noaccess.php"); /* Redirect browser */
      exit;
}
                                 
Cheers
0
 
Marco GasiFreelancerCommented:
Oh, yes, GaryC123 is right.
But note GaryC123 has used != and not !==: this is because if you use !== or === notation, you're checking not only the value but the type also. So, if $row['admin_id'] is an integer value and you check to see if it is !== '100', you'll ever get a false result since '100' is a string.

Read more about comparison operators here: http://it2.php.net/manual/en/language.operators.comparison.php

Cheers
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Marco GasiFreelancerCommented:
A last note: you should use OR clause only if you would check if values are equals:

if ($row['admin_id'] == $_SESSION['UserID'] || $row['admin_id'] == '100')
{
//some stuff as
echo "Wellcome, $row['admin_id']";
}else{
      header("Location: noaccess.php"); /* Redirect browser */
      exit;
}
0
 
GaryCommented:
Actually you can use OR or ||
http://www.w3schools.com/php/php_operators.asp
0
 
Marco GasiFreelancerCommented:
This is wath I read in that page

Operator: x || y       
Name: Or
Description:       True if either or both x and y are true
Examples: x=6
                 y=3
                 (x==5 || y==5) returns false

I don't see anything which could make think you cvan use OR in a php code

Cheers
0
 
Marco GasiFreelancerCommented:
I saw it now :-)
0
 
dsg138Author Commented:
Thanks Guys,
Gary, you're correct, it should've been &&.  By using an Or clause, I was almost guaranteeing it returned false every time because in almost every situation, one of those id's will be false.

Marqus, Thanks, I was not aware of the difference between != and !==.  My id is always an integer so I used '!=100'.  I suppose !==100 would work if I didn't use quotes?

Appreciate the help!
-dsg
0
 
GaryCommented:
Yes, because !== compares the value and var type (e.g. are they both a string or are they both a number) while != only compares the value regardless of its type (number or string doesn't matter)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now