Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app
- -lib
- -config.php
- -database.php
- -auth.php
- -user.php
- -index.php
- ------------------------------------------------------------------------------------------------
- database.php
- <?php
- require_once('config.php');
- class MySQLDatabase{
- private $connection;
- public function __construct(){
- $this->connect_database();
- }
- public function connect_database(){
- $this->connection = mysql_connect("localhost", "root", "password");
- if (!$this->connection){
- die("Cannot connect to database " . mysql_error());
- } else {
- $select_db = mysql_select_db('portal');
- if(!$select_db){
- die("Error in selecting database " . mysql_error());
- }
- }
- }
- public function query($sql){
- return mysql_query($sql, $this->connection);
- }
- public function fetch_array($result_set){
- return mysql_fetch_array($result_set);
- }
- public function num_rows($result_set){
- return mysql_num_rows($result_set);
- }
- public function close_connection(){
- if(isset($this->connection)) {
- mysql_close($this->connection);
- unset($this->connection);
- }
- }
- }
- $database = new MySQLDatabase();
- ---------------------------------------------------------------------------------------------
- user.php
- <?php
- require_once('database.php');
- class User {
- public function insert($tablename, $username, $password, $email_address){
- $result = $this->query("INSERT INTO $tablename(username, password, email_address) VALUES('$username','$password','$email_address')");
- if($result){
- return TRUE;
- } else {
- return FALSE;
- }
- }
- }
- ----------------------------------------------------------------------------------------------
- auth.php
- <?php
- require_once('database.php');
- class Auth extends MySQLDatabase{
- public static function authenticate_user($username, $password){
- global $database;
- $sql = "SELECT username, password FROM users ";
- $sql .= "WHERE username ='" . $username ."' ";
- $sql .= "AND password ='" . $password ."'";
- $result_set = $database->query($sql);
- if(!$result_set){
- die(mysql_error());
- }
- if($database->num_rows($result_set) == 1){
- return TRUE;
- }
- return FALSE;
- }
- }
- $auth = new Auth();
- ---------------------------------------------------------------------------------------------
- index.php
- <?php
- require_once('lib/config.php');
- require_once('lib/database.php');
- require_once('lib/auth.php');
- require_once('lib/user.php');
- if($_POST['submit']){
- $check_auth = $auth->authenticate_user($_POST['username'] ,$_POST['password']);
- if($check_auth){
- echo "welcome";
- }
- }
- ?>
- <form method="post" action="index.php" >
- <fieldset>
- <legend accesskey="l">Login</legend>
- <label for="username">Username :</label>
- <input type="text" name="username" />
- <label for="password">Password :</label>
- <input type="password" name="password" />
- <input type="submit" value="login" name="submit" />
- </fieldset>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement