记住 PHP 会话私密浏览 [英] Remembering PHP Session Private Browsing

查看:30
本文介绍了记住 PHP 会话私密浏览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个分析脚本,人们可以将其添加到他们的页面以跟踪访问者数据.我遇到的一个问题是设计一种方法来跟踪个人从私人浏览器(即隐身浏览器)查看某人的页面时的会话.

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).

这是我用来查看是否有人观察是否有人活动超过 30 分钟的脚本,如果有,将创建一个新会话,如果没有,则他们将恢复上一个会话.

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'];
            }
        }

当用户从常规浏览器(IE.Chrome Incognito)查看我的页面时,此脚本完美无缺,但是当他们在 iPhone 等设备上查看时,在隐私浏览中,每次访问新页面时,都会进行新会话呈现 - 以其他方式查看时我没有的问题.

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?

是否有解决方法?

我应该注意到这个脚本被放置在一个带有标题 application/json 的 php 文件中,以用作 JavaScript 文件.

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.

推荐答案

如果我们先看 php 会话文档

访问您网站的访问者会被分配一个唯一的 ID,即所谓的会话 ID.这要么存储在用户端的 cookie 中,要么在 URL 中传播.

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.

其次,什么 mozilla 支持 谈到隐私浏览(其他浏览器几乎相同)

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

无痕浏览不保存什么?饼干!

What does Private Browsing not save? Cookies!

所以现在答案很清楚了:浏览器不会保存 php 使用的任何 cookie 来检索相应的会话.

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.

是否有解决方法?

是的.$_SERVER 变量保存可以被视为唯一的数据.例如,尝试使用 REMOTE_ADDR 结合来自 HTTP_USER_AGENT 的解析数据并对其(可能是数据库)进行 CRUD.

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).

一些额外的信息

这篇关于记住 PHP 会话私密浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