程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 完整php ajax用戶注冊與用戶名驗證實例

完整php ajax用戶注冊與用戶名驗證實例

編輯:關於PHP編程

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>insert title here</title>
<script type="text/網頁特效">
function checkusername(input){
 var name = input.value;
 var url = 'checkusername.php教程?username=' + name;

 var xmlhttp = getxmlhttpobject();
 xmlhttp.onreadystatechange = function(){
  if (xmlhttp.readystate==4){
   $$("pmt_username").innerhtml = xmlhttp.responsetext;
  }
 }
 xmlhttp.open("get",url,true);
 xmlhttp.send(null);
}
function getxmlhttpobject(){
 var xmlhttp=null;
 try{
  // firefox, opera 8.0+, safari
  xmlhttp=new xmlhttprequest();
 }catch (e){
  // internet explorer
  try{
   xmlhttp=new activexobject("msxml2.xmlhttp");
  }catch (e){
   xmlhttp=new activexobject("microsoft.xmlhttp");
  }
 }
 return xmlhttp;
}
function checkform(form){
 try{
  assertnonempty(form.username);
  assertnonempty(form.pwd);
//  assertint(form.mobile);
 } catch(e){
  return false;
 }
 return true;
}
function assertnonempty(obj){
 if(obj.value == ''){
  var tds = gettdelems(obj);
  var text = tds[0].innerhtml;
  
  tds[1].innerhtml = text.slice(0,-1) + '不能為空';
  obj.focus();
  obj.select();
  
  throw new exception();
 }
}
function assertint(obj){
 var v = obj.value;
 if(/d{11}/.test(v)){
  alert('ok');
 } else {
  alert('error');
 }
 if(number(v) != v){
  var tds = gettdelems(obj);
  var text = tds[0].innerhtml;
  
  tds[1].innerhtml = text.slice(0,-1) + '應該是數值';
  
  obj.focus();
  obj.select();
  throw new exception();
 }
}
window.onload = function(){
// alert($$('username').type);
// $$('abc').onkeydown = mobilekeydown;
}
function gettdelems(obj){
 var tr = obj.parentnode.parentnode;
 var tds = tr.getelementsbytagname('td');
 return [tds[0],tds[2]];
}
function mobilekeydown(e){
 e = e || event;
 return e.keycode >47 && e.keycode < 59;
}

function $$(id){
 return document.getelementbyid(id);
}
function checkcode(input){
 var name = input.value;
 var url = 'checkcode.php?code=' + name;

 var xmlhttp = getxmlhttpobject();
 xmlhttp.onreadystatechange = function(){
  if (xmlhttp.readystate==4){
   $$("code").innerhtml = xmlhttp.responsetext;
  }
 }
 xmlhttp.open("get",url,true);
 xmlhttp.send(null);
}
</script>
</head>
<body>
<form method="post" action='register.php' enctype="multipart/form-data" onsubmit='return checkform(this)'>
 <table>
  <tr>
   <td>用戶名:</td>
   <td><input type='text' name='username' onblur='checkusername(this)'></td>
   <td id='pmt_username'></td>
  </tr>
  <tr>
   <td>密碼:</td>
   <td><input type='password' name='pwd'></td>
   <td id='pmt_pwd'></td>
  </tr>
  <tr>
   <td>重復密碼:</td>
   <td><input type='password' name='pwd2'></td>
   <td></td>
  </tr>
  <tr>
   <td>姓名:</td>
   <td><input type='text' name='name'></td>
   <td></td>
  </tr>
  <tr>
   <td>性別:</td>
   <td>
    <input id='sex_m' checked type='radio' name='sex' value='男'><label for='sex_m'> 男 </label>
    <label><input type='radio' name='sex' value='女'>女</label>
   </td>
   <td></td>
  </tr>
  <tr>
   <td>年齡:</td>
   <td><input type='text' name='age'></td>
   <td id='pmt_age'></td>
  </tr>
  <tr>
   <td>手機:</td>
   <td><input type='text' name='mobile'></td>
   <td></td>
  </tr>
  <tr>
   <td>通信地址:</td>
   <td><input type='text' name='address'></td>
   <td></td>
  </tr>
  <tr>
   <td>郵件地址:</td>
   <td><input type='text' name='email'></td>
   <td></td>
  </tr>
  <tr>
   <td>用戶照片:</td>
   <td><input type='file' name='photo'></td>
   <td></td>
  </tr>
  <tr>
   <td>出生日期:</td>
   <td><input type='text' name='birthday'></td>
   <td></td>
  </tr>
  <tr>
   <td>驗證碼:</td>
   <td><input type='text' name='verifycode' onblur='checkcode(this)'><img src='verifycode.php'></td>
   <td id='code'></td>
  </tr>
  <tr>
   <td colspan='3'><input type='submit' value='注冊'> <input type='reset' value='重置'></td>
  </tr>
 </table>
</form>

</body>
</html>

checkusername.php文件

<?php
 $username = $_get['username'];
 mysql教程_connect('127.0.0.1', 'root', '') or die('could not connect: ' . mysql_error());
 mysql_select_db('test');
 
 //拼接sql語句時必須將用戶輸入的值做處理,替換特殊字符,用引號包含
 $username = htmlentities($username,ent_quotes);
 $sql = "select count(*) from user where username ='{$username}'";
 
 $result = mysql_query($sql);
 if($row = mysql_fetch_array($result, mysql_num)){
  $cnt = $row[0];
  if($cnt == 0) {
   echo '用戶名未被使用';
  } else {
   echo '用戶名已被使用,請改換用戶名';
  }
 }
?>

verifycode.php驗證碼程序


<?php
session_start();

$rnd = array_merge(range(0,9),range('a','z'));
shuffle($rnd);

$code = implode('',$rnd);
$code = substr($code,0,4);

$_session['verifycode'] = $code;

header("content-type: image/png");
$im = @imagecreate(50, 25) or die("cannot initialize new gd image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 5, 5, 5,  $code, $text_color);
imagepng($im);
imagedestroy($im);


?>
checkcode.php 檢測用戶輸入的驗證碼是否一致
<?php
 session_start();
 
 $code = $_get['code'];
 
 echo strtoupper($code) == $_session['verifycode'] ? '驗證碼正確' : '驗證碼錯誤';
?>

register.php注冊處理程序

<?php
session_start();

$filename = '';
if(isset($_files['photo'])){
 $uploaddir = dirname(__file__) . directory_separator . 'upload';
 $originfilename = $_files['photo']['name'];
 $extname = strtolower(substr($originfilename,strrpos($originfilename,'.')+1));
 
 $filename = time() . '.' . $extname;
 
 $validext = array('jpg','jpeg','gif','png');
 if(!in_array($extname,$validext)){
  errormsg("錯誤的文件類型");
 }
 
 if($_files['photo']['size'] > 100*1024){
  errormsg("文件太大,超過了100k");
 }
 $uploadfile = $uploaddir . directory_separator . $filename;
 
 if(!move_uploaded_file($_files['photo']['tmp_name'], $uploadfile)) {
  errormsg("照片上傳失敗");
 }
}

$code = $_post['verifycode'];
if($code != $_session['verifycode']){
 errormsg("驗證碼輸入不正確");
}

mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('test');
mysql_query('set names utf8');

$username = addslashes($_post['username']);
$pwd = md5($_post['pwd']);
$name = addslashes($_post['name']);
$sex = addslashes($_post['sex']);
$age = intval($_post['age']);
$mobile = addslashes($_post['mobile']);
$address = addslashes($_post['address']);
$email = addslashes($_post['email']);
$photo = $filename;
$birthday = addslashes($_post['birthday']);

$sql = "insert into user(username,pwd,name,sex,age,mobile,address,email,photo,birthday)
 values('$username','$pwd','$name','$sex',$age,'$mobile','$address','$email','$photo','$birthday')";

if(!mysql_query($sql)){
 errormsg('數據庫教程寫入不成功!');
}

$sql = 'select * from user';
$res = mysql_query($sql);
echo '
 <table>
  <tr>
   <td>用戶名</td>
   <td>姓名</td>
   <td>性別</td>
   <td>年齡</td>
   <td>手機</td>
   <td>通信地址</td>
   <td>郵件地址</td>
   <td>出生日期</td>
<!--   <td>照片</td> -->
  </tr>
';
while($row = mysql_fetch_assoc($res)){
 $photo = $row['photo'] ? "<img src='./upload/{$row['photo']}'>" : '';
 echo "
  <tr>
   <td>{$row['username']}</td>
   <td>{$row['name']}</td>
   <td>{$row['sex']}</td>
   <td>{$row['age']}</td>
   <td>{$row['mobile']}</td>
   <td>{$row['address']}</td>
   <td>{$row['email']}</td>
   <td>{$row['birthday']}</td>
<!--   <td>$photo</td> -->
  </tr>
 ";
 
}

echo '</table>';


function errormsg($str){
 die('<script type="text/javascript">alert("' . $str . '");</script>');
}


?>

最簡單數據庫結構

drop database if exists test;
create database test character set utf8 collate utf8_general_ci;

use test;

create table user(
 username char(10) primary key
 ,pwd char(32) not null
 ,name char(10) not null
 ,sex char(1) not null
 ,mobile char(11)
 ,age smallint
 ,address varchar(50)
 ,email varchar(30)
 ,photo varchar(20)
 ,birthday date
);

http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22331.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved