If session is not user or admin....

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

encodemeAsked:
Who is Participating?
 
Cornelia YoderArtistCommented:
Try using more straightforward programming style:

$usertype = $_SESSION['usertype'];

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


Makes it much easier to debug :)
0
 
Marco GasiFreelancerCommented:
Do you mean this?

if ($_SESSION['usertype'] == 'user')  || ($_SESSION['usertype'] == 'admin') {
            
      header("Location:index.php");
      }
0
 
encodemeAuthor 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
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Marco GasiFreelancerCommented:
Sorry for misunderstanding :-)

if ($_SESSION['usertype'] != 'user') && ($_SESSION['usertype'] != 'admin') {
            
      header("Location:index.php");
      }
0
 
Marco GasiFreelancerCommented:
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
0
 
encodemeAuthor 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

0
 
Marco GasiFreelancerCommented:
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
0
 
Beverley PortlockCommented:
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;
}

0
 
Ray PaseurCommented:
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
0
 
Cornelia YoderArtistCommented:
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.
0
 
encodemeAuthor 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
0
 
encodemeAuthor Commented:
Phew ... hope that's an acceptable point distribution.
Once more thanks for all the help.
:)
0
 
Marco GasiFreelancerCommented:
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.
0
 
Beverley PortlockCommented:
Thanks Yodercm and  encodeme.

0
 
Cornelia YoderArtistCommented:
Good job, endoceme :)
0
 
Cornelia YoderArtistCommented:
And if I could only spell, I'd say

Good job, encodeme :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.