Support Forums

Full Version: Simple secure login script.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A member nicknamed Janvier123 in a forum released this script in public, he said that it has "anti SQL inject". Take a look at the code bellow and mind telling me if that is true ? I have no idea about security in PHP.

PHP Code:
<?php

// Edit your mssql info here
// BEGIN MSSQL INFO
$CONFIG['host'] = "localhost";
$CONFIG['user'] = "sa";
$CONFIG['pass'] = "server";
// END MSSQL INFO

//----------------------------- DO NOT EDIT ANYTHING BELOW HERE !!!!! ------------------------------------

$CONFIG['conn']  = mssql_connect$CONFIG['host'], $CONFIG['user'], $CONFIG['pass']);

function 
anti_injection($sql) {
   
$sql preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"),"",$sql);
   
$sql trim($sql);
   
$sql strip_tags($sql);
   
$sql addslashes($sql);
   return 
$sql;
}
//----------------------------------------------------------------------------------------------------------

if(isset($_GET['action']) && ($_GET['action'] == "login")){

    
$user anti_injection($_POST['user']);
    
$pass anti_injection($_POST['pass']);
    
$crypt_pass md5($pass);
        
    
$result1 mssql_query("SELECT * FROM account.dbo.user_profile WHERE user_id = '".$user."'"); 
    
$count1 mssql_num_rows($result1); 

    
$result2 mssql_query("SELECT user_pwd FROM account.dbo.user_profile WHERE user_id = '".$user."'"); 
    
$row2 mssql_fetch_row($result2); 

    if(
$count1 == '0') {
        echo 
'<br>This game account is not found in the database.';
    }
    elseif(
$row2[0] != $crypt_pass) {
        echo 
'<br>Wrong password. Try again.';
    }
    elseif(
$_GET['login'] != 'login' && $count1 == '0') {
        echo 
'<br>Login Error, Please login again.';
    } else {
    
    
// Begin secure content 
        
$_SESSION['user'] = $user;
        echo 
"<h3>Welcome, ".$_SESSION['user']."</h3>";
        echo 
"<br>";
        echo 
"Your content here";
    
// Dont forget to and your session
    // session_destroy();
    // End secure content
    
}
} else {
  
echo 
'<h2>Login here</h2><br /> 
    <form name="" action="'
.$_SERVER['php_self'].'?action=login" method="post"> 
        Name: <input type="text" name="user" maxlength="16"><br /> 
        Password: <input type="password" name="pass" maxlength="16"> <br />
        <input type="submit" value="Login!"> 
    </form>'
;
}
?>
Yes it is true, or atleast appears so....
Anyway in the function anti_injection, replace the following:
PHP Code:
$sql addslashes($sql);
// WITH
$sql addcslashes(mysql_real_escape_string($sql), "%_"); 

This now takes LIKE injections into consideration.
Nice But How should I do If I want to alow more User then One?Smile
wayneee-