Home > Backend Development > PHP Tutorial > An introduction to how PHP uses independent login mechanisms for two different projects under the same domain name

An introduction to how PHP uses independent login mechanisms for two different projects under the same domain name

巴扎黑
Release: 2023-03-16 11:42:02
Original
1804 people have browsed it

This article mainly introduces to you the relevant information on how to implement independent login mechanisms for two different projects in PHP under the same domain name. The article introduces it in detail through sample code, which has certain reference for everyone's study or work. The value of learning, friends who need it, come and take a look below.

Preface

Currently there is such a demand, under a domain name such as: http://example.com, there are two projects , example.com/a/, example.com/b/, these two projects are independent programs and have different member login mechanisms, but we know that under the same domain name, its sessions are shared. That is to say, after you log in at station a, your session information at station a will also appear at station b, because the default session_id name is PHPSESSID, that is, when you visit project a for the first time, it will automatically generate a session_id named PHPSESSID. , and create a file named session_id on the server side, and then send the session_id to the browser's cookie to save it. When the next visit is made, the cookie information will be carried, the server side gets the session_id, and then continues the session. In this way, there will be a situation of session information sharing. How to separate two different session information?

1. Define session_name

It’s actually very simple, just use session in the initialization file of project b , just modify the session_name.

example.com/a/init.php


session_start();
// ...
Copy after login

example.com/b/init.php


// session_id('123456'); // 可以自定义session_id,默认是系统自己生成的

session_name('EBCP_SID'); // session_name 必须定义在session_start() 前

session_start();
// ...
Copy after login

2. Test

example.com/a /test.php


<?php

// a项目测试页面

define("IN_EB", true);
include_once("./init.php");

if($_SESSION[&#39;nickname&#39;])
{
 //
 dump("session 页面- 欢迎你继续回来 {$_SESSION[&#39;nickname&#39;]} " . date("Y-m-d H:i:s"));
}
else
{
 $_SESSION[&#39;nickname&#39;] = "Corwien";
 dump("session 页面- 你是第一次登录 {$_SESSION[&#39;nickname&#39;]} " . date("Y-m-d H:i:s"));
}
Copy after login

Output result:


session 页面- 欢迎你继续回来 Corwien 2017-09-22 07:49:15
Copy after login

## Browser cookie for #a project:


example.com/b/test.php


<?php

// b项目测试页面

define("IN_EB", true);
include_once("./init.php");

if($_SESSION[&#39;nickname&#39;])
{
 //
 dump("session_v2 页面- 欢迎你继续回来 {$_SESSION[&#39;nickname&#39;]} " . date("Y-m-d H:i:s"));
}
else
{
 $_SESSION[&#39;nickname&#39;] = "JackMa";
 dump("session_v2 页面- 你是第一次登录 {$_SESSION[&#39;nickname&#39;]} " . date("Y-m-d H:i:s"));
}
Copy after login

Output result:


session_v2 页面- 欢迎你继续回来 JackMa 2017-09-22 07:49:15
Copy after login

b Browser cookie for project:

The above is the detailed content of An introduction to how PHP uses independent login mechanisms for two different projects under the same domain name. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template