注册页面之前先验证用户名是否存在的php代码


Posted in PHP onJuly 14, 2012

reg.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 
<link rel="stylesheet" type="text/css" href="css/int.css" /> 
<script type="text/javascript" src="js/func.js"></script> 
<style type="text/css"> 
td{ 
height:30px; 
vertical-align:middle; 
align:center; 
} 
#myText{ 
width:600px; 
} 
</style> 
<title>注册页面</title> 
</head> 
<body > 
<?php 
error_reporting(0); 
//不让PHP报告有错语发生。如果不关闭好有类似这的错语 Warning: preg_match() 关闭就不出现了 
session_start(); 
header("Cache-control: private"); 
$conn = @ mysql_connect("localhost","root","")or die("数据库连接错误"); 
mysql_select_db("bbs",$conn); 
mysql_query("set names utf8"); 
if($_POST['submit']) 
{ 
$username = $_POST["username"]; 
$sql="select userName from user_info where userName='$username'"; 
// echo $sql; 
$query=mysql_query($sql); 
$rows = mysql_num_rows($query); 
if($rows > 0){ 
echo "<script type='text/javascript'>alert('用户名已存在');location='javascript:history.back()';</script>"; 
}else{ 
$user_in = "insert into user_info (username,pass,sex,qq,email,img) values ('$_POST[username]',md5('$_POST[pass]'),'$_POST[sex]','$_POST[qq]','$_POST[email]','$_POST[img_select]')"; 
//echo $user_in; 
mysql_query($user_in); 
echo "<script type='text/javascript'>alert('写入成功!!');location.href='login.php';</script>"; 
} 
//javascript:history.go(-1) 
} 
?> 
<form action="reg.php" name="reg_form" method="post" onsubmit="return check_reg()"> 
<table name="reg_table" align="left"> 
<tr> 
<td>用户:</td><td><input id="username" name="username" class="myText" type="text" maxlength="12" /></td> 
</tr> 
<tr> <!--性别:0 保密 1 女 2 男--> 
<td > 性别:</td> 
<td>女<input type="radio" value="1" name="sex"/> 
男<input type="radio" value="2" name="sex" /> 
保密<input type="radio" value="0" name="sex" checked/></td> 
</tr> 
<tr> 
<td>密码:</td><td><input name="pass" class="myText" type="password" onblur="check_len(this)"/><span id="show_pass" style="color:red;"></span></td> 
</tr> 
<tr> 
<td>重复密码:</td><td><input name="repass" class="myText" type="password" onblur="check_pass(this)" /><span id="show_repass" style="color:red;"></span></td> 
</tr> 
<tr> 
<td>QQ:</td><td><input type="text" class="myText" name="qq" onblur="check_qq(this)"/><span style="color:red;" id="show_qq"></span></td> 
</tr> 
<tr> 
<td>邮箱:</td><td><input type="text" class="myText" name="email" onblur="check_email(this)"/><span id="show_e" style="color:red;"></span></td> 
</tr> 
<tr> 
<td height="60">头像:</td> 
<td> 
<select name="img_select" onchange="img_change(this)"> 
<option value="101" >女 001</option> 
<option value="102" >女 002</option> 
<option value="103" >女 003</option> 
<option value="104" >女 004</option> 
<option value="105" >男 001</option> 
<option value="106" >男 002</option> 
<option value="107" >男 003</option> 
<option value="108" >男 004</option> 
</select> 
<img src="/bbs/img/101.gif" id="tx_change" style="width:50px; height:65px;" alt=""/> 
</td> 
</tr> 
<tr height="20" align="justify"> 
<td align="right" ><input type="submit" value="注册" name="submit" style="margin-right:5px;"/></td> 
<td><input type="reset" value="重置" name="reset" style="margin-left:5px;"/></td> 
</tr> 
<tr> 
<td colspan="2">我已有账号现在<a href="login.php">登录</a></td> 
</tr> 
</table> 
</form> 
</body> 
</html>

func.js
//根据下拉框变换图片 
function img_change(thisObj){ 
var imgsrc = "/bbs/img/"+ thisObj.value+".gif"; 
document.getElementById("tx_change").src=imgsrc; 
} 
//检查是否都符合 注册 要求 
function check_reg() 
{ 
if(check_len() && check_pass() && check_email() && check_qq()) 
{ 
return true; 
}else{ 
return false; 
} 
} 
//检查密码长度不能少于6 
function check_len(thisObj){ 
if(thisObj.value.length==0) 
{ 
document.getElementById('show_pass').innerHTML="密码不能为空"; 
return false; 
}else{ 
if (thisObj.value.length<6) 
{ 
document.getElementById('show_pass').innerHTML="密码长度不少于6"; 
return false; 
} 
document.getElementById('show_pass').innerHTML=""; 
return true; 
} 
} 
//检查俩次密码输入是否一致 
function check_pass(thisObj){ 
var psw=document.getElementById('pass'); 
if(psw.value.length==0) 
{ 
document.getElementById('show_pass').innerHTML="密码不能为空"; 
return false; 
}else{ 
document.getElementById('show_pass').innerHTML=""; 
if (thisObj.value!=psw.value) 
{ 
document.getElementById('show_repass').innerHTML="两次密码输入不正确"; 
return false; 
} 
document.getElementById('show_repass').innerHTML=""; 
return true; 
} 
} 
//检查email是否正确 
function check_email(thisObj){ 
var reg=/^([a-zA-Z\d][a-zA-Z0-9_]+@[a-zA-Z\d]+(\.[a-zA-Z\d]+)+)$/gi; 
var rzt=thisObj.value.match(reg); 
if(thisObj.value.length==0){ 
document.getElementById('show_e').innerHTML="Email不能为空"; 
return false; 
}else{ 
if (rzt==null) 
{ 
document.getElementById('show_e').innerHTML="Email地址不正确"; 
return false; 
} 
document.getElementById('show_e').innerHTML=""; 
return true; 
} 
} 
//检查qq格式是否正确 
function check_qq(thisObj){ 
var qq=document.getElementById('qq').value; 
var reg=/^\d+$/; 
if(qq.search(reg)) 
{ 
document.getElementById('show_qq').innerHTML=" QQ 只能为数字"; 
return false; 
}else{ 
document.getElementById('show_qq').innerHTML=""; 
return true ; 
} 
}

