php用戶注冊過濾中文用戶名,在很多開發中,用戶名是不允許中文名字的,所以我們今天做個實例,限制中文名注冊,在php中我們可以利用正則表達式來限制中文名如preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/"了,下面看實例。
- <?php
- header("Content-type:text/html;charset=gb2312");
- require_once('inc/connect.php');
- $xm=isset($_GET['txt'])?$_GET['txt']:'null';
- $ta=isset($_GET['tag'])?$_GET['tag']:3;
- if( $ta==1 ){
- if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $xm)){
- echo("<div id='error'>對不起,不能使用中文作為用戶名! </div>");
- exit();
- }
- if($xm=='null' || $xm=='' || strlen($xm)>10 || strlen($xm)<3){
- echo("<div id='error'>對不起,用戶由3-12Aa_zZ及數字組成! </div>");
- exit();
- }else{
- $result=mysql_query("select m_uid from wk_member where m_uid='$xm' ") or die('Error !'.mysql_error());
- if(!mysql_num_rows($result)){
- echo("<div id='success'><font color=red> 恭喜你!此用戶可以注冊!</font> </div>");
- }else{
- echo("<div id='error'>對不起,用戶己被注冊請選擇其它名稱!</div>");
- }
- }
- }elseif( $ta==0){
- if(@file_get_contents($xm)){
- echo '<div id="success"> 博客地址可以訪問</div>';
- }else{
- echo '<div id="error">對不起,你輸入的博客地址不能正常訪問<div >';
- }
- }else{
- echo "<div id='error'>對不起,無效的操作!</div>";
- }
- ?>
總結:方法很簡單,只要獲取用戶輸入的資料再利用preg_match判斷一下就可以了。
新聞熱點
疑難解答