首頁 > php教程 > php手册 > php 用户注册并且设置为己登录状态实现方法

php 用户注册并且设置为己登录状态实现方法

WBOY
發布: 2016-05-26 15:21:59
原創
1127 人瀏覽過

用户注册并且设置为己登录状态实现方法,下面实例讲述了如何把表单提交的数据保存到mysql数据库,而没有实现用户注册后自动登录的功能,而实例二就实现了这种做法.

 class="brush:php;"><html> 
	<body> 
	<?php 
	 
	$self = $_SERVER[&#39;PHP_SELF&#39;]; 
	$firstname = $_POST[&#39;firstname&#39;]; 
	$lastname = $_POST[&#39;lastname&#39;]; 
	$username = $_POST[&#39;username&#39;]; 
	$password = $_POST[&#39;password&#39;]; 
	 
	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> 
登入後複製

下面个实例更详细,用户注册后并且设置用户的为登录状态,本实现利用了setcookie来保存用户登录信息,先看数据库代码如下:

create table user_info ( 
	user_id char(18), 
	fname char(15), 
	email char(35)); 
登入後複製

实例文件index.php代码如下:

<?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(&#39;$uniq_id&#39;, &#39;$fname&#39;, &#39;$email&#39;)"; 
	     $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!");//开源代码phprm.com 
	     $query = "SELECT * FROM user_info WHERE user_id = &#39;$userid&#39;"; 
	     $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; 
	 
	
登入後複製

本文地址:

转载随意,但请附上文章地址:-)

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板