•  0

    Implement Keep Me Logged In feature using PHP, HTML, JQuery, Javascript and MySQL

      Asmin Jismun    4485        0        Report content

    In index.php file, use the code as shown below:

    <strong>Username</strong> :<br />
    <br />
    <strong>Password</strong> :<br />
    <br />
    <strong>Keep me logged in </strong>

    <?php if(isset($_GET['msg'])){ if($_GET['msg'] == 3){ echo 'Session and cookie has been deleted'; } } ?>




    if(isset($_POST['submit'])){ validateUsers(); }

    function validateUsers() {

    // get $result data, use CURL or Ajax or any other ways to validate the username and password // you also can do the matching username and password in database

       if ($result['status'] == 'success') {//login success

           $_SESSION["username"] = $_POST['username'];

           header('Location: http://localhost/keeplogin/success.php?msg=1');

           //Logged in using normal login


              onLogin($_POST['username']); } }

           else {//login failed

               echo "<div style='padding: 20px 0px;position:relative;top:20%;color:red;font-weight:bold;' > Login Failed </div>";




    In database.php file, set your database configuration as shown in example below:

    $servername = "localhost";
    $username = "root";
    $password = "admin";
    $dbname = "token_db";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);

    If the login success, it will generate a random token and stored it in database and also stored the token in your browser cookie as well. The next time user visit the login page again, it will check the username and password based on the token stored in cookie then redirect to login success page for example as shown below:

    <?php if(isset($_GET['msg'])){ if($_GET['msg'] == 1){ echo 'Logged in using normal login'; } else if ($_GET['msg'] == 2){ echo 'Logged in using the remember me feature'; } } ?>


    <?php session_start(); if(isset($_POST['logout'])){ $browser = ExactBrowserName(); include 'database.php'; $sql = "UPDATE token_table SET is_delete = 't' WHERE token_user = '".$_SESSION["username"]."' AND browser = '".$browser."' "; if ($conn--<query($sql) === TRUE) { //echo "New record created successfully"; } else { //echo "Error: " . $sql . "
    " . $conn->error; } $conn->close(); // remove all session variables session_unset(); // destroy the session session_destroy(); if(isset($_COOKIE['rememberme'])){ unset($_COOKIE['rememberme']); setcookie('rememberme', '', time() - 3600); // empty value and old timestamp } header('Location: http://localhost/keeplogin/index.php?msg=3');//Session and cookie has been deleted } function ExactBrowserName() { $ExactBrowserNameUA=$_SERVER['HTTP_USER_AGENT']; If (strpos(strtolower ($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "opr/")) { // OPERA $ExactBrowserNameBR="Opera"; } ElseIf (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower($ExactBrowserNameUA), "chrome/")) { // CHROME $ExactBrowserNameBR="Chrome"; } ElseIf (strpos(strtolower($ExactBrowserNameUA), "msie")) { // INTERNET EXPLORER $ExactBrowserNameBR="Internet Explorer"; } ElseIf (strpos(strtolower($ExactBrowserNameUA), "firefox/")) { // FIREFOX $ExactBrowserNameBR="Firefox"; } ElseIf (strpos(strtolower($ExactBrowserNameUA), "safari/") and strpos(strtolower ($ExactBrowserNameUA), "opr/")==false and strpos(strtolower($ExactBrowserNameUA), "chrome/")==false) { // SAFARI $ExactBrowserNameBR="Safari"; } Else { // OUT OF DATA $ExactBrowserNameBR="OUT OF DATA"; }; return $ExactBrowserNameBR; } ?>

    And if the login failed, you can redirect it to the page as shown below:

    echo "Login Failed";



Leave a Comment

Please Login to insert comment.


Facebook Conversations