PHP Login Script

Discussion in 'PHP' started by miexl, Jul 27, 2009.

  1. #1
    I have a login script that coded in simple way. I just want to ask if this script is secured. here's the script

    
    
    <? 
    function login($username,$password) {
       $query = mysql_query("SELECT username,password FROM table WHERE username = '$username'  and password = '$password' ");
       if(mysql_num_rows($query) == 0) {
          return false;
       } else {
          return true;
       }
    }
    
    if(isset($_POST['submit'])) {
      $log = login($_POST['username'],$_POST['password']);
      if(!$log) echo "Invalid username or password"; 
    }
    
    ?>
    
    
    Code (markup):
    What do you think guys! is the code safe from any attacks?
     
    miexl, Jul 27, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Definitely not secure.

    You need to at a minimum escape your user input.

    $query = mysql_query("
    SELECT username,password
    FROM table
    WHERE username = '". mysql_real_escape_string($username) ."' and password = '". $password." ' ");

    You also should be storing your password as a hash. MD5 or SHA1. You will want to hash the password, with a salt before storing it.

    if(isset($_POST['submit'])) {
    $password = md5($_POST['password'].'NHU*E^%$SDMJN');
    $log = login($_POST['username'],$password);
    if(!$log) echo "Invalid username or password";
    }
     
    jestep, Jul 27, 2009 IP
  3. miexl

    miexl Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Great.. this is a big help..thanks jestep
     
    miexl, Jul 27, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    or you can use mysql's password() function.

    update blah set pass = password('what they typed') where id =nn

    select id from users where user = 'john' and pass = password('their pass') -> to authenticate it. this way you never store any pass in plain text as well and let mysql work it out.

    one bit of advice - avoid naming db fields 'password' as there's a function with that name, can get confusing in big queries :)
     
    dimitar christoff, Jul 27, 2009 IP
  5. miexl

    miexl Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #5
    how about a comment form or a registration form do i need a minimum escape also?
     
    miexl, Jul 27, 2009 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Escape or sanitize any user input no matter how insignificant. Also, if you ever use hidden fields, or variables that are set by user input, anything that could possibly be altered by user input, do the same with it.
     
    jestep, Jul 27, 2009 IP