?
Solved

is my login script vulnerable to sql injection attacks, if yes, what should i do

Posted on 2007-11-16
3
Medium Priority
?
338 Views
Last Modified: 2013-12-13
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
  $GLOBALS['PrevUrl'] = $accesscheck;
  session_register('PrevUrl');
}

if (isset($_POST['user'])) {
  $loginUsername=$_POST['user'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "login.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_mine, $mine);
 
  $LoginRS__query=sprintf("SELECT user_name, password, user_id, role, first_name, last_name, company_id, email FROM user WHERE user_name='%s' AND password='%s'",
    get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
   
  $LoginRS = mysql_query($LoginRS__query, $mine) or die(mysql_error());
  $row_LoginRS = mysql_fetch_assoc($LoginRS);
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
   
    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
      $GLOBALS['user1'] = $row_LoginRS['user_id'];      
      $GLOBALS['level'] = $row_LoginRS['role'];
      $GLOBALS['name'] = $row_LoginRS['first_name'];      
      $GLOBALS['lname'] = $row_LoginRS['last_name'];
      $GLOBALS['comp'] = $row_LoginRS['company_id'];      
      $GLOBALS['email'] = $row_LoginRS['email'];
      $GLOBALS['MM_UserGroup'] = $loginStrGroup;           

//register the session variables
session_start();
session_register("MM_Username");
session_register('user1');      
session_register('level');
session_register('name');
session_register('lname');
session_register('comp');
session_register('email');
session_register("MM_UserGroup");
$valid_user = 1;

0
Comment
Question by:jblayney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Expert Comment

by:kylealanhale
ID: 20299465
Well, you do add slashes, that's good.  The only other thing I would suggest would be to put anything (such as those username and password values) through quick regex check to make sure they only have the characters you want.  Something like
if (!preg_match('/[^\w\d!@#]{6,15}$/', $username)) die('Invalid username.');

Open in new window

0
 
LVL 3

Accepted Solution

by:
kylealanhale earned 800 total points
ID: 20299486
Sorry, small typo; here's a (hopefully) bug free example.  Untested, but the principle is true.  By the way, this checks for a username between 6 and 15 characters long, either a letter or a number, or the character !, @, or #.
if (!preg_match('/^[a-zA-Z\d!@#]{6,15}$/', $username)) die('Invalid username.');

Open in new window

0
 
LVL 1

Author Comment

by:jblayney
ID: 20300081
thank you
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question