Current location: Hot Scripts Forums » Programming Languages » PHP » help me improve my login script please


help me improve my login script please

Reply
  #1 (permalink)  
Old 08-11-03, 04:11 AM
paulj000 paulj000 is offline
Bull in a china shop
 
Join Date: Jul 2003
Location: California, USA
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
help me improve my login script please

Hi,

I have made a working login script that checks stuff against a database and then redirects to the appropriate subdirectory if there is a username/password match. If match is found then session variable for the specific subdirectory is set and is continuously checked each time a protected page is requested later and checked again for a match or the user is kicked out.

I had a real hard time making this and I am sure there are ways to improve what I have. This is how I need your help.

Here is the login page code and the processing page (proc.php) code:

Code:
CONTENTS OF INDEX.HTML=========

<form action="proc.php" method="post">
<input type="text" class="input" name="username">
<input type="password" class="input" name="password"">
<input type="submit" class="send" value="L O G I N">
</form>
PHP Code:

CONTENTS OF PROC.PHP===========

<?
session_start
();
$username $_POST['username'];
$password $_POST['password'];
include (
"dbconnect.php");// supplies credentials to connect to Database


$sql "SELECT * FROM logins ";
$sql .= "WHERE user='".$username."';";

$result mysql_query($sql);
$line mysql_fetch_array($resultMYSQL_NUM);

$directory $line[2];
session_register('directory');// this is needed later to look up the validation in DB on each protected page


if ($username == "") { 
header ("Location: index.html"); 
}
if (
$password == "") { 
header ("Location: index.html"); 
}

if (
$username == $line[0] && $password == $line[1]) { 
    echo 
'
    <html>
    <head>
    <meta http-equiv="refresh" content="0;URL='
.$directory.'">
    </head>
    <body>
    <center>
    
    <h4>Logging In</h4>
    
    <p>please wait...</p>
    
    <center>
    </body>
    </html>
    '

}
else { 
header ("Location: index.html"); }
?>
For starters is this a decent vailidation for initial access checking? Are there slicker ways of doing it?

Thanks - paul
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-11-03, 08:06 AM
kickinhard007's Avatar
kickinhard007 kickinhard007 is offline
Newbie Coder
 
Join Date: Aug 2003
Location: Manchester, UK
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
i wouldn't use...
if ($username == "") {
header ("Location: index.html");
}
if ($password == "") {
header ("Location: index.html");
}

do it another way, like...

if ($username == "" & $password =="") {
header ("Location: index.html");
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-12-03, 05:26 PM
paulj000 paulj000 is offline
Bull in a china shop
 
Join Date: Jul 2003
Location: California, USA
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you,

That streamlined my code a bit and works just dandy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-13-03, 03:42 AM
kickinhard007's Avatar
kickinhard007 kickinhard007 is offline
Newbie Coder
 
Join Date: Aug 2003
Location: Manchester, UK
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
i think the '&' needs to be '&&', on reflection :-)

glad to help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-14-03, 10:54 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
I'd do some username validation as well. You've gotta test if the user actually exists in the database. The code which tests the username and password against two db rows for equality is checking if the user exisits yeh? If so, this would be better:

PHP Code:

// Using your $result var.


$found_user = @mysql_num_rows($result);

if(
$found_user 0){
// do session and header stuff
}
else {
// print a "user not found" error.

PS: If your not encrypting your passwords in the db then you should.

PHP Code:

// Encrypt the password before it goes in the db:


$password md5($_POST["password"]); /* you can use crypt() as an alternative. */

// This line of code would be used in the registration script. 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-15-03, 03:51 AM
webscript webscript is offline
Webscript Lead Developer
 
Join Date: Jun 2003
Location: New Zealand
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
I use an ASP.NET login system.

I built it myself.

It has these features.

- User Levels (member, vip, admin)
- Database to store usernames and enrypted passwords
- Cookies to start 'session'
- Every page checks cookie for access

- Fully working admin
--- Delete Users
--- Edit Users
--- Only admin level members get access admin
--- Ability to deactivate users

- Internal Message Service (i am still finishing this)
__________________
Webscript
Low Cost High Quality Webdesign and Development.
Website: http://www.webscript.co.nz
Forum: http://www.webscript.co.nz/forum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 08-15-03, 12:58 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Correct me if im wrong but this is a PHP board. Not an ASP.NET board. The guy wanted his login script improved by ppl posting some script examples. You failed to do so, you're just explaining what yours can do.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 08-15-03, 01:07 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Webscript, a small note... Your Admin system failed to retrieve my IP address. It's displaying in incorrect one.

My IP: 81.76.151.55
My ISP IP: 195.92.168.35

Your system is displaying: 195.92.168.167

I think this issue needs considering.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 08-15-03, 06:37 PM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
$username = $_POST['username'];
$password = $_POST['password'];

include ("dbconnect.php");// supplies credentials to connect to Database

$sql = "SELECT * FROM logins ";
$sql .= "WHERE user='".$username."';";
Also, you would need to do some checking on the 'username'. It is not a good idea to query user supplied information from a DB without checking it.

One way to do it would be something like this:

Quote:
$SQL = "SELECT Users_ID, Users_Access FROM users WHERE Users_Name=" . Custom_StripText($login) . " AND Users_Password=" . Custom_StripText($password);
$db->query($SQL);
$Result = $db->next_record();
if($Result) {
SetSession("UserID", $Result("Users_ID"));
SetSession("UserLogin", $login);
SetSession("UserPassword", $password);
SetSession("AccessLevel", $Result("Users_Access"));
}

Last edited by YourPHPPro; 08-15-03 at 06:57 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
Looking for a good review management script griz_fan Script Requests 10 07-29-07 06:08 AM
login script required lochie Script Requests 2 03-06-04 08:44 PM
login script @NVP@ Script Requests 2 09-30-03 01:44 AM
Talkback script..help pls BC_ PHP 0 06-22-03 07:44 PM
Need help with a script boardpix PHP 7 06-09-03 12:37 AM


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