SQL injection prevention

This is a discussion on "SQL injection prevention" within the PHP Forum section. This forum, and the thread "SQL injection prevention are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 6th, 2007, 11:53
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
SQL injection prevention

I have a login system and I want to be able to prevent SQL injection but whatever I try doesn't work.

When I add mysql_real_escape_string() it stops the form for displaying.

I tried doing this to the login form but it didn't work

PHP: Select all

$username mysql_real_escape_string($_POST['username']);
$password mysql_real_escape_string($_POST['password']);
 
$result user_login($username$password); 
Does the mysql function need to go in my login function?

Here is the login function
PHP: Select all

function user_login($username$password)
{
     
// Try and get the salt from the database using the username
     
$query "select salt from users where username='$username' limit 1";
     
$result mysql_query($query);
     
$user mysql_fetch_array($result);
     
// Using the salt, encrypt the given password to see if it 
     // matches the one in the database
     
$encrypted_pass md5(md5($password).$user['salt']);
     
// Try and get the user using the username & encrypted pass
     
$query "select userid, username, user_level from users where username='$username' and password='$encrypted_pass'";
     
$result mysql_query($query);
     
$user mysql_fetch_array($result);
     
$numrows mysql_num_rows($result);
     
$userid $user['userid'];
     
$user_level $user['user_level'];
     
// Now encrypt the data to be stored in the session
     
$encrypted_id md5($user['userid']);
     
$encrypted_name md5($user['username']);
     
$encrypted_user md5($user['user_level']);
     
// Store the data in the session
     
$_SESSION['userid'] = $userid;
     
$_SESSION['username'] = $username;
     
$_SESSION['user_level'] = $user_level;
     
$_SESSION['encrypted_id'] = $encrypted_id;
     
$_SESSION['encrypted_name'] = $encrypted_name;
     
$_SESSION['encrypted_user'] = $encrypted_user;

    if (
$numrows == 1)
    {
        return 
'Correct';
    }
    else
    {
        return 
false;
    }

Here is the login form
PHP: Select all

 <?php
// Include init file
include 'init.php';
if (!isset(
$_POST['submit']))
{
     
// Show the form
     
include 'includes/login_form.inc.php';
     exit;
}
else
{
     
// Try and login with the given username & pass
     
$result user_login($_POST['username'], $_POST['password']);
     if (
$result != 'Correct')
     {
          
// Reshow the form with the error
          
$login_error $result;
          include 
'includes/login_form.inc.php';
     }
     else
     {
          echo 
'Thank you for logging in, <a href="index.php">click here</a> to go back.';
     } 
}
?>
and finally the login_form.inc file
PHP: Select all

<?php if (isset($login_error)) { ?>
There was an error: <?php echo $login_error?>, please try again.
<?php ?>
<form action="login.php" method="post">
<b>Username:</b> <input type="text" size="20" maxlength="20" name="username" 
<?php if (isset($_POST['username'])) { ?> value="<?php echo $_POST['username']; ?><?php ?>/><br />
<b>Password:</b> <input type="password" size="20" maxlength="10" name="password" /><br />
<input type="submit" name="submit" value="Login" />
</form>
Reply With Quote

  #2 (permalink)  
Old Sep 6th, 2007, 13:22
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: SQL injection prevention

You must have an active connection to MySQL before you can use mysql_real_escape_string.

So conenct to to your DB on your login page if you want to use the function there.

Or just escape the data in the function.
Reply With Quote
  #3 (permalink)  
Old Sep 6th, 2007, 13:47
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
Re: SQL injection prevention

Thanks Blake121

I got it fixed now
Reply With Quote
  #4 (permalink)  
Old Sep 6th, 2007, 13:55
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: SQL injection prevention

No Problem
Reply With Quote
Reply

Tags
login, reigstration, sql injection

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
question about database injection sudhakararaog PHP Forum 0 Mar 17th, 2008 01:00
SQL Injection Security PHP nate2099 Databases 7 Jul 14th, 2007 13:58
Devilsown water injection rocket468 Free Web Site Critique 9 Jan 21st, 2007 18:13
PHP & Mysql injection in phplist ktsirig PHP Forum 2 Nov 9th, 2006 07:48
ohol-injection.com rocket468 Free Web Site Critique 2 Oct 27th, 2006 00:03


All times are GMT. The time now is 11:45.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43