首页 > 开发 > PHP > 正文

php+ajax用户注册验证用户是否在存

2024-05-04 21:59:10
字体:大 中 小
来源:转载
供稿:网友

这是一个完整理的php+mysql+ajax的用户注册实例程序,可以提供检测用户名是否被注册,这样可以增强用户体验,在你填写好用户名后就会提供你当前所有注册的用户是否己经被注册了.

本程序包括三个文件:

reg.html 用户注册html页面

reg.php php处理代码

conn.php 数据库教程连接文件

reg.html代码如下:

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  5. <title>php+ajax用户注册验证用户是否在存(php mysql完整实例)</title> 
  6. <style type="text/css"> 
  7. body{ 
  8.  font-size:12px; 
  9.  text-align:center; 
  10. } 
  11. .text{ 
  12.  width:180px; 
  13.  height:12px; 
  14. } 
  15. p{ 
  16.  width:600px; 
  17.  height:20px; 
  18.  line-height:20px; 
  19.  text-align:left; 
  20. } 
  21. p label{ 
  22.  display:block; 
  23.  width:80px; 
  24.  height:20px; 
  25.  line-height:20px; 
  26.  float:left; 
  27.  text-align:right; 
  28. } 
  29. p span{ 
  30.  margin-left:10px; 
  31. } 
  32. </style> 
  33. </head> 
  34. <body> 
  35. <script language="javascript"> 
  36. function createxmlhttprequest(){ 
  37.  var xmlhttp; 
  38.  if(window.activexobject){ 
  39.   xmlhttp = new activexobject("microsoft.xmlhttp"); 
  40.  }else if(window.xmlhttprequest){ 
  41.   xmlhttp = new xmlhttprequest(); 
  42.  } 
  43.  return xmlhttp; 
  44. } 
  45. function checkname(){ 
  46.  var name = document.getelementbyid('name'); //获取用户名文本框 
  47.  var span = document.getelementbyid('name_info'); //获取用于显示结果的span标记 
  48.  if(name.value.length <= 4){ 
  49.   span.style.color = '#ff0000'; //设置span标记内的字体颜色为红色 
  50.   span.innerhtml = '用户名长度不能少于4个字符!'; //span标记内容 
  51.   return false; 
  52.  } 
  53.  var xmlhttp = createxmlhttprequest();//创建异步请求对象 
  54.  var time = new date().gettime(); 
  55.  var url = 'reg.php?act=reg&name=' + name.value.tolowercase() + '&tmp=' + time;//构造出请求地址 
  56.  xmlhttp.open("get",url,true); //建立一个异步请求 
  57.  /*这里我们使用get方式请求 
  58.    post方式的请求基本差不多,朋友们自己试试如果不行,在下面给我留言*/ 
  59.  xmlhttp.onreadystatechange = function(){ //监视请求状态 
  60.   span.style.color = '#ff9900'; 
  61.   span.innerhtml = '查询中,请稍候!'; 
  62.   if(xmlhttp.readystate == 4 && xmlhttp.status == 200){ 
  63.    if(xmlhttp.responsetext.indexof('no') != -1){ //如果服务器返回的信息中有no 
  64.     span.style.color = '#cb2121'; //设置span标记颜色为红色 
  65.     span.innerhtml = '用户名[' + name.value + ']已经被别的用户使用!'; 
  66.    }else{//如果返回信息中没有no 
  67.     span.style.color = '#00a800';//设置颜色为绿色 
  68.     span.innerhtml = '恭喜您,该用户名未被注册!'; 
  69.    } 
  70.    return true; 
  71.    delete xmlhttp; //删除请求对象 
  72.   } 
  73.  } 
  74.  xmlhttp.send(null); //发送请求 
  75. } 
  76. </script> 
  77. <form method="post" action="reg.php"> 
  78. <p><label>用户名:</label><input type="text" class="text" id="name" name="user_name"/><span id="name_info"></span></p> 
  79. <p><label></label><input type="button" value="检查用户名" onclick="checkname()"/></p> 
  80. <p><label>密码:</label><input type="password" class="text" /></p> 
  81. <p><label>&nbsp;</label><input type="submit" value="注册" /></p> 
  82. </form> 
  83. </body> 
  84. </html> 

reg.php文件,代码如下:

  1. <?php 
  2. include("conn.php"); 
  3. $name=$_get['name']; 
  4. $name=urldecode($name); 
  5. if(strlen($name)>0){ 
  6.  $sql="select username from registration where username = '$name'"; 
  7.  $result=mysql_query($sql); 
  8.  $row=mysql_fetch_array($result); 
  9. //开源代码Vevb.com 
  10.  if($_get['act'] == 'reg'){  
  11.   if(!emptyempty($row['username'])){ //只要注册用户名为kaixin的时候,注册页面就会用红色字体提示次用户已被注册!  
  12.   echo 'no';  
  13.   }else{  
  14.   echo 'yes';  
  15.   }  
  16.  }  
  17. } 
  18. ?> 

conn.php数据库文件,代码如下:

  1. <?php 
  2. /* created on 下午12:08:25*/ 
  3.  $conn=@mysql_connect("localhost","root","")or die("Vevb.com提示你:连接失败!"); 
  4.  mysql_select_db("reg",$conn); 
  5.  mysql_query("set names 'gbk'"); 
  6. ?> 

registration数据表结构,代码如下:

  1. -- phpmyadmin sql dump 
  2. -- version 2.11.2.1 
  3. -- http://www.phpmyadmin.net 
  4. -- 
  5. -- 主机: localhost 
  6. -- 生成日期: 2009 年 05 月 20 日 05:29 
  7. -- 服务器版本: 5.0.45 
  8. -- php 版本: 5.2.5 
  9. set sql_mode="no_auto_value_on_zero"; 
  10. -- 
  11. -- 数据库: `reg` 
  12. -- 
  13. -- -------------------------------------------------------- 
  14. -- 
  15. -- 表的结构 `registration` phpmyadmin导入数据 
  16. -- 
  17. create table `registration` ( 
  18.   `id` tinyint(6) not null auto_increment, 
  19.   `username` varchar(14) not null comment '注册用户名', 
  20.   `userpwd` varchar(14) not null comment '注册密码', 
  21.   primary key  (`id`) 
  22. ) engine=innodb  default charset=gb2312 auto_increment=6 ; 
  23. -- 
  24.  
  25. -- 导出表中的数据 `registration` 
  26. -- 
  27. insert into `registration` (`id`, `username`, `userpwd`) values 
  28. (1, 'admin', 'admin888'), 
  29. (2, 'lyn', 'Vevb.com'), 
  30. (3, 'xiaot', 'xiaot'), 
  31. (4, 'xiaoe', 'xiaoe'), 
  32. (5, '我爱www.Vevb.com', '5201314110');

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表