Solved

Restricting access to page

Posted on 2013-01-22
4
419 Views
Last Modified: 2013-01-24
Hi all,

I am using a PHPfox script and need to follow their syntax but am wondering how to restrict access to a page. The following code restricts access for only logged in users:

public function process() 
    {         
	phpfox::isUser(true);
    } 

Open in new window


I want to restrict it also so that the user must be logged in and must also be a member of a particular user group (let's say usergroups 1,2 and 3)

Here is how I got the value of the user's user group if they are logged in:

$username2 = Phpfox::getUserBy('user_group_id');

Open in new window


Can someone tell me how to put both of these together so that the user must be logged in and be a member of this page also?

Thanks,

D
0
Comment
Question by:TLN_CANADA
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
You need to do a check on their group id then decide to let them in or not. Without seeing the class I'm not sure what it does but basically, you assign the users group to a variable ($group or $groupid). Same thing for the logged in value. There will be something in that class to see if they are logged in. Assign them to variables then somewhere in your script you do a simple check. If they aren't logged in or in the right group you redirect them.

Logged in should be a bool value (true/false)

This must be done before any output to the browser so I would do it as soon as you instantiate the class object. For example let's say the groupid for this particular example is 123

if ($groupid != '123' || !$loggedin) {
    header("location: index.html"); // redirect them to whatever page you want
    die();
}
0
 

Author Comment

by:TLN_CANADA
Comment Utility
Here is what I am trying at the moment but it is giving the error that there are too many redirects on the page:

public function process() 
    {      
    phpfox::isUser(true);
	
	$usergroup = Phpfox::getUserBy('user_group_id');
	if ($usergroup != '123' || !$loggedin) {
    header("location: index.html"); 
    die();
}
    }

Open in new window


Could you tell me how to phrase it in an if statement so that it only allows access if both conditions are true (logged in user and member of group 1-3)

Thanks so much,

Derek
0
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
Comment Utility
Well you already have that logic there in this line

if ($usergroup != '123' || !$loggedin) {
 // redirect
}

You need to change the user group from '123' to the actual usergroup you are testing for. I put '123' as an example but I don't know what your user groups are named.

If a user must be in usergroup 1, 2 or 3 (replace the 1 2 3 with the actual usergroup ids or names) then you would test for them all like this...

if ($usergroup != 1 || $usergroup != 2 || $usergroup != 3 || !$loggedin) {
   // redirect here.....
}

You will need to make sure the $loggedin variable is set properly and I'm not sure how you get that information from your class.

I would remove the check for loggedin for now and just check for the user groups first. Once that is working then add the loggedin check.

Hope this helps
0
 

Author Closing Comment

by:TLN_CANADA
Comment Utility
Thank you!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now