[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

Restricting access to page

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
TLN_CANADA
Asked:
TLN_CANADA
  • 2
  • 2
1 Solution
 
Mark BradyCommented:
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
 
TLN_CANADAAuthor Commented:
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
 
Mark BradyCommented:
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
 
TLN_CANADAAuthor Commented:
Thank you!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now