User authentication PHP mySQL not working -
so building user authentication system using php , mysql in xampp.
i have managed recognize if user exists email address, other functions don't seem working. example check if user has activated account or not comes haven't if change active status 1 in database. or login function if both email , password correct incorrect.
here login.php script
<?php include 'init.php'; function sanitize($data){ return mysql_real_escape_string($data); } //check if user exists function user_exists($email){ $email = sanitize($email); //$query = mysql_query("select count('id') 'register' 'email' = '$email'"); return (mysql_result(mysql_query("select count(id) register email = '$email'"),0) == 1)? true : false; } //check if user has activated account function user_activate($email){ $email = sanitize($email); //$query = mysql_query("select count('id') 'register' 'email' = '$email'"); return (mysql_result(mysql_query("select count(id) register email = '$email' , 'active' =1"),0) == 1)? true : false; } function user_id_from_email($email){ $email = sanitize($email); return (mysql_result(mysql_query("select id register email = '$email'"),0,'id')); } function login($email,$password){ $user_id = user_id_from_email($email); $email = sanitize($email); $password = md5($password); return (mysql_result(mysql_query("select count(id) register email = '$email' , 'password' ='$password'"),0) == 1)? $user_id : false; } if(empty($_post)=== false){ $email = $_post['email']; $password = $_post['password']; } if(empty($email)|| empty($password) === true){ $errors[] = "you must enter username , password"; } else if(user_exists($email) === false){ $errors[] = "email address not registered"; } else if(user_activate($email) === false){ $errors[] = "you haven't activated account yet"; } else{ $login = login($email, $password); if($login === false){ $errors[] = "email/password incorrect"; } else { echo "ok"; } } print_r($errors); /*$email = $_post['email']; $password = $_post['password']; if($email&&$password){ $connect = mysql_connect("localhost","root","") or die ("couldn't connect"); mysql_select_db("users") or die("couldn't find database"); } else die("please enter username , password"); $query = mysql_query("select * register email = '$email'"); $numrows = mysql_num_rows($query); echo $numrows;*/ ?>
my database called 'users'
, @ moment has 1 table called 'register'
. rows: id, firstname, lastname, email, password, , active
.
in function login, try remove quotes ' arround field name password. or prefer use 1 ` .
and take care, using function mysql_result , mysql_query both no longer supported in php 7.0
as can see here : http://php.net/manual/en/function.mysql-query.php http://php.net/manual/en/function.mysql-result.php
Comments
Post a Comment