您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關利用PHP怎么編寫一個用戶注冊表單驗證功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
具體如下:
注冊界面
register.html
<h2>用戶注冊</h2> <form method="post" action="register_verify.php"> <input type="text" placeholder="用戶名" name="username"><br><br> <input type="password" placeholder="密碼" name="password"><br><br> <input type="password" placeholder="重復密碼" name="password2"><br><br> <label>性別: <input type="radio" name="sex" value="男" checked="checked">男 <input type="radio" name="sex" value="女">女</label><br><br> <input type="email" placeholder="郵箱" name="email"><br><br> <button class="btn" type="submit">注冊</button> </form>
register_verify.php
<?php require "mysql.php"; //導入mysql.php訪問數據庫 $username=$_POST['username']; $password=$_POST['password']; $password2=$_POST['password2']; $sex=$_POST['sex']; $email=$_POST['email']; if(checkEmpty($username,$password,$password2,$sex,$email)){ if(checkpwd($password,$password2)){ if(checkEmail($email)){ if(insert($username,$password,$sex,$email)) echo"注冊成功"; } } } //方法:判斷是否為空 function checkEmpty($username,$password,$password2,$sex,$email){ if($username==null||$password==null||$password2==null){ echo '<html><head><Script Language="JavaScript">alert("用戶名或密碼為空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } else{ if($sex==null){ echo '<html><head><Script Language="JavaScript">alert("性別為空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } elseif($email==null){ echo '<html><head><Script Language="JavaScript">alert("郵箱為空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } else{ return true; } } } //方法:檢查兩次密碼是否相同 function checkpwd($password,$password2){ if($password==$password2) return true; else echo '<html><head><Script Language="JavaScript">alert("兩次密碼不一致");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } //方法:郵箱格式驗證 function checkEmail($email){ $preg = '/^(\w{1,25})@(\w{1,16})(\.(\w{1,4})){1,3}$/'; if(preg_match($preg, $email)){ return true; }else{ echo '<html><head><Script Language="JavaScript">alert("郵箱格式有誤");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } } //方法:將數據插入數據庫中 function insert($username,$password,$sex,$email){ $conn=new Mysql(); $sql="insert into user VALUE (null,'$username','$password','$sex','$email')"; $result=$conn->sql($sql); if($result){ return true; } else{ echo '<html><head><Script Language="JavaScript">alert("寫入數據庫失敗");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } $conn->close(); }
看完上述內容,你們對利用PHP怎么編寫一個用戶注冊表單驗證功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。