Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remembering PHP Session Private Browsing

Tags:

php

session

I'm developing an analytics script that people will be able to add to their page in order to track visitor data. One of the issues I've come across is devising a way to track individual's sessions when they're viewing someone's page from a private browser (I.e. Incognito).

This is the script I'm using to see if someone to observe if someone has been active for more than 30 minutes, if they have, a new session will be created, if not, then they will resume their previous session.

        session_start();
        $max_time = 1800;
        $current  = time();
        if (!isset ($_SESSION['Stationary'])){
            $_SESSION['Stationary'] = time();
            $session = $_SESSION['Stationary'];
        }
        if (!isset ($_SESSION['Inactive'])) { 
            $_SESSION['Inactive'] = time();
        } else {
            $session_life = $current - $_SESSION['Inactive'] ;
            if ($session_life > $max_time ) {
                session_destroy();
                session_start();
                $_SESSION['Inactive'] = time();
                $_SESSION['Stationary'] = time();
                $session = $_SESSION['Stationary'];
            } else {
                $_SESSION['Inactive'] = time();
                $session = $_SESSION['Stationary'];
            }
        }

This script works flawlessly when a user views my page from a regular browser (IE. Chrome Incognito), however when they view it on something like an iPhone, in Private Browsing, every time they access a new page, a new session is rendered -- a problem that I do not have when viewed otherwise.

So my question then is, I'm aware that viewing pages in a Private Browser is achieved through temporary cacheing which is cleared once the browser is closed, however why is it that even when the browser is not closed, opening a link destroys their previous session even when the link leads to another page, with the same script on the page?

Is there a workaround to this?

EDIT: I should note that this script is being placed in a php file with the header application/json to be used as a JavaScript file as well.

like image 572
Brad Avatar asked May 27 '15 07:05

Brad


People also ask

Does private browsing mode remember anything?

Private browsing only prevents your web browser from saving your browsing history. This means anyone else who uses your computer will not be able to see your online activity. Unfortunately, it doesn't guarantee security—your activity can still be tracked by websites. We'll talk more about tracking later in this lesson.

How can save session data in PHP?

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session.

Is PHP session stored in browser?

PHP allows us to track each visitor via a unique session ID which can be used to correlate data between connections. This id is a random string sent to the user when a session is created and is stored within the user's browser in a cookie (by default called PHPSESSID).

Where are PHP sessions saved?

PHP Session Start By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier). By itself, the session_start() function doesn't add much functionality to a web page.


1 Answers

If we firstly look at php session documentation

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

Secondly, what mozilla support says about private browsing (pretty much the same for other browsers)

What does Private Browsing not save? Cookies!

So the answer is clear now: the browser is not saving any of cookies that are used by php in order to retrieve the respective session.

Is there a workaround to this?

Yes. The $_SERVER variable holds data that can be considered as unique. For example, try using REMOTE_ADDR combined with parsed data from HTTP_USER_AGENT and CRUD it (database, probably).

Some extra info

  • How to get the client IP address in PHP?
  • Get users OS and version number
like image 96
sitilge Avatar answered Oct 05 '22 21:10

sitilge