php實(shí)現(xiàn)用戶注冊(cè)登錄界面的方法:1、創(chuàng)建log.php登錄主界面;2、設(shè)置register.php注冊(cè)主界面;3、通過(guò)mysqli_connect連接數(shù)據(jù)庫(kù)并進(jìn)行相應(yīng)的數(shù)據(jù)操作即可。
php入門到就業(yè)線上直播課:進(jìn)入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點(diǎn)擊使用
本教程操作環(huán)境:Windows7系統(tǒng)、PHP8.1版、Dell G3電腦。
php 怎么實(shí)現(xiàn)用戶注冊(cè)登錄界面?
PHP登錄與注冊(cè)頁(yè)面簡(jiǎn)單實(shí)現(xiàn)(包含數(shù)據(jù)庫(kù)驗(yàn)證)(包含數(shù)據(jù)庫(kù))
log.php(登錄主界面)
由于是簡(jiǎn)單的頁(yè)面,登錄頁(yè)面只做了數(shù)據(jù)庫(kù)驗(yàn)證,其實(shí)有條件也可以加上郵箱驗(yàn)證和驗(yàn)證碼驗(yàn)證。
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無(wú)標(biāo)題文檔</title> </head> <style type="text/css"> #aaa{width: 300px; height: 30px;} </style> <body> <center> <h2>登錄界面</h2> <form method="POST" action="log_ok.php"> 賬號(hào) :<input id="aaa" type="text" name="uname" placeholder="用戶名" /> <br /><br/> 密碼 :<input id="aaa"type="password" name="pwd" placeholder="密碼" /> <br /> <input type="submit" > <input type="reset"> <a href="register.php">注冊(cè)賬號(hào)</a> </form> </center> </body> </html>
log_ok.php
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無(wú)標(biāo)題文檔</title> </head> <body> <?php header("content-type:text/html; charest=UTF-8");//文件編碼格式 // Session需要先啟動(dòng)。 session_start(); //判斷uname和pwd是否賦值 if(isset($_POST['uname']) && isset($_POST['pwd'])){ $name = $_POST['uname']; $pwd = $_POST['pwd']; //連接數(shù)據(jù)庫(kù) require("conn.php"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //驗(yàn)證內(nèi)容是否與數(shù)據(jù)庫(kù)的記錄吻合。 $sql = "SELECT * FROM register WHERE (account='$name') AND (password='$pwd')"; //執(zhí)行上面的sql語(yǔ)句并將結(jié)果集賦給result。 $result = $conn->query($sql); //判斷結(jié)果集的記錄數(shù)是否大于0 if ($result->num_rows > 0) { // 輸出每行數(shù)據(jù) ,,,不知道為什么在這個(gè)變量前設(shè)置字符串的時(shí)候,必須得用"",''這個(gè)是不實(shí)現(xiàn)的。。什么不使用也可以成功 while($row = $result->fetch_assoc()) { echo'歡迎'.$row['username']; echo '<p> 賬號(hào):' . $row['account'].' <br/> 姓名:' . $row['username'].' <br/> 性別:' . $row['sex'].' <br/> 年齡:' . $row['age'].' <br/> 地址:' . $row['address'].' <br/> 郵箱:' . $row['mail'].' <br/> 電話:' . $row['number'].' <br/> </p>'; } } else { echo "沒(méi)有您要的信息"; } $conn->close(); //關(guān)閉數(shù)據(jù)庫(kù) } ?> </body> </html>
register.php(注冊(cè)主界面)
這個(gè)頁(yè)面我設(shè)置了,隨機(jī)id,只要刷新頁(yè)面就會(huì)出現(xiàn)一個(gè)新的賬戶,但是由于我做的是個(gè)簡(jiǎn)單的,所以沒(méi)有實(shí)現(xiàn)id不重復(fù)。有條件,您可以實(shí)現(xiàn)一下,可以發(fā)給我互相交流下,另外,重復(fù)密碼驗(yàn)證我也沒(méi)有設(shè)置,這只是個(gè)簡(jiǎn)單的,您如果想的話,做出來(lái)給我發(fā)一份。哈哈。
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無(wú)標(biāo)題文檔</title> </head> <style type="text/css"> .aaa{width: 300px; height: 30px;} </style> <style type="text/javascript"> </style> <body> <?php $a=mt_rand(333333,999999999); ?> <center> <h2>注冊(cè)界面</h2> <form method="post" name="from1" action="register_ok.php"><!-- 在數(shù)據(jù)庫(kù)中id是自動(dòng)增長(zhǎng)列 --> 賬號(hào): <input name="id" class="aaa" type="text" value="<?php echo"$a";?>"><br/><br/> 姓名: <input class="aaa" type="text" name="username"><br/><br/> 性別: <input class="aaa" type="text" name="sex"><br/><br/> 年齡: <input class="aaa" type="text" name="age"><br/><br/> 地址: <input class="aaa" type="text" name="address"><br/><br/> 郵箱: <input class="aaa" type="text" name="mail"><br/><br/> 密碼: <input class="aaa" type="text" name="password"><br/><br/> 確認(rèn) 密碼: <input class="aaa" type="text" name="repsw"><br/><br/> 手機(jī)號(hào): <input class="aaa"type="text" name="number"><br/><br/> <input type="reset"name="reset" value="重置"> <input type="submit"name="submit" value="注冊(cè)" onClick="myfunction"> <a href="log.php"><< 返回上一頁(yè)</a> <a href="register.php">點(diǎn)擊注冊(cè)</a> </form> </body> </html>
register_ok.php
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無(wú)標(biāo)題文檔</title> </head> </body> <body> <?php include_once("conn.php"); header("content-type:text/html; charest=UTF-8");//文件編碼格式 $id=$_POST['id']; $username=$_POST['username']; $sex=$_POST['sex']; $age=$_POST['age']; $address=$_POST['address']; $mail=$_POST['mail']; $password=$_POST['password']; $number=$_POST['number']; if(!($id and $username and $sex and $age and $address and $mail and $password and $number)){ echo("輸入值不能為空");//判斷變量名是否為空值 }else{ $sqlstr1="insert into register values('".$id."','".$username."','".$sex."','".$age."','".$address."','".$mail."','".$password."','".$number."')"; //執(zhí)行sql insert語(yǔ)句 把用post引用的變量接入到bookable中 $result = mysqli_query($conn,$sqlstr1);//承接結(jié)果集 if($result){ echo"添加成功"; }else{ echo"<script>alter('添加失敗');history.go(-1);</script>"; } } echo"$id $username $sex $age $address $mail $password $number"; ?> </body> </html>
conn.php(連接數(shù)據(jù)庫(kù))
<!doctype html> <html> <head> <meta charset="utf-8"> <title>conn文件</title> </head> <body> <?php $mysql_server_name = 'localhost'; //改成自己的mysql數(shù)據(jù)庫(kù)服務(wù)器 $mysql_username = 'root'; //改成自己的mysql數(shù)據(jù)庫(kù)用戶名 $mysql_password = ''; //改成自己的mysql數(shù)據(jù)庫(kù)密碼 $mysql_database = 'regist'; //改成自己的mysql數(shù)據(jù)庫(kù)名 $conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //連接數(shù)據(jù)庫(kù) //連接數(shù)據(jù)庫(kù)錯(cuò)誤提示 mysqli_query($conn, 'set names utf8'); mysqli_query($conn, 'set character set utf8'); if (mysqli_connect_errno($conn)) { die("連接 MySQL 失敗: " . mysqli_connect_error()); } ?> </body> </html>
數(shù)據(jù)庫(kù)
1.數(shù)據(jù)庫(kù)名是regist
2.表名是register
推薦學(xué)習(xí):《PHP視頻教程》