客商注册况兼安装为己登陆状态完成格局,客商

作者: 计算机网络  发布:2019-09-26

php教程 客户cookie登陆验证与mysql教程数据登陆验证的措施

php教程 顾客注册并且安装为己登入状态完成格局,上面实例呈报了如何把表单提交的多郎中存到mysql教程数据库教程,而未有达成客户注册后活动登陆的作用,而实例二就贯彻了这种做法。

 

<html>
<body>
<?php

$self = $_SERVER['PHP_SELF'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];

if( (!$firstname) or (!$lastname) or (!$username) or (!$password) )
{
  $form ="Please enter all new user details...";
  $form.="<form action="$self"";
  $form.=" method="post">First Name: ";
  $form.="<input type="text" name="firstname"";
  $form.=" value="$firstname"><br>Last Name: ";
  $form.="<input type="text" name="lastname"";
  $form.=" value="$lastname"><br>User Name: ";
  $form.="<input type="text" name="username"";
  $form.=" value="$username"><br>Password:   ";
  $form.="<input type="text" name="password"";
  $form.=" value="$password"><br>";
  $form.="<input type="submit" value="Submit">";
  $form.="</form>";
  echo($form);
}
else
{
  $conn = @mysql_connect("localhost","root", "") or die("Could not connect to MySQL");
  $db = @mysql_select_db("my_database",$conn) or die("Could not select database");
  $sql = "insert into users (first_name,last_name,user_name,password)values ("$firstname","$lastname","$username",password("$password") )";
  $result = @mysql_query($sql,$conn)or die("Could not execute query");
  if($result){
    echo("New user $username added");
  }
}
?>
</body></html>

<?php
unset($username);
if ($_COOKIE['login']) {
    list($c_username,$cookie_hash) = split(',',$_COOKIE['login']);
    if (md5($c_username.$secret_word) == $cookie_hash) {
        $username = $c_username;
    } else {
        print "You have sent a bad cookie.";
    }
}

if ($username) {
    print "Welcome, $username.";
} else {
    print "Welcome, anonymous user.";
}
?>

 

上边个实例更详实,客户注册后还要安装客商的为记名状态,本完成利用了setcookie来保存客户登陆音信

看个完整的与数据库教程连日来登入的代码

create table user_info (
user_id char(18),
fname char(15),
email char(35));

<html>

 <head>
  <title>Log-In Page</title>
 </head>

 <body>
 Please enter your user details to log-in here...

 <form action = "authenticate.php" method = "post">
 Username:<br>
 <input type = "text" name = "username">
 <br><br>
 Password:<br>
 <input type = "text" name = "password">
 <br><br>
 <input type = "submit" value = "Log In">
 </form>

 </body>

</html>

File: authenticate.php


<?php

$username = $_POST['username'];
$password = $_POST['password'];
$self =     $_SERVER['PHP_SELF'];
$referer =  $_SERVER['HTTP_REFERER'];

if( ( !$username ) or ( !$password ) )
{ header( "Location:$referer" ); exit(); }

$conn=@mysql_connect( "localhost", "userName", "password" ) or die( "Could not connect" );

$rs = @mysql_select_db( "my_database", $conn ) or die( "Could not select database" );
$sql = "select * from users where user_name="$username" and password = password( "$password" )";
 
$rs = mysql_query( $sql, $conn ) or die( "Could not execute query" );

$num = mysql_numrows( $rs );

if( $num != 0 )
{
  $msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
}
else
{
  header( "Location:$referer" ); exit();
}
?>

<html>

 <head>
  <title>Log-In Authenticated</title>
  </head>

  <body>
   <?php echo( $msg ); ?>
  </body>

</html>

//File: index.php

<?
$form = "
<form action="index.php" method="post">
<input type="hidden" name="seenform" value="y">
Your first name?:<br>
<input type="text" name="fname" value=""><br>
Your email?:<br>
<input type="text" name="email" value=""><br>
<input type="submit" value="Register!">
</form>
";
if ((! isset ($seenform)) && (! isset ($userid))) :
     print $form;
elseif (isset ($seenform) && (! isset ($userid))) :
     $uniq_id = uniqid(rand());
     @mysql_pconnect("localhost", "root", "") or die("Could not connect to MySQL server!");
     @mysql_select_db("user") or die("Could not select user database!");
     $query = "INSERT INTO user_info VALUES('$uniq_id', '$fname', '$email')";
     $result = mysql_query($query) or die("Could not insert user information!");
     setcookie ("userid", $uniq_id, time()+2592000);

     print "Congratulations $fname! You are now registered!.";
elseif (isset($userid)) :
     @mysql_pconnect("localhost", "root", "") or die("Could not connect to MySQL server!");
     @mysql_select_db("user") or die("Could not select user database!");
     $query = "SELECT * FROM user_info WHERE user_id = '$userid'";
     $result = mysql_query($query) or die("Could not extract user information!");

     $row = mysql_fetch_array($result);
     print "Hi ".$row["fname"].",<br>";
     print "Your email address is ".$row["email"];

endif;

?>

本文由金沙澳门官网送注册58发布于计算机网络,转载请注明出处:客商注册况兼安装为己登陆状态完成格局,客商

关键词: