PHP Sessions Login with Remember Me

PHP remember me functionality works after pressing log in

The navbar code is not checking if remember cookie is set. That's why even if you are logged in via the cookie, the page renders you're not.

Add to the navbar code the below code. Add it just after require_once 'connect.php';

if( isset($_COOKIE['remember_me']) AND trim($_COOKIE['remember_me'] ) != "" ) {
$_SESSION['user'] = $_COOKIE['remember_me'];
}

Also in navbar code, to treat empty $_SESSION['user'] as not logged:

Replace:

<?php if (!isset($_SESSION['user'])): ?>

With:

<?php if (!isset($_SESSION['user']) OR trim( $_SESSION['user'] ) == ""): ?>

New Code:

<?php
require_once 'connect.php';

if( isset($_COOKIE['remember_me']) AND trim($_COOKIE['remember_me'] ) != "" ) {
$_SESSION['user'] = $_COOKIE['remember_me'];
}
?>

<h1 style="width:50%; margin:0 auto; font-size: 50px; font-family: 'Raleway', sans-serif; color: black; font-weight: bold;">Quality House Beer</h1>
<nav>
<a href="index.php"><img src="images/logoNew_bubbles.png"></a>
<ul>
<?php if (!isset($_SESSION['user']) OR trim( $_SESSION['user'] ) == ""): ?>
<li><a id="home" href="index.php"><i class="fa fa-home"></i> HOME</a></li>
<li><a id="catalog" href="catalog.php"><i class="fa fa-list"></i> CATALOG</a></li>
<li><a id="about" href="about.php"><i class="fa fa-pencil"></i> ABOUT</a></li>
<li><a id="register" href="register.php"><i class="fa fa-user"></i> REGISTER</a></li>
<li><a id="login" href="login.php"><i class="fa fa-sign-in"></i> LOGIN</a></li>
<li><a id="faq" href="faq.php"><i class="fa fa-question"></i> FAQ</a></li>
<li><a href="#"><i class="fa fa-search"></i> <input
style="box-sizing: border-box; border: 2px solid; border-radius: 15px; height: 40px;"
type="text" placeholder=" Search..." name="search"></a></li>

<?php elseif (isset($_SESSION['user']) && $_SESSION['user'] == 'admin'): ?>
<li><a id="home" href="index.php"><i class="fa fa-home"></i> HOME</a></li>
<li><a id="catalog" href="catalog.php"><i class="fa fa-list"></i> CATALOG</a></li>

<li><a id="addBeer" href="create.php"><i class="fa fa-beer"></i> Add Beer</a></li>
<li><a id="settings" href="addBeer.php"><i class="fa fa-beer"></i> SETTINGS</a></li>
<li><a id="logout" href="logout.php"><i class="fa fa-sign-out"></i> LOGOUT</a></li>
<li><a href="#"><i class="fa fa-search"></i> <input
style="box-sizing: border-box; border: 2px solid; border-radius: 15px; height: 40px;width: 220px;"
type="text" placeholder=" Search..." name="search"></a></li>
<li><a id="profile" href="profile.php"><i class="fa fa-user"></i> <?= $_SESSION['user'] ?></a></li>
<?php else :; ?>
<li><a id="home" href="index.php"><i class="fa fa-home"></i> HOME</a></li>
<li><a id="catalog" href="catalog.php"><i class="fa fa-list"></i> CATALOG</a></li>
<li><a id="about" href="about.php"><i class="fa fa-pencil"></i> ABOUT</a></li>
<li><a id="basket" href="basket.php"><i class="fa fa-beer"></i> BASKET</a></li>
<li><a id="faq" href="faq.php"><i class="fa fa-question"></i> FAQ</a></li>
<li><a id="logout" href="logout.php"><i class="fa fa-sign-out"></i> LOGOUT</a></li>
<li><a href="#"><i class="fa fa-search"></i> <input
style="box-sizing: border-box; border: 2px solid; border-radius: 15px; height: 40px;width: 300px;"
type="text" placeholder=" Search..." name="search"></a></li>
<li><a id="profile" href="profile.php"><i class="fa fa-user"></i> <?= $_SESSION['user'] ?></a></li>
<?php endif; ?>
</ul>
</nav>

Proper way to use Remember me functionality in PHP

But I really confused about my main problem: which way is proper, for "remember me" feature? to use cookies/session/database?

Http is a stateless protocall. Authentication token must persist to keep the state.
Proper way is to use session. Now how do you track the session? It's up to you. But cookies are not bad.

In the session you can save a hash created from browser different criteria(user agent, os, screen resolution etc) to check if the token is from same environment. The more criteria you save the more itll be harder to hijack. Btw you need JavaScript to grab ths extra information every time.

remember me checkbox in login using cookie in codeignitor

firstly you have to include cookie helper as I mention in the comment section

After that in your controller

public function loginaction()
{
$this->load->helper('cookie');
$email=$this->input->post('email');
$password=$this->input->post('password');
$where = array('email'=>$email,'password'=>$password);
$tbname='login';
$query = $this->Insert_Model->viewdata($tbname,$where);

if(empty($query))
{
$data['msg']="Invalid email or password";
$this->load->view('login',$data);
}
else
{
//first you have to delete old cookie and create new one
delete_cookie("email");
delete_cookie("password");
if ($this->input->post('remember') == 'true') {

$userName = array(
'name' => 'email',
'value' => YOUREMAIL,
'expire' => '86500',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($userName);

$password = array(
'name' => 'password',
'value' => YOURPASSWORD,
'expire' => '86500',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($password);
}
redirect('dashboardv1');
}
}

Get the cookie you can use below code

<?php echo get_cookie('email'); ?>
<?php echo get_cookie('password'); ?>


Related Topics



Leave a reply



Submit