dimanche 18 octobre 2020

php login with $_SESSION

i am making a login form with sessions this is my index.php page code:

<?php
    include "../config/MainClass.php";
    include "../connection.php";
    session_start();
    if (empty($_SESSION['admin'])){
        echo "<script>location='../login.php'</script>";
    }
    if (isset($_GET['logout'])){
        unset($_SESSION['admin']);
        echo "<script>location='../login.php'</script>";
    }
    if (isset($_GET['success'])) {
        if ($_GET['success']==1) {
            echo "<script>alert('Record Deleted Successfully');</script>";
        }
        
    }
    if (isset($_GET['success1'])) {
        if ($_GET['success1']==1) {
            echo "<script>alert('Record Updated Successfully');</script>";
        }
        
    }
?>

(thats the bit with the error) and this is my login page code:

<!DOCTYPE html>
<html>

<?php
    include "config/MainClass.php";
?>

<?php if (isset($_POST['SubBtn'])) {
    $to = $use->login($_POST['u'], $_POST['pw'], $_POST['em']);
    if ($to == 0) {
        ?>
        <script type="text/javascript">alert('Undefined user')</script>
        <meta http-equiv="refresh" content="0; ./login.php">
    <?php } else {
        echo "<script>location='./dashboard/';</script>";
    }
} ?>


<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Login Form</title>
    <link rel="stylesheet" href="asset/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="asset/fonts/ionicons.min.css">
    <link rel="stylesheet" href="asset/css/Login-Form-Dark.css">
    <link rel="stylesheet" href="asset/css/styles.css">
</head>

<body>
    <div class="login-dark">
        <form method="POST">
            <h2 class="sr-only">Login Form</h2>
            <div class="illustration"><i class="icon ion-ios-locked-outline"></i></div>
            <div class="form-group"><input class="form-control" type="name" name="u" autocomplete="off" placeholder="Username"></div>
            <div class="form-group"><input class="form-control" type="k" name="em" autocomplete="off" placeholder="Email"></div>
            <div class="form-group"><input class="form-control" type="password" name="pw" autocomplete="off" placeholder="Password"></div>
            <div class="form-group"><button class="btn btn-primary btn-block" type="submit" name="SubBtn">Login</button></div><a class="forgot" href="signup.php">Dont have an account? Sign Up!</a></form>
    </div>
    <script src="asset/js/jquery.min.js"></script>
    <script src="asset/bootstrap/js/bootstrap.min.js"></script>
</body>

</html>

and the main class one is:

<?php
session_start([1]);

class MainClass
{
    function __construct()
    {

        
        $host_name = 'aaaaaaaaaaaa';
           $database = 'aaaaaaaaaaaaaa';
           $user_name = 'aaaaaaaaaaaaa';
           $password = 'aaaaaaaaaa';


        
        try {
            $this->db = new PDO('mysql:host=aaaaaaaaaaaaaaa;dbname=aaaaaaaaaaaa', $user_name, $password);
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
        
    
    }

    function login($user, $pw, $em)
    {

        $to = $this->db->query("SELECT * FROM users where username='$user' AND password='$pw' AND email='$em' ");
        if ($to->rowCount() > 0) {
            $_SESSION['admin'] = $to->fetch(PDO::FETCH_ASSOC);
            return 1;
        } else {
            return 0;
        }

    }

}



$use = new MainClass();

and it all works but when you login it redirects you to the index.php page but instantly redirects you back? it meant to only redirect you if your not logged in.

Anyone know how to fix this??

Aucun commentaire:

Enregistrer un commentaire