Introduction to PHP's independent login mechanism for two different projects under the same domain name

Source: Internet
Author: User
This article mainly introduces you about PHP under the same domain name two different projects how to do the independent login mechanism of the relevant information, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends to see together below.

Objective

There is currently a requirement under a domain name such as: http://example.com, there are two projects, example.com/a/,example.com/b/, these two projects are independent of each other program, there are different member login mechanisms, but we know that Under the same domain name, its session is shared, that is, after you log in a station, B station will also appear you in a station session information, because the default session_id name is Phpsessid, that is, when you first access a project, it will automatically generate a name Phpsessid session_id, and creates a session_id named file on the server, and then sends session_id to the browser's cookie to be saved, and the next time it is accessed, the cookie information is carried The server side gets session_id, and then continues the session. In this way, the session information sharing situation, how should be independent of two different session information?

I. Definition of Session_name

In fact, it is very simple, just use the session in the initialization file of the B project, modify the next session_name.

example.com/a/init.php


Session_Start ();//...

example.com/b/init.php


session_id (' 123456 '); Can be customized session_id, the default is the system itself generated session_name (' Ebcp_sid '); Session_name must be defined before Session_Start () session_start ();//...

Second, the test

example.com/a/test.php


<?php//a Project test page define ("In_eb", True), Include_once ("./init.php"), if ($_session[' nickname ']) {//Dump ("SESSION page- You are welcome to go back to {$_session[' nickname '} ". Date ("y-m-d h:i:s")); else{$_session[' nickname '] = "Corwien"; Dump ("SESSION page-you are the first to login {$_session[' nickname ']}". Date ("y-m-d h:i:s"));

Output Result:


Session page-Welcome back to Corwien 2017-09-22 07:49:15

Browser cookie for Item A:


example.com/b/test.php


<?php//B Project test page define ("In_eb", True), Include_once ("./init.php"), if ($_session[' nickname ']) {//Dump ("Session_v2 Page-You are welcome to go back to {$_session[' nickname '} ". Date ("y-m-d h:i:s")); else{$_session[' nickname '] = "Jackma"; Dump ("SESSION_V2 page-you are the first to login {$_session[' nickname ']}". Date ("y-m-d h:i:s"));

Output Result:


SESSION_V2 page-Welcome back to Jackma 2017-09-22 07:49:15

Browser Cookie for Project B:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.