i want to make a login class using MVC approach. First my html form code is in my view folder then i want authorizing code in controller class and the query and the connection are under my model class so
is it possible to call the method of one class into another class e.g (line no 11 in second tag correspond to line no 31 in third tag)
html form
In my controller class(check_log in.php)
model class
is it possible to call the method of one class into another class e.g (line no 11 in second tag correspond to line no 31 in third tag)
html form
Code:
<form method="post" action="check_login.php"> <table><tr><td>Name: <input type="text" name="Username" size="10" maxlength="40" /></td></tr> <tr><td>Password: <input type="password" name="Password" size="10" maxlength="10" /></td></tr> <tr><td><input type="submit" name="Submit" value="login" /></td></tr></table> </form>
In my controller class(check_log in.php)
Code:
class controller{ private $username; private $password; function_construct(); { $this->username=$_POST[Username]; $this->password=$_POST[Password]; require_once(../model/Loginsystem.php) $loginSystem = new LoginSystem(); if($loginSystem->doLogin($this->username,$this->password)) } }
model class
Code:
class LoginSystem { var $db_host, $db_name, $db_user, $db_password, $connection, $username, $password; /** * Constructor */ function LoginSystem() { require_once('settings.php'); $this->db_host = $dbhost; $this->db_name = $dbname; $this->db_user = $dbuser; $this->db_password = $dbpassword; } /** * Check username and password against DB * * @return true/false */ function doLogin($username, $password) { $this->connect(); $this->username = $username; $this->password = $password; // check db for user and pass here. $sql = sprintf("SELECT * FROM `admin` WHERE user = '$this->username' and Pass = '$this->password'", $this->clean($this->username), md5($this->clean($this->password))); $result = mysql_query($sql, $this->connection); // If no user/password combo exists return false if(mysql_affected_rows($this->connection) != 1) { $this->disconnect(); return false; } else // matching login ok { $row = mysql_fetch_assoc($result); // more secure to regenerate a new id. session_regenerate_id(); //set session vars up } $this->disconnect(); return true; } /** * Destroy session data/Logout. */ function logout() { unset($_SESSION['LoggedIn']); unset($_SESSION['userName']); session_destroy(); } /** * Connect to the Database * * @return true/false */ function connect() { $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Unable to connect to MySQL"); mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!"); // Valid connection object? everything ok? if($this->connection) { return true; } else return false; } /** * Disconnect from the db */ function disconnect() { mysql_close($this->connection); }
Comment