How To Tell If a Session Is New


How To Tell If a Session Is New? - PHP Script Tips - Understanding and Using Sessions



There is not direct way to tell if a session is new or old. But you can design your site to have a required session value in all sessions. Then you can check the existence of this value in a session to determine if it is a new session by isset($_SESSION['name']).

Let's say you decided to have a required session value called "Status" with two possible values: "Guest" and "Registered". The landing script of your site should look like:

  if (!isset($_SESSION['Status'])) {
    $_SESSION["Status"] = "Guest";
    print("Welcome to!\n");
    print("  <a href=login.php>Login</a>\n");
    print("  <a href=guest_home.php>Stay as a guest</a>\n");
  } else {
    if ($_SESSION["Status"] == "Guest") {
      header( 'Location: http://localhost/guest_home.php');
    } else if ($_SESSION["Status"] == "Registered") {
      header( 'Location: http://localhost/home.php');

2007-04-17, 5076👍, 0💬