1

how to create cookie or session that not expires on browser close i created some cookies and session they work till i close browser or logingout but i dont want like this i want the sessions and cookies remain if and only if
the users logout by theirself from website and this is login.php session and cookie after email and password verification become true:

$_SESSION['loggedIn'] = true;

$cookie_name = "user";
$cookie_value = "websitename";
setcookie($cookie_name, $cookie_value);
header("location:profile.php");

and this is what profile.php session:

if ( !$_SESSION['loggedIn'] ) {
    header("location: index.php");    
}

and this is what logout.php session and cookie:

session_unset();
session_destroy(); 
setcookie("user", "", time() - 3600);

also this is the index.php cookie and sesssion:

if(isset($_COOKIE['user'] ) || isset($_SESSION['loggedIn']) )
{
    header("location:profile.php");
}
SapuSeven
  • 1,473
  • 17
  • 30
Anonymous
  • 11
  • 3
  • 2
    Use a far future date. For example, set a cookie that expires in ten years: – B001ᛦ Aug 23 '18 at 12:28
  • Thanks @B001ᛦ But i think if i use cookie, i do not need to use sessions is it true? – Anonymous Aug 23 '18 at 13:35
  • But you know that sessions use cookies? Take a look at [cookies-vs-sessions](https://stackoverflow.com/questions/6253633/cookies-vs-sessions) – B001ᛦ Aug 23 '18 at 14:01
  • A session is meant only being valid for this single session. When working with session, first command should be `session_start()`. A cookie can have an expiration date. Do not forget to provide a path (I prefer "/") to make it available for subpaths, too. – Markus Zeller Aug 23 '18 at 14:23

0 Answers0