Page 1 of 1

php login with restrictions

Posted: Sun Aug 22, 2010 10:37 am
by peterhall
hi there. i've got a dbase where the table users is with the following definitions:

username -- password -- service
user1 11111111 TTT
user2 2222222 FFF
user3 44444444 TTT
user4 33333333 XXX

and what i need, is if the user1 login, only have access to the pages of the service "TTT", the user2 to pages with the service "FFF", and so on...

can someone help me on this?

thank u

Re: php login with restrictions

Posted: Sun Aug 22, 2010 10:52 am
by oscardog
Hello,

What you need to do is detect the 'service' upon login and set a cookie/session variable to store their 'access' in. Something like:

Code: Select all

//Obviously you would need to grab the information from the database and set it.
$_SESSION['service'] = $userService;
Then at the top of all pages you wish to restrict add a little check:

Code: Select all

if($_SESSION['service'] != "TTT") { //This would mean only users with the service TTT could access
//Redirect user to login
header("Location: login.php");
}
That is a very simple way of doing it. But it will work. You will need to change the if statement in the second PHP block depending which group of users you want to access the page. So if you wanted FFF and XXX to access the page you use:

Code: Select all

if($_SESSION['service'] != "FFF" || _SESSION['service'] != "XXX") { //This would mean only users with service FFF or XXX could access
//Redirect user to login
header("Location: login.php");
}
Hope that answers your question :)

Re: php login with restrictions

Posted: Sun Aug 22, 2010 11:06 am
by peterhall
hi there. thank u for the fast reply. i'll test it and then i'll let u know.

another question: how to register the users in a table every time they make a login???

tks

Re: php login with restrictions

Posted: Sun Aug 22, 2010 12:18 pm
by agriz
Register user after login?

Or do you want to store the login date and time details?

Re: php login with restrictions

Posted: Sun Aug 22, 2010 12:41 pm
by oscardog
peterhall wrote:hi there. thank u for the fast reply. i'll test it and then i'll let u know.

another question: how to register the users in a table every time they make a login???

tks
Surely users must register before they can login?

Therefore the data would already be in the database when they login and you would only need to check if they exist, that the password is correct and then store the service like I mentioned above. Not sure what you mean by "register" the users when they login.