PHP Implements administrator login function

Source: Internet
Author: User
Tags setcookie

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 "<meta http-equiv= ' Refresh ' content= ' 1; url=ch16-9.php ' > ';

echo "

echo "<body><center> Login successful! 1 seconds back backstage home ......</center></body> ";

echo "

}

else//If there are no eligible users and passwords

{

echo "<center> username or password error! </center> ";

}

}

?>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

<title> Admin Login </title>

<link href= "Mystyle.css" rel= "stylesheet" type= "Text/css"/>

<body>

<script language=javascript>

function juge (theform)

{

if (TheForm.user.value = = "")

{

Alert ("Please enter user name!");

TheForm.user.focus ();

return (false);

}

if (TheForm.pass.value = = "")

{

Alert ("Please enter user name!");

TheForm.pass.focus ();

return (false);

}

}

</script>

<form id= "Form1" Name= "Form1" method= "Post" action= "ch16-8.php" onsubmit= "return juge (this)" >

<p align= "center" > Current Position: <a href= "ch16-10.php" > Home </a>--User Login </p>

<p align= "center" > Admin Login:</p>

<p align= "center" > User name:

<input name= "User" type= "text" id= "user"/>

</p>

<p align= "center" > Password:

<input name= "pass" type= "password" id= "pass"/>

</p>

<p align= "center" >

<input type= "Submit" name= "Submit" value= "Submission"/>

<input type= "reset" name= "Submit2" value= "reset"/>

</p>

</form>

<p align= "center" >@ All rights reserved </p>

</body>

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 "<meta http-equiv= ' Refresh ' content= ' 1; url=ch16-9.php ' > ';

echo "

echo "<body><center> Login successful! 1 seconds back backstage home ......</center></body> ";

echo "

}

else//If there are no eligible users and passwords

{

echo "<center> username or password error! </center> ";

}

}

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

<script language=javascript>

function juge (theform)

{

if (TheForm.user.value = = "")

{

Alert ("Please enter user name!");

TheForm.user.focus ();

return (false);

}

if (TheForm.pass.value = = "")

{

Alert ("Please enter user name!");

TheForm.pass.focus ();

return (false);

}

}

</script>

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

<form id= "Form1" Name= "Form1" method= "Post" action= "ch16-8.php" onsubmit= "return juge (this)" >

End, you can think in practice.

PHP Implements administrator login function

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.