Two PHP user login codes (cookies, database)

Source: Internet
Author: User

Here we want to talk about two kinds of user login methods is to use window validation Plus database tutorial Password Authentication implementation login, the latter is the use of cookies to record user information to log in, the need for friends can try.

<?php Tutorial
function Authenticate_user () {
Header (' Www-authenticate:basic realm= "Secret Stash");
Header ("http/1.0 401 Unauthorized");
Exit
}

if (! isset ($_server[' Php_auth_user ')) {
Authenticate_user ();
} else {
MySQL tutorial _pconnect ("localhost", "authenticator", "secret") or Die ("Can ' t connect to Database server!");
mysql_select_db ("Java2s") or Die ("Can ' t select Authentication database!");

$query = "Select Username, PS Tutorial wd from user WHERE username= ' $_server[php_auth_user] ' and PSWD=MD5 (' $_SERVER[PHP_AUTH_PW] ')";

$result = mysql_query ($query);

If Nothing is found, reprompt the user for the login www.111cn.net information.
if (mysql_num_rows ($result) = = 0) {
Authenticate_user ();
}
}
?>

Using Cookies to login methods



<title>enter password</title>

<body>
<form name= "Forml" method= "POST" action= "cookiebasedpasswordlogin.php" >
<table>
<tr>
&LT;TD colspan= "2" >
<div align= "center" ><b>please Specify the Password</b></div>
</td>
</tr>
<tr>>
<td>
<div align= "right" >customer id</div>
</td>
<td>
<input type= "text" name= "username" >
</td>
</tr>
<tr>
<td>
<div align= "right" >Password</div>
</td>
<td>
<input type= "password" name= "password" >
</td>
</tr>
<tr>
&LT;TD colspan= "2" >
<center>
<input type= "Submit" name= "Submit" value= "Login" >
</center>
</td>
</tr>
</table>
</form>
</body>





<!--cookiebasedpasswordlogin.php
<?php
$now = getdate ();
$storetime = $now ["Weekday"]. " " . $now ["Month"]. " " . $now ["Year"];
$storetime. = "Time:";

if ($now ["hours"] < 10) {
$storetime. = "0". $now ["hours"];
} else {
$storetime. = $now ["hours"];
}

$storetime. = ":";
if ($now ["Minutes"]<10) {
$storetime. = "0". $now ["Minutes"];
} else {
$storetime. = $now ["Minutes"];
}

$storetime. = ":";
if ($now ["seconds"] <10) {
$storetime. = "0". $now ["seconds"];
} else {
$storetime. = $now ["Seconds"];
}
if (Isset ($data)) {
$counter =++ $data [l];
Setcookie ("data[0]", $storetime, Time () + (60*60*24));
Setcookie ("data[l]", $counter, Time () + (60*60*24)); Setcookie ("data[2]", $username, Time () + (60*60*24));
echo "<b><center>hi". $data [2]. "!!</center></b><br>n";
echo "<b><center>last Login Time:". $data [0]. " </center></b><br>n ";
echo "<b><center>current Date:". $storetime. " </center></b><br>n ";
echo "<b><center>page View Count:". $data [L]. "</center></b><br>n";
echo "<b><center>you have successfully logged in!</center></b>";
Echo ("<b><contor>you can access this area without entering a password for" next HOURS!&LT;/CENTER&GT;&L T;/b> ");
} else {
if (Isset ($username) && isset ($password)) {
if ($password = = "SuperPass") {
$counter = 0;
Setcookie ("data[0]", $storetime, Time () + (60*60*24));
Setcookie ("data[l]", $counter, Time () + (60*60*24));
Setcookie ("data[2]", $username, Time () + (60*60*24));
$url = "Location:cookieimp.php";
Header ($url);
}else{
echo "}
}
}
?>

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.