PHP/Mysql authentication script getting NULL result from query -
i working on test project, trying learn more php , mysql , not. have decided build authentication engine test website having issues query.
basically, if run...
select * users name="tester" , passwd=md5('pass');
from mysql console, get...
+--------+---------------+----------------------------------+ | name | email | passwd | +--------+---------------+----------------------------------+ | tester | test@test.com | 1a1dc91c907325c69271ddf0c944bc72 | +--------+---------------+----------------------------------+ 1 row in set (0.00 sec)
however,when issue same query (at least think same) php code (see below) appear getting null result, authentication fails.
<?php include 'db_connect.php'; $username = $_post['username']; $passwd = $_post['passwd']; // protect mysql injection (more detail mysql injection) //$username = stripslashes($username); //$passwd = stripslashes($passwd); //$username = mysql_real_escape_string($username); //$passwd = mysql_real_escape_string($passwd); //encrypted password $secpasswd = md5($passwd); $sql="select * users name='$username' , passwd = $secpasswd"; $result=mysql_query($dbconnect,$sql); var_dump($result); // mysql_num_row counting table row $count=mysql_num_rows($result); //var_dump($count); // if result matched $myusername , $mypassword, table row must 1 row if($count==1){ // register $myusername, $mypassword , redirect file "login_success.php" session_register("username"); session_register("passwd"); header("location:login_success.php"); } else { echo "wrong username or password"; } ob_end_flush(); ?>
there s lot of problems code.
- you sql query format wrong
- you don't fetch values query result
- your hashing weak
- you aren't using prepared statements
- you using outdated session functions
here how have
<?php include 'db_connect.php'; //encrypted password $secpasswd = sha512($_post['passwd'].$_post['username']); $sql = "select * users name = ? , passwd = ?"; $stm = $pdo->prepare($sql); $stm->execute(array($_post['username'], $secpasswd)); $row = $stm->fetch(); if($row){ $_session["username"] = $row[username]; header("location:login_success.php"); exit; } else { echo "wrong username or password"; }
note code using pdo database interaction
Comments
Post a Comment