Current location: Hot Scripts Forums » Programming Languages » PHP » Login script - Secure?

Login script - Secure?

Reply
  #1  
Old 06-05-08, 06:44 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
Login script - Secure?

I have wrote a simple login script and was wondering if it is secure and if not, how insecure is it.

The Login Script:
PHP Code:
require("../config/config.php");

$connection mysql_connect("$host""$usr""$pwd")or die("cannot connect");
mysql_select_db("$db")or die("cannot select DB");

$username mysql_real_escape_string(strip_tags($_POST['username']));
$password mysql_real_escape_string(strip_tags($_POST['password']));

$query mysql_query("SELECT * FROM users WHERE username ='$username' and password = '$password'");
$count mysql_num_rows($query);

if(
$count==1){

session_register('username');
session_register('password');
$_SESSION['username'] = $username;
$_SESSION
['password'] = $password;
header("location:welcome.php");
}

else {
header("location:error.php");



The login checking at the top of each file:
PHP Code:
require('../config/config.php');

$username $_SESSION['username'];
$password $_SESSION['password'];
if (!
session_is_registered("username") and !session_is_registered("password"))
{
header("location:error.php");exit;
}
$connection mysql_connect($host,$usr,$pwd);
$query mysql_db_query($db"SELECT * from users where username = '$username' and password = '$password'"$connection);
$row mysql_fetch_assoc($query);
$u $row["username"];
$p $row["password"];
if(
$u != $username or $p != $password ){
header("location:error.php");exit;} 
__________________
Aye!
Reply With Quote
  #2  
Old 06-05-08, 11:36 AM
nfriedly's Avatar
nfriedly nfriedly is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
I don't see any glaring security holes in your script, but I have a couple of suggestions:

1) don't store passwords as plain text in your database. This is to protect your users should your database ever be compromised. Instead, store them as a hash of some sort. md5() is amazingly simple, and crypt() is only slightly more difficult to implement.
http://us2.php.net/md5
http://us2.php.net/crypt

2) you don't really need to store their password in the session - I doubt you'll be needing it again. Especially if it's encrypted.

3) location headers are *supposed* to have the full http://domain.com/whatever.php, not just the whatever.php. However, prettymuch every browser out there can handle it, so this is mostly a moot point.
http://www.w3.org/Protocols/rfc2616/....html#sec14.30
Reply With Quote
  #3  
Old 06-05-08, 11:40 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
If I don't store the username and password in the session, how can I check the login status on each page to make sure they are logged in? i.e. so that someone can't just type the url of an edit page and be logged in from there since they didn't have to go through the login part?
__________________
Aye!
Reply With Quote
  #4  
Old 06-05-08, 11:44 AM
nfriedly's Avatar
nfriedly nfriedly is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Do store the username, just not the password. If the username is stored in the session, you can safely assume that their password was already checked and they are logged in.
Reply With Quote
  #5  
Old 06-05-08, 11:50 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
ah! Ok. Thanks a lot. If I md5 the passwords, how would I be able to send out password reminders? Can php decode them again?
__________________
Aye!
Reply With Quote
  #6  
Old 06-05-08, 01:16 PM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Deansatch View Post
ah! Ok. Thanks a lot. If I md5 the passwords, how would I be able to send out password reminders? Can php decode them again?
You have to generate other password and change, MD5 is one-way encrypt

About your script, I have some sugestions, look:


PHP Code:
<?php
# THIS CAN STAY IN config.php
session_start();

require(
"../config/config.php");

# THIS CAN STAY IN config.php
$connection mysql_connect($host$usr$pwd)or die("cannot connect");
mysql_select_db($db)or die("cannot select DB");

$username mysql_real_escape_string($_POST['username']);
$password mysql_real_escape_string($_POST['password']);


$query mysql_query("SELECT * FROM users WHERE username ='$username' and password = '$password'");
$count mysql_num_rows($query);

if(
$count == 1) {

    
$r mysql_fetch_assoc($query);

    
$_SESSION['id'] = $r['id']; 
    
$_SESSION['username'] = $r['username'];
    
    
header("location: http://.../welcome.php");

}

else {
    
header("location: http://.../error.php");
}  
?>


The login check:

PHP Code:
<?php
# THIS CAN STAY IN config.php
session_start();

require(
'../config/config.php');

if (empty(
$_SESSION['username']) || empty($_SESSION['id'])) {
    
header("location: http://.../error.php");
    exit();
}
?>
Do a test!

Last edited by Keyne; 06-05-08 at 01:22 PM.
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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
most suitable php/mysql login script benalex Script Requests 0 06-15-07 01:19 AM
Run Your Own Profitable and VERY unique eBusiness Voltaire General Advertisements 1 09-06-05 12:51 PM
Login and Upload Script JamesLake Database 0 03-27-05 06:27 AM
Login script not working in frame... varial PHP 6 03-04-05 10:26 AM


All times are GMT -5. The time now is 10:25 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.2 (Unregistered)