I am beginner and this is my first project (happy face). I am trying to make log in systen to learn php and mySQL. At first everything was working so and then i decided to rewrite everything in Object oriented code and then it is not working. Since it is not syntax error it is hard to find for me, but i know that problem is in mySQL query, because it is empty.
Problem: It doesn't work and it doesn't set $errorLevel and there is no mysql_error() report
here is the class
<?php
class login
{
private $userName;
private $password;
private $queryResults; // will be used for checking user name and password
private $errorLevel;
/*return
* 1 if user is sucessfuly loged in
* -1 if user name is not in db
* -2 if cannot connect to db
*/
public function try_log_in($user_name, $password)
{
$this->setup_parameters($user_name, $password);
if($this->is_userName_in_db() && $this->is_password_correct())
{
$_SESSION["user"]=$this->userName;
return 1;
}
else
return $this->errorLevel;
}
//-------------------------Under the hud-----------------------//
function __construct()
{
}
//escapes all inputs and sets object`s parameters
private function setup_parameters($user_name, $password)
{
$this->userName = mysql_real_escape_string ($user_name);
$this->password = mysql_real_escape_string ($password);
$query = "
SELECT *
FROM users
WHERE userName = '$this->userName' ";
$this->queryResults = mysql_query($query);
}
/*Checks if given user name is db
* returns:
* 1 if it is in db
* -1 if it is not in db
* -2 if cannot connect to db
*/
private function is_userName_in_db()
{
if(!$this->queryResults)
$this->errorLevel = -2;
if(mysql_num_rows( $this->queryResults) == 0)
$this->errorLevel = -1;
else
return 1;
return $this->errorLevel;
}
/*returns
* 1 if entered password matches with one in db
* 0 if it doesn match
* -2 if could not nonnect to db;
*/
private function is_password_correct()
{
if($this->errorLevel == -2)//this should never happen
return $this->errorLevel;
$row = mysql_fetch_assoc($this->queryResults);// puts query results in array
$salt = substr ($row['password'], 0, strlen($row['password'])/2); //takes salt from query result
$hash = hash("sha256", $salt . $this->password);
$saltAndPassword = $salt . $hash;
if($saltAndPassword == $row['password'])
return 1;
else
return 0;
}
}
?>