Search code snippets, questions, articles...

login OOP MVC PDO

<?php
//loginController.class.php

class loginController extends Model{

    public function indexUser($username, $password){
        $username = $_POST['username'];
        $password = $_POST['password'];

        if(!empty($username) AND !empty($password)){
            $user = new Model();

            $credentialsAreValid = $user->CheckCredentials($username, $password);

            if($credentialsAreValid){

                $_SESSION['username'] = $username;
                echo "<script>alert('You are logged in )</script>";
                echo "<script>window.open('admin_home.php','_self')</script>";
                
            }
        }          
    }
}
?>
<?php
//model.class.php

class Model extends Dbh {

    protected function CheckCredentials($username, $password) {
    
        $sql = "SELECT * FROM admin_users WHERE username = ? && password = ?";
        $stmt = $this->connect()->prepare($sql);
        $stmt->bindvalue(1, $username);
        $stmt->bindvalue(2, $password);
        $result = $stmt->execute();
        
        $results = $stmt->fetchAll();        
        return $results;

    }

}

?>
<?php

                  if(!isset($_SESSION['username']) AND !isset($_GET['login'])){

                    echo "<a href='?login'>Admin Login</a>";
                    
                  }elseif(!isset($_SESSION['username']) && isset($_GET['login'])){
                      
                    echo "<form action=" . $_SERVER['PHP_SELF'] . " method='post'>";
                    echo "<input type='text' name='username' placeholder='Username'>";
                    echo "<input type='text' name='password' placeholder='Password'>";
                    echo "<input type='submit' name='submit_admin' placeholder='Admin Login'>";
                    echo "</form>";

                  }else{  
                   
                    echo "Hi " . $_SESSION['username'] . " Welcome back.";
                    echo "<a href='logout.php'>Log Out</a>";
                  
                  }

                  if(isset($_POST['submit_admin'])){
                    $username = $_POST['username'];
                    $password = $_POST['password'];
    
                    $user = new loginController();
                    $user->indexUser($username,$password);

                  }
                  
                ?>
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

Search Index Data (The code snippet can also be found with below search text)

login
MVC
Was this helpful?
0 Comments