Home php教程 php手册 Discuz!X中SESSION机制实例详解

Discuz!X中SESSION机制实例详解

Jun 06, 2016 pm 07:43 PM
discuz

这篇文章主要介绍了Discuz!X中SESSION机制,以实例形式较为详细的分析了Discuz!X中SESSION机制的原理与数据库操作技巧,具有一定参考借鉴价值,需要的朋友可以参考

本文实例讲述了Discuz!X中SESSION机制。分享给大家供大家参考。具体如下:

在Discuz! X中一如继往的,SESSION 并没有使用 PHP 自带的 SESSION 机制,而是系统的一套自带的机制。

在数据库中可以看到有两个 SESSION 表:
一个是pre_common_adminsession,是管理员登录后台的 SESSION 表;
另一个是 pre_common_session 表,是所有用户在前台浏览页面时的 SESSION 表。
这两个表都是内存表(内存表的读写速度远高于 MYISAM 表及文本文件)。

在 Discuz! X 中 SESSION 与 COOKIE 是分不开的,因为 SESSION 就是从客户端读取的 COOKIE ,
然后由浏览页面时触发相关的函数执行,再写入数据库 SESSION 表。

我以登录流程为例来讲解程序具体是如何执行的。
在前台首页,点击登录后,弹出一个登录窗口,填写好数据后,提交。form表单提交的 URL 是:

复制代码 代码如下:

?mod=logging&action=login&loginsubmit=yes&floatlogin=yes&inajax=1


数据提交到了 member.php 文件中,在程序中可看到下面的代码:

$mod = !in_array($discuz->var['mod'], $modarray) ? 'logging' : $discuz->var['mod']; //mod的值即是接下来加载的php页面 define('CURMODULE', $mod); $modcachelist = array('register' => array('modreasons', 'stamptypeid', 'fields_required', 'fields_optional', 'ipctrl')); $cachelist = array(); if(isset($modcachelist[CURMODULE])) { $cachelist = $modcachelist[CURMODULE]; } $discuz->cachelist = $cachelist; $discuz->init(); runhooks(); require DISCUZ_ROOT.'./source/module/member/member_'.$mod.'.php'; //完成程序的包含操作

打开source/module/member/member_logging.php文件,是一个类,在类的前面可看到下面三句代码:

$ctl_obj = new logging_ctl(); $method = 'on_'.$_G['gp_action']; // $_G['gp_action'] 等于action的值即 login $ctl_obj->$method(); //$ctl_obj->on_login();

在类中可找到login方法,在方法中,大约 56 行有下面一个判断语句:

if(!submitcheck('loginsubmit', 1, $seccodecheck)) { //判断语句是当游客浏览时,submitcheck 函数的返回值是假,取反,为真。 //当用户登录时,程序走的是else部分,在里面可看到下面五句代码: } else { $_G['uid'] = $_G['member']['uid'] = 0; $_G['username'] = $_G['member']['username'] = $_G['member']['password'] = ''; //变量赋值 $result = userlogin($_G['gp_username'], $_G['gp_password'], $_G['gp_questionid'], $_G['gp_answer'], $_G['setting']['autoidselect'] ? 'auto' : $_G['gp_loginfield']); //从数据库查询用户数据,并返回相应的信息 if($result['status'] > 0) { //状态值大于 0 ,说明有此用户,可以登录 setloginstatus($result['member'], $_G['gp_cookietime'] ? 2592000 : 0); //设置登录状态,即是写 COOKIE 操作,COOKIE 中的数据即是 SESSION 中相应的数据,但此函数并不负责写 SESSION 的操作

我们来看一下 source/function/function_login.php中的 setloginstatus 函数,是普通的写 COOKIE 操作,不再具体讲解:

function setloginstatus($member, $cookietime) { global $_G; $_G['uid'] = $member['uid']; $_G['username'] = $member['username']; $_G['adminid'] = $member['adminid']; $_G['groupid'] = $member['groupid']; $_G['formhash'] = formhash(); $_G['session']['invisible'] = getuserprofile('invisible'); $_G['member'] = $member; $_G['core']->session->isnew = 1; dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true); //authcode加密 dsetcookie('loginuser'); dsetcookie('activationauth'); dsetcookie('pmnum'); }

到这里可以说是登录流程大部分已经走完,但是 COOKIE 不清除时,会一直存在于客户端,如果超时,程序中会在判断弃用此 COOKIE,并重新写入。

下面我们来看一下 DZX 中 SESSION 操作的类,在 source/class/calss_core.php 文件中:
程序中每次请求都会加载 SESSION ,这是由核心类 discuz_core 中的 _init_session 方法来执行的,此方法被置于 类的 init方法中,说明每次加载类,会自动将 SESSION 写入。

function _init_session() { $this->session = new discuz_session(); //创建 SESSION 类 if($this->init_session) { //从 COOKIE 中读取数据 $this->session->init($this->var['cookie']['sid'], $this->var['clientip'], $this->var['uid']); $this->var['sid'] = $this->session->sid; $this->var['session'] = $this->session->var; //判断 SID 是否相等,不等,说明是多个用户在同一主机上登录网站,需要重新写 COOKIE if($this->var['sid'] != $this->var['cookie']['sid']) { dsetcookie('sid', $this->var['sid'], 86400); } if($this->session->isnew) { if(ipbanned($this->var['clientip'])) { $this->session->set('groupid', 6); } } if($this->session->get('groupid') == 6) { $this->var['member']['groupid'] = 6; sysmessage('user_banned'); } //UID 不为空,且需要更新 SESSION 或是 SESSION 超时,更改用户状态,需要用户重新登录 if($this->var['uid'] && ($this->session->isnew || ($this->session->get('lastactivity') + 600) session->set('lastactivity', TIMESTAMP); $update = array('lastip' => $this->var['clientip'], 'lastactivity' => TIMESTAMP); if($this->session->isnew) { $update['lastvisit'] = TIMESTAMP; } DB::update('common_member_status', $update, "uid='".$this->var['uid']."'"); } } }

操作 SESSION 的类是 discuz_session ,我们看这个类里面的两个方法:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Discuz background login problem solution revealed Discuz background login problem solution revealed Mar 03, 2024 am 08:57 AM

The solution to the Discuz background login problem is revealed. Specific code examples are needed. With the rapid development of the Internet, website construction has become more and more common, and Discuz, as a commonly used forum website building system, has been favored by many webmasters. However, precisely because of its powerful functions, sometimes we encounter some problems when using Discuz, such as background login problems. Today, we will reveal the solution to the Discuz background login problem and provide specific code examples. We hope to help those in need.

Detailed explanation of Discuz registration process: allowing you to easily modify personal information Detailed explanation of Discuz registration process: allowing you to easily modify personal information Mar 13, 2024 pm 12:21 PM

"Detailed Explanation of Discuz Registration Process: Allowing you to easily modify personal information, specific code examples are required" Discuz is a powerful community forum program that is widely used in various websites. It provides a wealth of user registration and personal information modification. functions and interfaces. This article will introduce you to Discuz's registration process in detail and provide specific code examples to help you easily customize and modify your personal information. 1. User registration process In Discuz, user registration is one of the important functions of the site. The smoothness of the registration process and

What is Discuz? Definition and function introduction of Discuz What is Discuz? Definition and function introduction of Discuz Mar 03, 2024 am 10:33 AM

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

What should I do if I encounter an incorrect Discuz password? Quick solution sharing! What should I do if I encounter an incorrect Discuz password? Quick solution sharing! Mar 03, 2024 am 09:33 AM

What should I do if I encounter an incorrect Discuz password? Quick solution sharing! Discuz! It is a very popular forum program that provides users with a platform for convenient communication. Using Discuz! When accessing a forum, sometimes you may encounter an incorrect password, which may cause users to be unable to log in and use the forum normally. Well, meet Discuz! When the password is wrong, how should we quickly solve the problem? Some solutions will be shared below, with specific code examples provided for reference. 1. Check whether the password

Discuz Editor: Powerful web page editing tool Discuz Editor: Powerful web page editing tool Mar 09, 2024 pm 06:06 PM

Discuz Editor: A powerful web page editing tool that requires specific code examples. With the development of the Internet, website construction and content editing have become more and more important. As a common web page editing tool, Discuz editor plays an important role in website construction. It not only provides a wealth of functions and tools, but also helps users edit and publish content more conveniently. In this article, we will introduce the features and usage of the Discuz editor, and provide some specific code examples to help readers better understand and use

A must-have for Discuz users! Comprehensive analysis of renaming props! A must-have for Discuz users! Comprehensive analysis of renaming props! Mar 12, 2024 pm 10:15 PM

A must-have for Discuz users! Comprehensive analysis of renaming props! In the Discuz forum, the name change function has always received much attention and demand from users. For some users who need to change their name, name change props can easily modify the user name, and this is also an interesting way of interaction. Let’s take an in-depth look at the renaming props in Discuz, including how to obtain them, how to use them, and solutions to some common problems. 1. Obtain name-changing props in Discuz. Name-changing props are usually purchased through points or the administrator

Discuz background account login exception, how to deal with it? Discuz background account login exception, how to deal with it? Mar 09, 2024 pm 05:51 PM

Title: Discuz background account login exception, how to deal with it? When you use the backend management of the Discuz forum system, you may sometimes encounter abnormal account login. This could be due to a variety of reasons, including a wrong password, account being blocked, network connection issues, etc. When encountering this situation, we need to solve the problem through simple troubleshooting and processing. Check whether the account number and password are correct: First, confirm whether the account number and password you entered are correct. When logging in, make sure the capitalization is correct and the password is

Discuz Editor: an efficient post layout tool Discuz Editor: an efficient post layout tool Mar 10, 2024 am 09:42 AM

Discuz Editor: An efficient post layout tool. With the development of the Internet, online forums have become an important platform for people to communicate and share information. In the forum, users can not only express their opinions and ideas, but also discuss and interact with others. When publishing a post, a clear and beautiful format can often attract more readers and convey more accurate information. In order to facilitate users to quickly type and edit posts, the Discuz editor came into being and became an efficient post typesetting tool. Discu

See all articles