jeudi 1 novembre 2018

i'm having a problem with adding a simple remember me function in php log in my page?

i'm trying to add a simple remember me function to my login.php code:

    <?php
    session_start();

    if(isset($_SESSION["comp"])){
        header("location view.php");//to redirect to the view page if already logged
        return;
        }

    if (isset($_POST['cancel'])) {
        // Redirect the browser to index.php
        header("Location: index.php");
        return;}


    /*
    if(isset($_POST['name'])){
    // to save the name for the next page
    }
    */

    $_SESSION['comp']=false;
    $salt = 'XyZzy12*_';
    $stored_hash = hash('md5', 'XyZzy12*_php123'); //the hash of the password php123 with some salt on it 


    if(isset($_POST['login'])){  //when login is pressed

        $check = hash('md5', $salt . $_POST['pwd']); // to store the inserted password and compare it with the one that already stored\

        if(strlen($_POST['name']) < 1 ||strlen($_POST['email']) < 1 ||strlen($_POST['name']) < 1 ){
            // if the inputs are empty redirect to login.php
            $_SESSION['error'] = "PLEASE don't leave any blank fields";
            }

        elseif($check != $stored_hash){
            $_SESSION['error'] = "incorrect password";
            error_log("Login failed with ".$_POST['email']." using the password ".$_POST['pwd']);
            //header("Location: login.php");
        }   
        else{
            $_SESSION['success'] = "you are logged in ";
            $_SESSION['comp']=true;
            $_SESSION['prs']  =  $_POST['name']; 

            error_log("Login success ".$_POST['email']);
            header("Location: view.php");
            //return;
        }
        if(isset($_POST["stay"])){$_SEESION['loged']=true;}
        }
    ?>


    <!--the view code-->
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body {
        height: 1000px;
        background: linear-gradient(to top, #ff0000 0%, #ffcc00 100%);

        margin: 0;
        padding: 0;
    }
    h1{
        color : #000000;
        text-align : center;
        font-family: "SIMPSON";
    }

    form {
        width: 300px;
        margin: 0 auto;

    }

    </style>
    <title>logging in ....</title>
    </head>

    <body>

    <div class="container">
    <h1> Log in please :</h1>
    <?php
        if ( isset($_SESSION['error']) ) {
            echo('<p align= "center" style="color: red;">'.htmlentities($_SESSION['error'])."</p>\n");
            unset($_SESSION['error']);
        }
        ?>

          <form method="POST" action="login.php" >
           <table>
           <tr>
             <td>Name :</td> <td><input type="text" maxlength="40" name="name"></td><br>
            </tr>
            <tr>
             <td>Email :</td> <td><input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" ></td><br>
            </tr>
            <tr>
             <td>Password :</td> <td><input type="password" name="pwd"></td><br>
            </tr>
            <tr>
            <td></td><td><input type="radio" name="stay" value="1"> remember me<br>
            </tr>
            <tr>
             <td></td> <td><input align ="center"  type="submit"  name="login" value="login"> <input align ="center" type="submit" name="cancel" value="Cancel"></td><br>
            </tr>

           </table>
          </form>

    </body>

</html>

so after entering the values and checking the radio box and submit to log in , and it should store some data in $_SESSION then the view page will be shown , but when i get back to the login.php page nothing is happening and the

header("location:view.php");

doesn't work . any one help me and tell me where i can do edits to make it work i'm a beginner in php and that is a simple training project




Aucun commentaire:

Enregistrer un commentaire