Solved

If session is not user or admin....

Posted on 2011-09-03
17
364 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:encodeme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +2
17 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36478160
Do you mean this?

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

Author Comment

by:encodeme
ID: 36478212
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36478219
Sorry for misunderstanding :-)

if ($_SESSION['usertype'] != 'user') && ($_SESSION['usertype'] != 'admin') {
            
      header("Location:index.php");
      }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36478227
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
 

Author Comment

by:encodeme
ID: 36478272
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
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 60 total points
ID: 36478313
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
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 80 total points
ID: 36478371
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
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 80 total points
ID: 36478579
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 30 total points
ID: 36479121
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
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 36480308
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
 

Author Comment

by:encodeme
ID: 36480339
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
 

Author Closing Comment

by:encodeme
ID: 36484025
Phew ... hope that's an acceptable point distribution.
Once more thanks for all the help.
:)
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36484042
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 36484094
Thanks Yodercm and  encodeme.

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 36484783
Good job, endoceme :)
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 36484787
And if I could only spell, I'd say

Good job, encodeme :)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question