Php method for determining that the current user has logged on elsewhere. current user is elsewhere _ PHP Tutorial

Source: Internet
Author: User
Php determines that the current user has logged on elsewhere, and the current user has logged on elsewhere. Php's method of determining that the current user has logged on elsewhere. the current user's example in this article describes how php judges that the current user has logged on elsewhere. Share it with you for your reference. Specific php method for determining that the current user has logged on elsewhere. the current user is logged on elsewhere

This example describes how php judges that the current user has logged on elsewhere. Share it with you for your reference. The specific analysis is as follows:

The main idea is as follows:
1. log on and record the user's SessionID.
2. verify that the user SessionID recorded matches the current SessionID during logon.
3. if they are different, log on elsewhere.

Click here to download the complete instance code.

First, go to http: // localhost/login_single/index. php to view the logon status.
The index. php page code is as follows:

The code is as follows:


<? Php
// Enable Session
Session_start ();

Header ("Content-type: text/html; charset = utf-8 ");

// Retrieve the user information in the Session
$ Username = $ _ SESSION ['username'];

// Determine whether the parameter is valid
If (! Isset ($ username )){
Echo "you are not logged on! Login ";
Exit ();
}

// The user SessionID saved during logon
$ Sessin_id = file_get_contents ('session _ id/'. $ username );

// If the current SessionID does not match the previously recorded SessionID
// Indicates that you have logged on elsewhere
If (session_id ()! = $ Sessin_id ){
// Deregister the current user
Unset ($ _ SESSION ['username']);
Echo "you have logged on elsewhere! Log On Again ";
Exit ();
} Else {
Echo "welcome:". $ username;
Echo "logout ";
}

Echo"

-- This is what you can see after logging on --

";

For unlogged-on users, the system will prompt to jump to the http: // localhost/login_single/login.html#page. the login.html page code is as follows:

The code is as follows:






Login




Tip: test username: admin Password: 123




After successful logon, the login. php page determines the corresponding session.

The login. php page code is as follows:

The code is as follows:


<? Php
// Enable Session
Session_start ();

// Set the encoding
Header ("Content-type: text/html; charset = utf-8 ");

// Receives the content submitted by the form
$ Username = $ _ POST ['username'];
$ Password = $ _ POST ['password'];

// Simulate user login verification
If ($ username = "admin" & $ password = "123 "){

// Log on successfully and save the user name to the Session
$ _ SESSION ['username'] = $ username;

// Create a directory
If (! File_exists ('session _ id ')){
Mkdir ('session _ id ');
}

// Saved file name
$ Filename = 'session _ id/'. $ username;

// SessionId of the currently logged-on user
$ Session_id = session_id ();

// When SessionID is saved to the corresponding file
// Practical application, which can be saved to databases, memcache, etc.
File_put_contents ($ filename, $ session_id );

// Jump to the homepage
Header ('Location: index. php ');

} Else {
Echo ('script alert ("logon failed"); window. location = "login.html" script ');
Exit ();
}

I hope this article will help you with php programming.

Examples in this article describes how php judges that the current user has logged on elsewhere. Share it with you for your reference. Details...

Related Article

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.