Solved

Restricting access to page

Posted on 2013-01-22
4
431 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
ID: 38809647
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
ID: 38812228
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
ID: 38812431
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
ID: 38816300
Thank you!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
two ways encryption with php 3 37
how to use a switch statement with heredoc 11 33
php56-php-mcrypt for rhel7 php56 1 51
Generate PDF from MySQL using PHP 3 19
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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 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