Post: [PHP] Secure Log In [Help]
07-10-2015, 12:30 AM #1
Passion
League Champion
(adsbygoogle = window.adsbygoogle || []).push({});
Hello NGU Tustin

Can anyone help me with creating a secure login that people can't 'crack' .

I've used the MD5 method and people are still able to 'crack' my passwords.. Sal

Much appreciated Beachington
Last edited by Passion ; 03-10-2016 at 08:54 PM.
07-10-2015, 12:42 AM #2
Tustin
Balls of Steel
Originally posted by Passion View Post
Hello NGU Tustin

Can anyone help me with creating a secure login that people can't 'crack' .

I've used the MD5 method and people are still able to 'crack' my site.. Sal

Much appreciated Beachington

What do you mean 'crack'? How are they getting into your database to begin with? If you're just talking about a secure way of storing passwords, upgrade to PHP 5.5+ and use password_hash and password_verify. It uses BCrypt (aka Blowfish) with a random salt. It's the best you can get and it's super easy to set up. If you need help just quote back and I can provide an example.
07-10-2015, 02:39 AM #3
Passion
League Champion
Originally posted by Tustin View Post
What do you mean 'crack'? How are they getting into your database to begin with? If you're just talking about a secure way of storing passwords, upgrade to PHP 5.5+ and use password_hash and password_verify. It uses BCrypt (aka Blowfish) with a random salt. It's the best you can get and it's super easy to set up. If you need help just quote back and I can provide an example.


They get into my database.

And i'd like an example :y:
07-10-2015, 03:01 AM #4
Tustin
Balls of Steel
Originally posted by Passion View Post
They get into my database.

And i'd like an example :y:

If they keep getting into your database, you're probably not sanitizing their inputs properly, thus leading to sql injection. At the very least, you need to use mysql_real_escape_string or just go with the most secure way which is PDO with prepared statements: You must login or register to view this content..

Here's a creating account example using password_hash and PDO with prepared statements:
    
<?php
//you probably should create an include file with this pdo object creation so you don't need to put this code in every file
$db_host = "localhost";
$db_user = "myuser";
$db_password = "mydbpass";
$database_name = "mydbtable";

//our pdo object
$pdo = new PDO("mysql:host=".$db_host.";dbname=".$database_name, $db_user, $db_password);

//no need to sanitze their inputs since we're using prepared statements
$username = $some_post_variable_with_username;
$password = $some_post_variable_with_password;

$q = "SELECT * FROM users WHERE username = :name";
$query = $pdo->prepare($q);
$query->execute( array( 'name'=>$username) );
if ($query->rowCount() == 0) //no user found, create...
{
$password_hash = password_hash($password, PASSWORD_BCRYPT);
$q = "INSERT INTO users(username, password) VALUES Upside Down Happyname,:pass)";
$query = $pdo->prepare($q);
$query->execute( array( 'name'=>$username, 'pass'=>$password_hash) ); //ALWAYS store the hash, not the plaintext!!
}
else
{
//user exists. throw some error here
}

?>


Now that you've stored the password, here's how to check it:
    
<?php
//you probably should create an include file with this pdo object creation so you don't need to put this code in every file
$db_host = "localhost";
$db_user = "myuser";
$db_password = "mydbpass";
$database_name = "mydbtable";

//our pdo object
$pdo = new PDO("mysql:host=".$db_host.";dbname=".$database_name, $db_user, $db_password);

//no need to sanitze their inputs since we're using prepared statements
$username = $some_post_variable_with_username;
$password = $some_post_variable_with_password;

$q = "SELECT * FROM users WHERE username = :name";
$query = $pdo->prepare($q);
$query->execute( array( 'name'=>$username) );
if ($query->rowCount() > 0) //found a user with the name
{
$pass_hash = $query->fetch()['password']; //the hash from the db
if (password_verify($password, $pass_hash)) //checks if they match
{
//they match!! =D
}
else
{
//no match, throw some error or use some login limit to prevent bruteforce
}
}
else
{
//user doesn't exist, throw some error
}

?>


Hopefully it's not too complex for you. This is what I use and it's really great. PDO with prepared statements is technically (as of now) impossible to exploit with standard sqli techniques. This might have some syntax errors as I just wrote this up real quick without testing.

If the code looks like shit for you, copy and paste it into a text editor to see it better :p.

The following user thanked Tustin for this useful post:

Passion
07-10-2015, 03:29 AM #5
Passion
League Champion
Originally posted by Tustin View Post
If they keep getting into your database, you're probably not sanitizing their inputs properly, thus leading to sql injection. At the very least, you need to use mysql_real_escape_string or just go with the most secure way which is PDO with prepared statements: You must login or register to view this content..

Here's a creating account example using password_hash and PDO with prepared statements:
    
<?php
//you probably should create an include file with this pdo object creation so you don't need to put this code in every file
$db_host = "localhost";
$db_user = "myuser";
$db_password = "mydbpass";
$database_name = "mydbtable";

//our pdo object
$pdo = new PDO("mysql:host=".$db_host.";dbname=".$database_name, $db_user, $db_password);

//no need to sanitze their inputs since we're using prepared statements
$username = $some_post_variable_with_username;
$password = $some_post_variable_with_password;

$q = "SELECT * FROM users WHERE username = :name";
$query = $pdo->prepare($q);
$query->execute( array( 'name'=>$username) );
if ($query->rowCount() == 0) //no user found, create...
{
$password_hash = password_hash($password, PASSWORD_BCRYPT);
$q = "INSERT INTO users(username, password) VALUES Upside Down Happyname,:pass)";
$query = $pdo->prepare($q);
$query->execute( array( 'name'=>$username, 'pass'=>$password_hash) ); //ALWAYS store the hash, not the plaintext!!
}
else
{
//user exists. throw some error here
}

?>


Now that you've stored the password, here's how to check it:
    
<?php
//you probably should create an include file with this pdo object creation so you don't need to put this code in every file
$db_host = "localhost";
$db_user = "myuser";
$db_password = "mydbpass";
$database_name = "mydbtable";

//our pdo object
$pdo = new PDO("mysql:host=".$db_host.";dbname=".$database_name, $db_user, $db_password);

//no need to sanitze their inputs since we're using prepared statements
$username = $some_post_variable_with_username;
$password = $some_post_variable_with_password;

$q = "SELECT * FROM users WHERE username = :name";
$query = $pdo->prepare($q);
$query->execute( array( 'name'=>$username) );
if ($query->rowCount() > 0) //found a user with the name
{
$pass_hash = $query->fetch()['password']; //the hash from the db
if (password_verify($password, $pass_hash)) //checks if they match
{
//they match!! =D
}
else
{
//no match, throw some error or use some login limit to prevent bruteforce
}
}
else
{
//user doesn't exist, throw some error
}

?>


Hopefully it's not too complex for you. This is what I use and it's really great. PDO with prepared statements is technically (as of now) impossible to exploit with standard sqli techniques. This might have some syntax errors as I just wrote this up real quick without testing.

If the code looks like shit for you, copy and paste it into a text editor to see it better :p.


Thanks man Happy

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo