注册页面之前先验证用户名是否存在的php代码
程序员文章站
2022-06-03 10:28:42
reg.php 复制代码 代码如下:
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
复制代码 代码如下:
<!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 数据库树的遍历方法