PHP determines how the current user has logged in elsewhere, and the current user _php tutorials elsewhere

Source: Internet
Author: User

PHP determines how the current user has logged in elsewhere, and the current user is elsewhere


This example describes how PHP determines that the current user has logged in elsewhere. Share to everyone for your reference. The specific analysis is as follows:

The main ideas are as follows:
1. Log the user's SessionID when logging in
2. Verify that the logged user SessionID matches the current SessionID when logging on
3. If it is not the same, indicate that you log in elsewhere

Full instance code click here to download this site.

First, enter http://localhost/login_single/index.php to see the login status.
The index.php page code is as follows:

Copy the Code code as follows:
<?php
Open session
Session_Start ();

Header ("content-type:text/html; Charset=utf-8 ");

Take the user information from the session
$username =$_session[' username '];

Judging if it works
if (!isset ($username)) {
echo "You are not logged in! Login ";
Exit ();
}

The user SessionID saved at logon
$sessin _id=file_get_contents (' Session_id/' $username);

If the current SessionID does not match the previously recorded SessionID
Description already logged in elsewhere
if (session_id ()! = $sessin _id) {
Log off the current user
unset ($_session[' username ');
echo "You are already logged in elsewhere!" New Login ";
Exit ();
}else{
echo "Welcome:". $username;
echo "Logout";
}

echo "

--This is what you see after you log in--

";

For users who are not logged in, you are prompted to jump to the http://localhost/login_single/login.html login page, and the login.html page code reads:

Copy the Code code as follows:




Login




Hint: Test user name: admin Password: 123



After successful login, the corresponding session judgment is made by Login.php page.

The login.php page code is as follows:

Copy the Code code as follows:
<?php
Open session
Session_Start ();

Set encoding
Header ("content-type:text/html; Charset=utf-8 ");

Receive the content submitted by the form
$username =$_post[' username '];
$password =$_post[' password '];

Impersonate Authenticated User Login
if ($username = = "Admin" && $password = = "123") {

Login successful, save user name to session
$_session[' username ']= $username;

Create a Directory
if (!file_exists (' session_id ')) {
mkdir (' session_id ');
}

The 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, can be saved to database, memcache, etc.
File_put_contents ($filename, $session _id);

Skip to Homepage
Header (' Location:index.php ');

}else{
Echo (');
Exit ();
}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/938847.html www.bkjia.com true http://www.bkjia.com/PHPjc/938847.html techarticle PHP determines how the current user has logged in elsewhere, and the current user elsewhere in this article describes how PHP determines that the current user has logged in elsewhere. Share to everyone for your reference. Specific ...

  • 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.