PHP login, is it secure? -
i hoping take @ first php login script , give me constructive criticism on may of done wrong , if secure. thank you.
i wasn't sure if had used password rehash correctly.
if (isset($_post['submit'], $_post['username'], $_post['password'])) { $username = null; if (isset($_post['username'])) $username = strip_tags(trim($_post['username'])); $password = null; if (isset($_post['password'])) $password = strip_tags(trim($_post['password'])); $sql = "select * login username=?"; $get = $connect->prepare($sql); $get->execute(array( $username )); // execute query if ($get->rowcount() === 1) { $row = $get->fetch(pdo::fetch_assoc); // fetch result $db_username = $row['username']; $db_password = $row['password']; if ((password_verify($password, $db_password)) && (strlen($username) >= 5) && (strlen($username) <= 10) && (strlen($password) >= 5) && (strlen($password) <= 12)) { if (password_needs_rehash($password, password_default)) { $hash = password_hash($password, password_default); $sql = "select * login username=?"; $get = $connect->prepare($sql); // use prepare prevent sql injection $sql = "update login set password=? username=?"; $statement = $connect->prepare($sql); $statement->execute(array( $hash, $username )); } $_session['auth'] = $db_username; session_regenerate_id(true); $sql = "update login set last_login=?, ip=? username=?"; $statement = $connect->prepare($sql); $statement->execute(array( $dt, $ip, $username )); $sql2 = "insert log (username,lastlogin,ip) values (:username,:lastlogin,:ip)"; $statement = $connect->prepare($sql2); $statement->execute(array(':username'=>$username, ':lastlogin'=>$dt, ':ip'=>$ip )); reloadpage(); } else { $loginmsg = 'wrong username / password'; } } else { $loginmsg = 'wrong username / password'; } }
no. 1 flaw
$username = null; if (isset($_post['username'])) $username = strip_tags(trim($_post['username'])); $password = null; if (isset($_post['password'])) $password = strip_tags(trim($_post['password']));
try
$username = null; if (isset($_post['username'])) { $username = strip_tags(trim($_post['username'])); } $password = null; if (isset($_post['password'])) { $password = strip_tags(trim($_post['password'])); }
no. 2 flaw
always use algo contants
along password_hash()
better usage.
if (password_needs_rehash($password, password_default)) { $cons = array('cost' => 12); $hash = password_hash($password, password_default, $cons); $sql = "select * login username=?";
all others seems ok me.
Comments
Post a Comment