Home>

I want to redirect to a specific page only when accessing the website for the first time.
Also, since the behavior varies depending on the browser, we want to unify it.

I am creating a process using a PHP session and I want the following behavior.

  1. Redirect to specific page at first access
  2. Make transitions to other pages as normal
  3. Redirect to a specific page when revisiting (If i have more than 15 minutes available for convenience)

For English-speaking countries, it is created because it wants to distribute the landing page by determining the browser setting language, but there is also a place where it is reasonable to use the session in the first place, If i have a suggestion, I would appreciate any advice.

For information on how to identify/redirect access from overseas, please refer to this page.
As I was keen on why I didn't choose .htaccess or Javascript, I decided to do it with PHP.
https://kotori-blog.com/php/globalaccess/

Problem i am experiencing

If i check with Chrome, the session is not destroyed even if you open it after 15 minutes have passed since closing the tab or closing the browser, and if you look at var_dump ($_SESSION), ['visit'] The value is increasing.
It seems that it's been 15 minutes when you check with FireFox, but it seems that the session is destroyed when you exit the browser.

Tryed code
// functions.php
function redirect () {
    // Discard session if more than 15 minutes have passed since last access
    if (isset ($_ SESSION ['last_access'])&&(time ()-$_SESSION ['last_access']>60 * 15)) {
        $_SESSION = array ();
        setcookie (session_name (), '', time ()-42000, '/');
        session_destroy ();
    }
    session_start ();
    $_SESSION ['last_access'] = time ();// last access update
    // first time redirect
    if (! isset ($_ SESSION ['visit'])) {
        $_SESSION ['visit'] = 1;
        header ("Location:/page I want to skip");
        exit;
    } else {
        // Count to check the status of ['visit']
        $visit = $_SESSION ['visit'];
        $visit ++;
        $_SESSION ['visit'] = $visit;
    }
}
// Run at the beginning of each page
<? php
    require_once ('functions.php');
    redirect ();
?>
<! DOCTYPE html>
<html>
Omitted
Supplemental information

Server
OS: Linux (details unknown, XServer)
PHP: 7.2.17

PC
OS: MacOSX 10.12
Browsers used: Chrome 78.0.3904.108, FireFox 70.0.1

  • Answer # 1

    Oh!

    That's why sessions usually expire when you close your browser.
    So if you want to do what you want to do, use cookies or localStorage.

  • Answer # 2

      

    If you check with Chrome, the session will not be discarded even if you close the tab or close the browser and then open it for more than 15 minutes

    Maybe Chrome's Window is closed but Process remains.
    Check if Chrome.exe remains in Task Manager.

    Is "Continue background app processing when Google Chrome is closed" enabled in Chrome settings?
    Go to chrome: // settings/system and check if the following are enabled:

  • Answer # 3

    Record the last login time in the session,
    If the session is not set or if 15 minutes have passed since the session value, isn't the process redirected?

    The expiration date of a session or cookie may not be as simple as discarding it when it expires.
    The explanation is difficult, but if you google it, it will come out a lot.