SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member jayminyoung's Avatar
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up encrypting a password field with PHP in MYSQL

    Hi all,
    First of all I am new to PHP and this forum hope I am in the correct area.
    What I want to do is have a HTML form with a password field.
    Then PHP encrypts this entry with MD5 so it can be submitted to a MYSQL database.
    So when anyone looks up the entry stored in the database it will be encrypted and unreadable.
    JayminYoung

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Welcome to SitePoint JayminYoung.

    MySQL has a built in SHA1 function you can use here, it's advisable to use this over MD5.

    PHP Code:
    <?php
    $sql 
    sprintf(
      
    "INSERT INTO table (username, password) VALUES ('%s', SHA1('%s'));",
      
    mysql_real_escape_string($_POST['username']),
      
    mysql_real_escape_string($_POST['password'])
    );

    $res mysql_query($sql);
    If any of the functions I've used above are new to you, check out sprintf, mysql_real_escape_string and mysql_query.

    You can then find this user with the following bit of complementary code:

    PHP Code:
    $sql sprintf(
      
    "SELECT username FROM table WHERE username = '%s' AND password = SHA1('%s') LIMIT 1;",
      
    mysql_real_escape_string($_POST['username']),
      
    mysql_real_escape_string($_POST['password'])
    );

    $res mysql_query($sql); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member jayminyoung's Avatar
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AnthonySterling,
    thank you for your quick reply, I will have a try and see how it works


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •