If session is not user or admin....

encodeme
encodeme used Ask the Experts™
on
Hi,
I'm not sure where my blunder is here.
I want to stay on this page unless the person logged in is not one of user or admin.


if ($_SESSION['usertype'] != 'user') && ||($_SESSION['usertype'] != 'admin') {
		
	header("Location:index.php");
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010

Commented:
Do you mean this?

if ($_SESSION['usertype'] == 'user')  || ($_SESSION['usertype'] == 'admin') {
            
      header("Location:index.php");
      }

Author

Commented:
Hi Marqus,
My login script directs both user and admin to a page.

What I want is that if anyone else other than user or admin tries to see this page they are directed to index.php.

I'm not sure your solution does this ,
Thanks
Marco GasiFreelancer
Top Expert 2010

Commented:
Sorry for misunderstanding :-)

if ($_SESSION['usertype'] != 'user') && ($_SESSION['usertype'] != 'admin') {
            
      header("Location:index.php");
      }
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Marco GasiFreelancer
Top Expert 2010

Commented:
You had an error in your syntax: you wrote && || and this should raise an error: be sure to set error_report(E_ALL);
while you are deplying your scripts (and to comment that lione once script is in production)

Cheers

Author

Commented:
Hi Marqus,
I'm not sure why but it will not run.
I've checked it individually and it works eg.
if ($_SESSION['usertype'] != 'admin'){
	header("Location:index.php");
	}

Open in new window

But when I combine them it does not run.
Where should I place the
error_report(E_ALL);

Open in new window

Marco GasiFreelancer
Top Expert 2010
Commented:
Place error_report(E_ALL); at the top of your script.

I was wrong again: replace && with ||

if ($_SESSION['usertype'] != 'user') || ($_SESSION['usertype'] != 'admin') {
           
      header("Location:index.php");
      }

If user is not user or if he is not admin then go away.

Cheers
Commented:
Try using more straightforward programming style:

$usertype = $_SESSION['usertype'];

if ($usertype != 'user'  &&  $usertype != 'admin')
  {
      header("Location:index.php");
  }


Makes it much easier to debug :)
ALWAYS put an exit after a header/location or things will not work as you expect.


Yodercm said: "Try using more straightforward programming style:"

Well, if we are going THAT way.... ;-)

switch( $_SESSION['usertype'] ) {

      case 'admin':
      case 'user':
                      break;

       default:
                      header("Location: index.php");
                      exit;
}

Most Valuable Expert 2011
Top Expert 2016
Commented:
Maybe this design pattern will help.  You only need to add one column to the data base to know if a client is a "user" or an "admin."
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

Commented:
Hi endoceme,

Thank you for the points, but I really think you should share some of the points to the others who also tried to help you.  The solution given by marqusg was almost the same as mine, and bportlock's point about exit() and his suggestion for using CASE were excellent ones.  Even if mine is the one you used, they both put in some time and effort to help you.  

Why don't you request attention and ask the moderator to reset, then accept again sharing some of the points with those two people.

Cheers,
C.

Author

Commented:
Hi yodercm,
I appreciate your sensitive  comments. As with coding ...I'm a begginer here.
I'll try and get the points distributed.
Thanks
encodeme

Author

Commented:
Phew ... hope that's an acceptable point distribution.
Once more thanks for all the help.
:)
Marco GasiFreelancer
Top Expert 2010

Commented:
lol, hope you're not too tired after having thought on how to distribute points :-) Thanks for them and thanks to yodercm also: you're always extremely correct.
Thanks Yodercm and  encodeme.

Commented:
Good job, endoceme :)

Commented:
And if I could only spell, I'd say

Good job, encodeme :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial