PHP Implements administrator login function

Source: Internet
Author: User
Open Dreamweaver CS6 and add the following code:


if ($_post[user])//Determine if user name is entered

{

$name =$_post["User"]; Get login user Name

$pass =$_post["Pass"]; Get login Password

Require "ch16-1.php"; Referencing a configuration file

$link =mysql_connect ($db _host, $db _user, $db _pass) or Die (Mysql_error ());

mysql_select_db ($db _name, $link);

$sql = "SELECT * from $table _user where u_name= ' $name ' && u_pass= ' $pass '";

$result =mysql_query ($sql); Send SQL Request

if (@mysql_num_rows ($result)!=0)//Find eligible users and passwords

{

Setcookie ("name", "$name", Time () +60*60*24); Create a cookie

echo " ";

echo " ";

echo "";

echo "

Login Successful! Back to the back page in 1 seconds ... ";

echo "";

}

else//If there are no eligible users and passwords

{

echo "

The user name or password is wrong! ";

}

}

?>

Administrator Login

@ All rights reserved

at last, save the file and debug the run

Click "File"-"save" or press the shortcut key ctrl+s to save the page with the file name ch16-8.php, the file is automatically saved to the site. Press F12 keyboard to debug the webpage, display:

Enter the user name and password, if successful, then go to the hypervisor: Show graph:

Both,




This example includes the User Login form section and the form's handler, which realizes the user login function.

? The user login function is also a very simple function, it is not difficult to achieve. As long as the input of the user and password is present in the Administrator information table, if present, the login is successful, and the user name is saved to the cookie variable, and automatically jump to the background home page; If no matching administrator information is found, a "username and password Error" is displayed.

This example code is as follows:

if ($_post[user])//Determine if user name is entered

{

$name =$_post["User"]; Get login user Name

$pass =$_post["Pass"]; Get login Password

Require "ch16-1.php"; Referencing a configuration file

$link =mysql_connect ($db _host, $db _user, $db _pass) or Die (Mysql_error ());

mysql_select_db ($db _name, $link);

$sql = "SELECT * from $table _user where u_name= ' $name ' && u_pass= ' $pass '";

$result =mysql_query ($sql); Send SQL Request

if (@mysql_num_rows ($result)!=0)//Find eligible users and passwords

{

Setcookie ("name", "$name", Time () +60*60*24); Creating cookie Variables

echo " ";

echo " ";

echo "";

echo "

Login Successful! Back to the back page in 1 seconds ... ";

echo "";

}

else//If there are no eligible users and passwords

{

echo "

The user name or password is wrong! ";

}

}

In the program using JavaScript code to implement the user name and password two input box, the corresponding code is as follows:

Call the JS function juge () during form submission to validate the entry:

End, you can think in practice.

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