PHP實(shí)現(xiàn)的用戶注冊表單驗(yàn)證功能簡單示例
本文實(shí)例講述了PHP實(shí)現(xiàn)的用戶注冊表單驗(yàn)證功能。分享給大家供大家參考,具體如下:
注冊界面
register.html
<h1>用戶注冊</h1> <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="重復(fù)密碼" 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"; //導(dǎo)入mysql.php訪問數(shù)據(jù)庫 $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\">"; } //方法:郵箱格式驗(yàn)證 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\">"; } } //方法:將數(shù)據(jù)到數(shù)據(jù)庫中 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("寫入數(shù)據(jù)庫失敗");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } $conn->close(); }
驗(yàn)證碼和數(shù)據(jù)庫的實(shí)現(xiàn)方法前面寫過,這里不再贅述。
可參考前面兩篇文章:
PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫操作類
PHP通過GD庫實(shí)現(xiàn)驗(yàn)證碼功能
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php程序設(shè)計安全教程》、《php安全過濾技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php實(shí)現(xiàn)比較兩個字符串日期大小的方法
這篇文章主要介紹了php實(shí)現(xiàn)比較兩個字符串日期大小的方法,涉及php日期操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05學(xué)習(xí)php過程中的一些注意點(diǎn)的總結(jié)
在學(xué)習(xí)php的過程中會有一些細(xì)節(jié)是需要注意的,本文整理了一些比較實(shí)際的問題,希望對大家有所幫助2013-10-10同臺服務(wù)器使用緩存APC效率高于Memcached的演示代碼
之前看到有文章說同臺服務(wù)器上APC的效率是Memcached的7倍,APC效率比Memcached高是肯定的,至于倒底快多少,我寫了個小程序測試了下。2010-02-02php.ini-dist 和 php.ini-recommended 的區(qū)別介紹(方便開發(fā)與安全的朋友)
在下載php后,進(jìn)行配置的時候,會出現(xiàn)兩個php.ini:php.ini-dist和php.ini-recommended,那有什么不同呢?應(yīng)該如果選擇適合自己的版本呢2012-07-07php數(shù)組函數(shù)序列 之a(chǎn)rray_count_values() 統(tǒng)計數(shù)組中所有值出現(xiàn)的次數(shù)函數(shù)
array_count_values() 函數(shù)用于統(tǒng)計數(shù)組中所有值出現(xiàn)的次數(shù),本函數(shù)返回一個數(shù)組,其元素的鍵名是原數(shù)組的值,鍵值是該值在原數(shù)組中出現(xiàn)的次數(shù)。2011-10-10