检查会话是否是PHP中的新内容 [英] Check whether a session is new in PHP
问题描述
我有一个网站,每个用户会话需要执行一次特定的后端功能。因此,我希望能够确定任何给定的页面视图是否是给定会话中的第一个。
I have a website that needs to perform a certain backend function once per user session. I therefore want to be able to determine whether any given page view is the first within a given session.
根据PHP文档:
session_start()根据通过GET或POST请求传递的
a会话标识符创建会话或恢复当前会话或传递通过
cookie。
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
那么有什么办法可以知道会话是在创建还是恢复?
So is there any way to know whether the session is being created or resumed?
或者这是我必须根据我在服务器上维护的列表检查当前会话ID的情况,以检查它是否已经注册过?如果是这样,这是否需要写入数据库,或者是否有一种不那么麻烦的内存方式?
Or is this a situation where I have to check the current session ID against a list I maintain on the server, to check if it's been registered before? If so, does this necessitate writing to a database, or is there a less cumbersome, in-memory way of doing this?
推荐答案
<?php
session_start();
if(empty($_SESSION['exists'])){
//handle completely new session here
}
$_SESSION['exists'] = true;
.... //continue on with normal request
这篇关于检查会话是否是PHP中的新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!