作者: sweet__smile
PHP 相关文章推荐
PHP统计目录下的文件总数及代码行数(去除注释及空行)
Jan 17 PHP
php入门学习知识点六 PHP文件的读写操作代码
Jul 14 PHP
php模拟js函数unescape的函数代码
Oct 20 PHP
PHP-redis中文文档介绍
Feb 07 PHP
PHP的curl实现get,post和cookie(实例介绍)
Jun 17 PHP
ThinkPHP CURD方法之data方法详解
Jun 18 PHP
在WordPress中使用PHP脚本来判断访客来自什么国家
Dec 10 PHP
PHPCMS手机站伪静态设置详细教程
Feb 06 PHP
Yii2 hasOne(), hasMany() 实现三表关联的方法(两种)
Feb 15 PHP
PHP获取链表中倒数第K个节点的方法
Jan 18 PHP
PHP 布尔值的自增与自减的实现方法
May 03 PHP
PHP基于swoole多进程操作示例
Aug 12 PHP
php学习之function的用法
Jul 14 #PHP
php 操作调试的方法
Jul 12 #PHP
php输出xml格式字符串(用的这个)
Jul 12 #PHP
php长字符串定义方法
Jul 12 #PHP
PHP数据库调用类调用实例(详细注释)
Jul 12 #PHP
PHP中extract()函数的妙用分析
Jul 11 #PHP
Zend Framework中的简单工厂模式 图文
Jul 10 #PHP
You might like
使用openssl实现rsa非对称加密算法示例
2014/01/24 PHP
解决CodeIgniter伪静态失效
2014/06/09 PHP
php使用fputcsv()函数csv文件读写数据的方法
2015/01/06 PHP
php获取远程文件内容的函数
2015/11/02 PHP
PHP的时间戳与具体时间转化的简单实现
2016/06/13 PHP
详解PHP多个进程配合redis的有序集合实现大文件去重
2019/03/06 PHP
PHP快速导出百万级数据到CSV或者EXCEL文件
2020/11/27 PHP
avascript中的自执行匿名函数应用示例
2014/09/15 Javascript
AngularJS学习笔记之基本指令(init、repeat)
2015/06/16 Javascript
javascript中使用未定义变量或值的情况分析
2016/07/19 Javascript
JavaScript职责链模式概述
2016/09/17 Javascript
在Web项目中引入Jquery插件报错的完美解决方案(图解)
2016/09/19 Javascript
bootstrap导航栏、下拉菜单、表单的简单应用实例解析
2017/01/06 Javascript
JavaScript算法教程之sku(库存量单位)详解
2017/06/29 Javascript
vue.js实例todoList项目
2017/07/07 Javascript
Vue之beforeEach非登录不能访问的实现(代码亲测)
2019/07/18 Javascript
vue prop属性传值与传引用示例
2019/11/13 Javascript
JavaScript装饰者模式原理与用法实例详解
2020/03/09 Javascript
[00:08]DOTA2勇士令状等级奖励“天外飞星”
2019/05/24 DOTA
[01:18]PWL开团时刻DAY10——一拳超人
2020/11/11 DOTA
python写日志封装类实例
2015/06/28 Python
Python中断言Assertion的一些改进方案
2016/10/27 Python
Python网络爬虫与信息提取(实例讲解)
2017/08/29 Python
关于python的list相关知识(推荐)
2017/08/30 Python
Python并发编程协程(Coroutine)之Gevent详解
2017/12/27 Python
python实现简单的单变量线性回归方法
2018/11/08 Python
python 两个数据库postgresql对比
2019/10/21 Python
Smashbox官网:美国知名彩妆品牌
2017/01/05 全球购物
美国眼镜在线零售商:Dualens
2019/12/07 全球购物
英国第一独立滑雪板商店:The Snowboard Asylum
2020/01/16 全球购物
公务员爱岗敬业演讲稿
2014/08/26 职场文书
我爱祖国演讲稿
2014/09/02 职场文书
作文评语怎么写
2014/12/25 职场文书
开会迟到检讨书范文
2015/05/06 职场文书
2015年小学体育教师工作总结
2015/10/23 职场文书
学生会2016感恩节活动小结
2016/04/01 职场文书