程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 用戶注冊並且設置為己登錄狀態實現方法

php 用戶注冊並且設置為己登錄狀態實現方法

編輯:關於PHP編程

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: &nbsp; ";
  $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>


下面個實例更詳細,用戶注冊後並且設置用戶的為登錄狀態,本實現利用了setcookie來保存用戶登錄信息

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


//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;

?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved