php login with restrictions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

php login with restrictions

Post 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
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: php login with restrictions

Post 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 :)
peterhall
Forum Newbie
Posts: 24
Joined: Sat Aug 21, 2010 5:47 pm

Re: php login with restrictions

Post 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
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: php login with restrictions

Post by agriz »

Register user after login?

Or do you want to store the login date and time details?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: php login with restrictions

Post 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.
Post Reply