PHP problem - login the user with md5 hash.


  1. Posts : 134
    Windows 7 Ultimate
       #1

    PHP problem - login the user with md5 hash.


    Hello colleagues,


    Please help me! I have this little annoying problem. When I want to log the user in using PHP and MySQL everything goes OK, using plain text as password. The thing is, that I want the passwords my users enter into the MySQL database to be md5 hash encrypted so that if any evil user break into the database he wouldn't be able to see the passwords.

    So long story short, here is the code, and please tell me what I have done wrong and why the user can't log in using the md5 hash even tho they can register and the MySQL database receives the passwords md5 hashed.

    Script:


            
    // Registration process file
            
                 
    $con mysql_connect("localhost","root","");
                 
                 global 
    $con;
                 
                
    $nickname $_POST['nickname'];
                
    $password $_POST['password'];
                
    $email $_POST['email'];
                
    $name $_POST['name'];
                
                
    $password_hash md5($password);
                
                    if (!
    $con)
                      {
                      die(
    'Could not connect: ' mysql_error());
                      }

                    
    mysql_select_db("first_database");

                    
    $sql="INSERT INTO users (username, password, firstname, email)VALUES('$nickname','$password_hash','$name', '$email')";

                    if (!
    mysql_query($sql,$con))
                      {
                      die(
    'Error: ' mysql_error());
                      }
                    echo 
    "1 record added";

                    
    mysql_close($con
    And here is the login process file.

    Script:
                <?php
                
                
                
    require 'mysql.php';
                            
                
                
                
    $nickname $_POST['nickname'];
                
    $password $_POST['password'];
                
                
    $password_hash md5($password);
                if(!empty (
    $nickname) and !empty ($password)){
                
                
                
    $query "SELECT id FROM users WHERE username='$nickname' AND password='$password_hash'";
                
                
                if(
    $query_run mysql_query($query)){
                
                
    $mysql_num_rows mysql_num_rows($query_run);
                
                if(
    $mysql_num_rows==0){
                
                echo 
    'Password/username error!';
                
                
                
                
                }else if(
    $mysql_num_rows==1){
                
                
    $user_id mysql_result($query_run0'id');
                
    $_SESSION['user_id']=$user_id;
                
    header('Location: index.php');
                
                echo 
    'You are now logged in!';
                
                }
                }
                
                
                
                
                
                }
                
                
                
                
                
                
                
    ?>
    This is the root account of my local server.
    Thank you very much!!

    Best Regards
    Stefany
      My Computer


  2. Posts : 51,479
    Windows 11 Workstation x64
       #2

    You would probably be better asking in a forum aimed at that sort of thing, maybe - PHP Forum
      My Computers


  3. Posts : 134
    Windows 7 Ultimate
    Thread Starter
       #3

    Thank you
      My Computer


  4. Posts : 44
    Windows 7 Pro x86
       #4

    There's no polite way to put this... Your scripts are a serious disaster area just waiting for an SQL injection. If you learned this stuff from a book then throw it away. If you learned it from a website then delete the bookmark.

    You really need to do some reading to know why virtually everything in those scripts is bad bad bad.

    Start here: PHP: SQL Injection - Manual

    If you go elsewhere for help with this and don't get told the same thing then take whatever advice you've been given as being wrong.

    Use PDO and prepared statements: PHP: PDO - Manual

    Also, don't use MD5() ; use crypt() and learn about salting your hashes.

    Expect that once you've learned how to work securely with your database, your code will probably have other issues.

    https://www.owasp.org/index.php/Cate...op_Ten_Project
      My Computer


  5. Posts : 134
    Windows 7 Ultimate
    Thread Starter
       #5

    @murmatron Thank you very much for the information, don't worry I think you were very polite because you told me what I have done wrong, thank you very much again. 1 more question please. The crypt() tag, is it the same as md5 hash or a better way to do the hashing?

    Thank you again!
      My Computer


  6. Posts : 44
    Windows 7 Pro x86
       #6

    PHP: crypt - Manual

    crypt is similar to md5 (it produces a hash) except you can choose a different (better) algorithm and a salt string.
      My Computer


  7. Posts : 134
    Windows 7 Ultimate
    Thread Starter
       #7

    Thank you very much murmatron, much appriciated!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:40.
Find Us