Current location: Hot Scripts Forums » Programming Languages » PHP » Tricky Login Page


Tricky Login Page

Reply
  #1 (permalink)  
Old 07-07-06, 07:21 AM
losse losse is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Barrie, Ontario, Canada
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Tricky Login Page

Hi there
I want to create a login page where 2 types of people come to 1 login page. users and administrators. Let's call this login page login.php.

So if a user comes in and logs in, they go to a client profile page (call it edit_user.php) and if an administrator logs in they go to a master user list page (call it client_list.php).

What would be the best way to a) flag the users/administrators and create that redirect on that login page?

I thought that maybe the users, when they register could be flagged in the DB as users (ie-create a field called "access" and put a value of "1" in there) and when when the login page runs the script, checks to see the "access" level, if "1" go here, if NULL, go there?

Does that make sense? You're input is greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 07-07-06, 07:41 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I would add a field called "permission". Normal users have permission set to 0, admins to 1 or 2. This way you can easily manage your usergroups later, or add moderators for example. You just give each usergroup it's own number. So when you log in you do something like this.

PHP Code:



$query 
mysql_query("
          SELECT * FROM users 
          WHERE username = '"
clean_input($_POST['username']) ."' 
          AND password = '"
clean_input($_POST['password']) ."'
"
);

if (
mysql_num_rows($query) == 1)   // User found
{
    
$userinfo mysql_fetch_array($query);
    
    if (
$userinfo['permission'] == 1// Is admin
    
{
          
header('Location: client_list.php');
          exit();
    }
    else  
// Isn't admin
    
{
          
header('Location: edit_user.php');
          exit();
    }
}
else  
// User not found.
{
     echo 
'Wrong username or password.';

Reply With Quote
  #3 (permalink)  
Old 07-07-06, 07:46 AM
losse losse is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Barrie, Ontario, Canada
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Sweet... So when the client registers I just pass a value of "0" in the DB right?

Since they're not filling out the "O" on a form, how would I insert that?

Something like:

PHP Code:

INSERT INTO client (permissionVALUES ('0', ) 

??
Reply With Quote
  #4 (permalink)  
Old 07-07-06, 07:47 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Either that, or you can set the default value to 0. This way you don't have to enter it each time.
Reply With Quote
  #5 (permalink)  
Old 07-07-06, 07:51 AM
losse losse is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Barrie, Ontario, Canada
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
What's this error?

Fatal error: Call to undefined function: clean_input() in /yadayada/cms_login.php on line 4

this is line 4: WHERE id = '". clean_input($_POST['id']) ."'
Reply With Quote
  #6 (permalink)  
Old 07-07-06, 07:55 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Remember the function I posted in your other thread? I used it here since I knew you had it. Include your functions.php file and it should work.
Reply With Quote
  #7 (permalink)  
Old 07-07-06, 07:59 AM
losse losse is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Barrie, Ontario, Canada
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Right.. thanks for that reminder...

That's weird now it says
Wrong username or password.

when I refresh the page... followed by the client ID field and the password field?

I checked all the fields in the DB and they match the form...
Reply With Quote
  #8 (permalink)  
Old 07-07-06, 08:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Try replacing this
PHP Code:

 $query mysql_query("

          SELECT * FROM users 
          WHERE username = '"
clean_input($_POST['username']) ."' 
          AND password = '"
clean_input($_POST['password']) ."'
"
); 
with this
PHP Code:

 $query mysql_query("

          SELECT * FROM users 
          WHERE username = '"
clean_input($_POST['username']) ."' 
          AND password = '"
clean_input($_POST['password']) ."'
"
) OR die( mysql_error() ); 
Any errors?
Reply With Quote
  #9 (permalink)  
Old 07-07-06, 08:04 AM
losse losse is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Barrie, Ontario, Canada
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Still getting the same error... by the way instead of "username" I have the field called "id"

By the way, there isn't anyone with a permission of "1" in the table.. .woudl this make a difference?
Reply With Quote
  #10 (permalink)  
Old 07-07-06, 08:09 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It shoud redirect to edit_user.php if $userinfo['permission'] isn't set or empty. Does the username or password contain any special characters that clean_input() removes? Try removing the function just to see if it works without.

Give this a try.
PHP Code:

$query ="SELECT * FROM users 

          WHERE username = '"
$_POST['id'] ."' 
          AND password = '"
$_POST['password'] ."'";

$result mysql_query($query) OR die( mysql_error() .'<br />'$query);

if (
mysql_num_rows($result) == 1)   // User found
{

// Rest of code.... 
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
Page access and user login nicpon ASP 10 09-09-05 03:53 PM
login page misterman PHP 0 06-04-04 06:36 AM
A login page grayfox Perl 2 04-20-04 09:54 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM


All times are GMT -5. The time now is 07:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.