Hi,
I have just started a new project but thought it would be a good idea to learn how to develop the system using objects.
The files...
index.php
/includes/header.php
/includes/footer.php
/includes/class.php
/action/check_login.php
On the index page I have a login form that posts the data over to check_login.php...
<?php
include_once("../includes/class.php");
$user = new login();
$user->check_login($_POST['email'],$_POST['password']);
header("location:/");
?>
Which runs the functions included from class.php...
<?php
include_once("config.php");
class login {
// Check login return true on correct login
var $return;
function check_login($email,$password){
// Check user table with username/password to validate.
$email = mysql_escape_string($email);
$password = md5(mysql_escape_string($password));
$query = mysql_query("SELECT * FROM ".TABLE_LOGIN." WHERE `email`='$email' AND `password`='$password'");
$row = mysql_fetch_array($query);
if(mysql_num_rows($query) == "1"){
$this->return = "Success";
$this->set_session("1",$row[id],$row[name],$row[email]);
}elseif(mysql_num_rows($query) > "1"){
$this->return = "more than one row found";
$this->unset_session();
}else{
$this->return = "row not found";
$this->unset_session();
}
}
function login_return(){
if(!empty($this->return)){
return "<h1>".$this->return."</h1>";
}
}
function set_session($success,$id,$name,$email){
$_SESSION['user']['success'] = $success;
$_SESSION['user']['id'] = $id;
$_SESSION['user']['name'] = $name;
$_SESSION['user']['email'] = $email;
}
function unset_session(){
unset($_SESSION['user']);
}
}
?>
Am I running in the right direction?? Is it right to have a seperate page to initiate each function (such as check_login.php). This works - as in the session is set containing the data from the query.
I would like to echo 'login_return' which as you can see contains any errors on the index.php page. This will work fine on the page that the function runs on (check_login) however i dont understand how the data can be returned after the redirect.
echo $user->login_return();