T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 64
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 215
  Visitor Online : 1
  Today Visit: 311
  Total Visitor : 301333
  Most Online : 41
::: Sponsored Links :::

PR 4 For This Webpage

Vinaora Logo
Home Today :

Anti SQL Injection Login Filter

Author : Integral

This function will help to prevent an SQL injection attack from being carried out against your website's login form. It probably won't stop EVERY variation of such an attack, but it'll give you some measure of security.

PHP Example :

#
# login.htm
#


<!DOCTYPE html PUBLIC "-//W3C//XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>MyWeb.com Login</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>

<body>
    <form method="post" action="verify.php">
    <input type="text" name="user" />
    <input type="password" name="pass" />
    <input type="submit" value="Login" />
    </form>
</body>
</html>


#
# verify.php
#

<?php

function anti_injection$user$pass )
{
    
# We'll first get rid of any special characters using a simple regex statement.
    # After that, we'll get rid of any SQL command words using a string replacment.

    
$banlist = array
        (
        
"insert""select""update""delete""distinct""having""truncate""replace",
        
"handler""like""as""or""procedure""limit""order by""group by""asc""desc"
        
);

    if ( 
eregi "[a-zA-Z0-9]+"$user ) )
    {
        
$user trim str_replace $banlist''strtolower $user ) ) );
    }
    else
    {
        
$user NULL;
    }

    
# Now to make sure the given password is an alphanumerical string
    # devoid of any special characters. strtolower() is being used
    # because unfortunately, str_ireplace() only works with PHP5.

    
if ( eregi "[a-zA-Z0-9]+"$pass ) )
    {
        
$pass trim str_replace $banlist''strtolower $pass ) ) );
    }
    else
    {
        
$pass NULL;
    }

    
# Now to make an array so we can dump these variables into the SQL query.
    # If either user or pass is NULL (because of inclusion of illegal characters),
    # the whole script will stop dead in its tracks.

    
$array = array ( 'user' => $user'pass' => $pass );

    if ( 
in_array NULL$array ) )
    {
        die ( 
'Hacking attempt. Go play someplace else, you script kiddie.' );
    }
    else
    {
        return 
$array;
    }
}


# Now to filter the login data through the Anti-Injection Attack function
# and assign the results to an array. The values used are assuming the
# login form itself is using the POST method, and the username and
# password fields were given the names of "user" and "pass"
# respectively. This works with the GET method, too, but
# I *STRONGLY* advise you not to use it.


$login anti_injection $_POST['user'], $_POST['pass'] );


# Verify the filtered user/pass combo...

$conn mysql_connect 'localhost''sql_user''sql_pass' );
$conn_db mysql_select_db 'some_db'$conn );

$result mysql_query "SELECT * FROM some_table WHERE user = '" $login['user'] . "' AND pass = '" $login['pass'] . "'" );

if ( 
mysql_num_rows $result ) > )
{
    
# Success!

    
echo "Welcome!";
}
else
{
    
# Humiliating defeat!

    
echo "Bad credentials.";
}

?>


Other Example


::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007