FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Using PHP "OR" Logical Operator





ftv_flung
I'm having some trouble using OR in PHP...

Here's my example...

It's a user system, so for one page, it checks the user's rank. For example, if an admin is 5, this code will only let users at this level access the page..

Code:

<?php
while ($row = mysql_fetch_array($result))
{

$rank = $row[rank];

if ($rank != 5)
{
header('Location: index.php');
}

}
?>


What I want to do is have multiple user levels allowed to access a page... what is the best way to do this??

Thanks in advance for any replies Smile
DanielXP
Code:
if (($rank != 5) AND ($rank != 6))
{
header('Location: index.php');
}


You could do it that way Smile
Star Wars Fanatic
You could use a select statement and redirect them to different pages depending on their levels, or you could direct everything lower then 5 to one place:

Code:
select ( $rank )  {
  case 1:
     header('Location: index.php') ;
     break ;
  case 2:
     header('Location: index.php') ;
     break ;
  case 3:
     header('Location: index.php') ;
     break ;
  case 4:
     header('Location: index.php') ;
     break ;
  case 5:
     break ;
}


To get it so cases 1 through 4 all go the same place:

Code:
select ( $rank )  {
  case 1:
  case 2:
  case 3:
  case 4:
     header('Location: index.php') ;
     break ;
  case 5:
     break ;
}
rvec
I suggest only using the switch if you want to send users to 3 or more pages.
If you want to send users with a level above 3 to one page and all others to some other You should use this:
Code:
if ($rank > 3) {
    header('admin.php');
} else {
    header('index.php');
}


the switch should only be used to prevent you using a lot of elseif () ...
sonam
In similar situation I am prefer case/switch function like Star Wars Fanatic sugest but for your needs you can include one more line default what will send all undefined users on your index page.

Code:
switch($rank) {
        case "1": header('Location: page1.php') ; break;
        case "2": header('Location: page2.php') ; break;
        case "3": header('Location: page3.php') ; break;
        default: header('Location: index.php') ;
    }


Sonam
ftv_flung
Hmmmm.... I would like to have it, so I can choose a number, so if there are different levels, I can choose Level 2 and 3, or 1 and 3... etc.

Update: I've used DanielXP's suggestion, and it's worked - thanks! XD
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
Using php to change content of a .txt
How to get the tree structure done using PHP
Making a expression calculator using PHP.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.