Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2019
    Thanked 0 Times in 0 Posts

    How do I keep a user logged in even if I close the browser.

    <?php require 'connections.php'; ?>
        	if(isset ($_POST["rememberMe"]))
        		setcookie("e_mail2",$_POST["e_mail1"],time()+ 60 * 60 * 24 * 365);
        		ini_set('session.cookie_lifetime', 60 * 60 * 24 * 365);
        		ini_set('session.gc-maxlifetime', 60 * 60 * 24 * 365);
                $e_mail2 = $_POST['e_mail1'];
                $password2 = $_POST['password1'];
                $result = $con->query("select * from user where e_mail='$e_mail2'");
                $row = $result->fetch_array(MYSQLI_BOTH);
                if(password_verify($password2, $row['password'] )){
                	$_SESSION["user_id"] = $row['user_id'];
                		header('Location: welcome.php');
                		$_SESSION["LogInFail"] = "Yes";
    <!doctype html>
    <link href="http://communications.x10host.com/Master.css" rel="stylesheet" type="text/css" />
    <link href="http://communications.x10host.com/Menu.css" rel="stylesheet" type="text/css" />
    <meta charset="utf-8">
    <div class="Container">
             <div class="Header"></div>
             <div class="Menu">
             <div id="Menu">
             <ul class="cssmenu">
    <div class="Container" id="tabs31">
    	<li><a href="http://communications.x10host.com/login.php" title=""><span>Home</span></a></li>
    	<li><a href="#" title=""><span>About Us</span></a></li>
    	<li><a href="#" title="" class="current"><span>Services</span></a></li>
    	<li><a href="#" title=""><span>Our Work</span></a></li>
    	<li><a href="http://communications.x10host.com/create-account.php" title=""><span>Create account</span></a></li>
    	<li><a href="#" title=""><span>Contact Us</span></a></li>
             <div class="LeftBody"></div>
             <div class="RightBody">
             <form name="login-form" method="post" action="">
             <label for="e_mail1">  E-mail: </label>
             <div class="FormElement">
             <input name="e_mail1" type="email" required="required" class="editText" id="e_mail1" value="<?php  echo $_COOKIE["e_mail2"]?>" />
             <label for="password1">  Password: </label>
             <div class="FormElement">
             <input name="password1" type="password" required="required" class="editText" id="password1">
             <input type="checkbox" name="rememberMe" value="rememberMe" />
             <?php if(isset($_SESSION["LogInFail"])){?>
            <span style='color:red;'>The username or email or password you enter does not match or exist please try again.</span>
            <?php } ?>
             <div class="FormElement">
             <input name="login" type="submit" class="button" id="login" value="Submit">
             <div class="Footer"></div>
    Last edited by vinyl-junkie; Feb 4th, 2019 at 08:35 AM. Reason: added code tags

  2. #2
    Master Coder
    Join Date
    Feb 2011
    Your Monitor
    Thanked 623 Times in 609 Posts
    By default, PHP sessions last something like 21 minutes - might be 24, can't remember. Anyway, faffing about with the cookie expiry won't really help, once PHP has decided the session is done then that's it - it's blackholed.

    What I did was to modify a php session handler that i found php.net i think - PHP: session_set_save_handler - Manual) which allows you to store sessions in the database instead. It has custom event handlers which means you can ignore the code that checks the time and just pull it straight out of the database regardless. What i did was just remove the code in the destroy function so that php can call it but nothing happens. Everything other function (eg open) just reads from the database instead. As nothing gets destroyed you can keep the sessions pretty much forever. That by itself is kind of a bad idea so you could instead use a check for anything that hasn't been used for ages. I have a table with a created, modified and closed column. If it's got a time recorded in closed then obviously my class won't pull it out. If it's got a modified date younger than 4 weeks then that's fair game and the SQL selects it by the session id.

    Hope that helps..
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts