1. 程式人生 > >一個比較完整的登陸和註冊後臺

一個比較完整的登陸和註冊後臺

資料庫類

conn.php

<?php
class opmysql{
    private $host = 'localhost';            //伺服器地址
    private $name = 'root';                    //登入賬號
    private $pwd = '123456';                    //登入密碼
    private $dBase = 'db_reglog';            //資料庫名稱
    private $conn = '';                        //資料庫連結資源
    private $result = '';                    //結果集
    private $msg = '';                        //返回結果
    private $fields;                        //返回欄位
    private $fieldsNum = 0;                    //返回欄位數
    private $rowsNum = 0;                    //返回結果數
    private $rowsRst = '';                    //返回單條記錄的欄位陣列
    private $filesArray = array();            //返回欄位陣列
    private $rowsArray = array();            //返回結果陣列
    //初始化類
    function __construct($host='',$name='',$pwd='',$dBase=''){
        if($host != '')
            $this->host = $host;
        if($name != '')
            $this->name = $name;
        if($pwd != '')
            $this->pwd = $pwd;
        if($dBase != '')
            $this->dBase = $dBase;
        $this->init_conn();
    }
    //連結資料庫
    function init_conn(){
        $this->
[email protected]
_connect($this->host,$this->name,$this->pwd); @mysql_select_db($this->dBase,$this->conn); mysql_query("set names gb2312"); } //查詢結果 function mysql_query_rst($sql){ if($this->conn == ''){ $this->init_conn(); } $this->result = @mysql_query($sql,$this->conn); } //取得欄位數 function getFieldsNum($sql){ $this->mysql_query_rst($sql); $this->fieldsNum = @mysql_num_fields($this->result); } //取得查詢結果數 function getRowsNum($sql){ $this->mysql_query_rst($sql); if(mysql_errno() == 0){ return @mysql_num_rows($this->result); }else{ return ''; } } //取得記錄陣列(單條記錄) function getRowsRst($sql){ $this->mysql_query_rst($sql); if(mysql_error() == 0){ $this->rowsRst = mysql_fetch_array($this->result,MYSQL_ASSOC); return $this->rowsRst; }else{ return ''; } } //取得記錄陣列(多條記錄) function getRowsArray($sql){ $this->mysql_query_rst($sql); if(mysql_errno() == 0){ while($row = mysql_fetch_array($this->result,MYSQL_ASSOC)) { $this->rowsArray[] = $row; } return $this->rowsArray; }else{ return ''; } } //更新、刪除、新增記錄數 function uidRst($sql){ if($this->conn == ''){ $this->init_conn(); } @mysql_query($sql); $this->rowsNum = @mysql_affected_rows(); if(mysql_errno() == 0){ return $this->rowsNum; }else{ return ''; } } //獲取對應的欄位值 function getFields($sql,$fields){ $this->mysql_query_rst($sql); if(mysql_errno() == 0){ if(mysql_num_rows($this->result) > 0){ $tmpfld = @mysql_fetch_row($this->result); $this->fields = $tmpfld[$fields]; } return $this->fields; }else{ return ''; } } //錯誤資訊 function msg_error(){ if(mysql_errno() != 0) { $this->msg = mysql_error(); } return $this->msg; } //釋放結果集 function close_rst(){ mysql_free_result($this->result); $this->msg = ''; $this->fieldsNum = 0; $this->rowsNum = 0; $this->filesArray = ''; $this->rowsArray = ''; } //關閉資料庫 function close_conn(){ $this->close_rst(); mysql_close($this->conn); $this->conn = ''; } } $conne = new opmysql();//新建資料連線源 ?>


index.php

實現cookie自動登陸

<?php
    session_start();
    header('Content-Type:text/html;charset=gb2312');
    if(!empty($_COOKIE['name']) and !is_null($_COOKIE['name'])){
        $_SESSION['name'] = $_COOKIE['name'];
        header('location:http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']).'/main.php');
    }else{
        header('location:http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']).'/login.php');
    }
?>

main.php

登陸成功後反饋的資訊

<?php
    session_start();
?>
<!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=gb2312" />
<title>登入成功</title>
</head>
<body>
<?php echo '歡迎光臨!'.$_SESSION['name']; ?>
</body>
</html>

login.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=gb2312" />
<title>使用者登入</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script language="javascript" src="js/login.js"></script>
<script language="javascript" src="js/xmlhttprequest.js"></script>
</head>
<body>
      使用者名稱<input id="lgname" name="lgname" type="text"/><br>
      密  碼<input id="lgpwd" name="lgpwd" type="password" /><br>
      驗證碼<input id="lgchk" type="text" maxlength="4" style="width:35px;">
      <img id='chkid' src=""/><a id="changea">看不清</a><br>
      <button id="lgbtn" >登陸</button>
      <button id="rgbtn">註冊</button>
      <button id="fdbtn">找回密碼</button>
      <input id="chknm" name="chknm" type="hidden" value=""  />
</body>
</html>

Ajax初始化

xmlhttprequest.js

var xmlhttp = false;
if (window.XMLHttpRequest) {                                     //Mozilla、Safari等瀏覽器
    xmlhttp = new XMLHttpRequest();
} 
else if (window.ActiveXObject) {                                 //IE瀏覽器
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (e) {}
    }
}



login.js

登陸介面JS

// JavaScript Document
function $(id){
    return document.getElementById(id);
}
window.onload = function(){
    showval();
    $('lgname').focus();
    
    $('lgname').onkeydown = function(){//按回車向下移動游標
        if(event.keyCode == 13){
            $('lgpwd').select();
        }
    }
    $('lgpwd').onkeydown = function(){
        if(event.keyCode == 13){
            $('lgchk').select();
        }
    }
    $('lgchk').onkeydown = function(){
        if(event.keyCode == 13){
             chklg();
        }
    }
    $('lgbtn').onclick = chklg;
    function chklg(){
        if($('lgname').value.match(/^[a-zA-Z_]\w*$/) == null){
            alert('請輸入合法名稱');
            $('lgname').select();
            return false;
        }
        if($('lgname').value == ''){
            alert('請輸入使用者名稱!');
            $('lgname').focus();
            return false;
        }
        if($('lgpwd').value == ''){
            alert('請輸入密碼!');
            $('lgpwd').focus();
            return false;
        }
        if($('lgchk').value == ''){
            alert('請輸入驗證碼');
            $('lgchk').select();
            return false;
        }
        if($('lgchk').value != $('chknm').value){
            alert('驗證碼輸入錯誤');
            $('lgchk').select();
            return false;
        }
        //防SQL注入式攻擊(
        count = document.cookie.split(';')[0];
        if(count.split('=')[1] >= 3){
            alert('因為您的非法操作,您將無法再執行登入操作');
            return false;
        }
        //)
        //$('regimg').style.visibility = "visible";//顯示登陸的進度條
        //Ajax傳送與接受(
        url = 'login_chk.php?act='+(Math.random())+'&name='+$('lgname').value+'&pwd='+$('lgpwd').value;
        xmlhttp.open('get',url,true);
        
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4){
                if(xmlhttp.status == 200){
                    msg = xmlhttp.responseText;
                    if(msg == '0'){
                        alert('您還沒有啟用,請先登入郵箱進行啟用操作。');
                    }else if(msg == '1'){
                        alert('使用者名稱或密碼輸入錯誤,您還有2次機會');
                        $('lgpwd').select();
                    }else if(msg == '2'){
                        alert('使用者名稱或密碼輸入錯誤,您還有1次機會');
                        $('lgpwd').select();
                    }else if(msg == '3'){
                        alert('因為登入次數過多,您的帳號已被凍結,請聯絡管理員');
                        $('lgname').select();
                    }else if(msg == '4'){
                        alert('使用者名稱輸入錯誤');
                        $('lgname').select();
                    }else if(msg == '-1'){
                        alert('登入成功');
                        location = 'main.php';
                    }else{
                        alert(msg);
                    }
                    //$('regimg').style.visibility = "hidden";
                }
            }
        }
        xmlhttp.send(null);
    }
    //)
    $('changea').onclick = showval;//重新整理驗證碼
    //生成驗證碼(
    function showval(){
        num = '';
        for(i=0;i<4;i++){
            tmp =  Math.ceil((Math.random() * 15));
            if(tmp > 9){
                switch(tmp){
                    case(10):
                        num += 'a';
                        break;
                    case(11):
                        num += 'b';
                        break;
                    case(12):
                        num += 'c';
                        break;
                    case(13):
                        num += 'd';
                        break;
                    case(14):
                        num += 'e';
                        break;
                    case(15):
                        num += 'f';
                        break;
                }
            }else{
                num += tmp;
            }
        }
        $('chkid').src='valcode.php?num='+num;
        $('chknm').value = num;
    }
    //)
    $('fdbtn').onclick = function(){
        fd  = window.open('found.php','found','width=300,height=200');//在新視窗中開啟
        fd.moveTo(screen.width/2,200);
    }
    $('rgbtn').onclick = function(){
        open('register.php','_parent','',false);//直接開啟
    }
}

login_chk.php

Ajax登陸後臺驗證

<?php
    session_start();
    header('Content-Type:text/html;charset=gb2312');
    include_once 'conn/conn.php';
    $name = addslashes($_GET['name']);
    $pwd = $_GET['pwd'];
    if(!empty($name) and !empty($pwd)){
        $sql = "select name,count,active from tb_member where name = '".$name."'";
        $active = $conne->getFields($sql,2);
        $count = $conne->getFields($sql,1);
        $conne->close_rst();
        if($active == ''){
            if(!isset($_COOKIE['count']) or $_COOKIE['count'] == 0){
                setcookie('count',1);
            }else{
                setcookie('count',$_COOKIE['count']+1);
            }
            $reback = 4;
        }else if($active == 0){//判斷使用者名稱是否被啟用
            $reback = '0';
        }else if($count >= 3){//判斷使用者的登陸次數
            $reback = '3';
        }else{
            $sql .= " and password = '".md5($pwd)."'";
            $num = $conne->getRowsNum($sql);
            if($num == 0 or $num == ''){//如果使用者名稱密碼錯誤,登入次數加1
                $num = $conne->uidRst("update tb_member set count = ".($count+1)." where name = '".$name."'");
                $reback = ($count+1);
            }else{//登陸成功,清空count欄位值
                if($count != 0){
                    $num = $conne->uidRst("update tb_member set count = 0 where name = '".$name."'");
                }
                //設定COOKIE
                if(isset($_COOKIE['count']) and $_COOKIE['count'] != 0){
                    setcookie('count',0);
                }
                setcookie('name',$name,time()+60*10);
                $_SESSION['name'] = $name;
                $reback = '-1';
            }
        }
    }
    echo $reback;
?>


生成驗證碼圖片

valcode.php

<?php
    //header("content-type:image/png");
    $num = $_GET['num'];
    $imagewidth=60;
    $imageheight=18;

    $numimage = imagecreate($imagewidth,$imageheight);
    imagecolorallocate($numimage,240,240,240);
    for($i=0;$i<strlen($num);$i++){
        $x = mt_rand(1,8)+$imagewidth*$i/4;
        $y = mt_rand(1,$imageheight/4);
        $color=imagecolorallocate($numimage,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
        imagestring($numimage,5,$x,$y,$num[$i],$color);
    }

    for($i=0;$i<200;$i++){
          $randcolor=imagecolorallocate($numimage,rand(200,255),rand(200,255),rand(200,255));
          imagesetpixel($numimage,rand()%70,rand()%20,$randcolor); 
    }
    imagepng($numimage);
    imagedestroy($numimage);
?>


註冊頁面
register.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=gb2312" />
<title>註冊</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script language="javascript" src="js/xmlhttprequest.js"></script>
<script language="javascript" src="js/register.js"></script>
</head>
<body>
<div id="container">
  <div id="rgbgdiv">
    <div id="regnamediv"><b>註冊名稱:</b>
      <input id="regname" name="regname" type="text" />
      <div id="namediv">請輸入使用者名稱</div>
    </div>
    <div id="regpwddiv1"><b>註冊密碼:</b>
      <input id="regpwd1" name="regpwd1" type="password" />
      <div id="pwddiv1">請輸入密碼</div>
    </div>
    <div id="regpwddiv2"><b>確認密碼:</b>
      <input id="regpwd2" name="regpwd2" type="password" />
      <div id="pwddiv2">請輸入確認密碼</div>
    </div>
    <div id="regemaildiv"><b>電子郵箱:</b>
      <input id="email" name="email" type="text" />
      <div id="emaildiv">使用者啟用和找回密碼使用</div>
    </div>
    <div id="morediv" style="display:none;">
      <hr id="part" />
      <div id="regquestiondiv"><b>密保問題:</b>
        <input id="question" name="question" type="text" />
        <div id="questiondiv">使用者啟用和找回密碼使用</div>
      </div>
      <div id="reganswerdiv"><b>密保答案:</b>
        <input id="answer" name="answer" type="text" />
        <div id="answerdiv">使用者啟用和找回密碼使用</div>
      </div>
      <div id="regrealnamediv"><b>真實姓名:</b>
        <input id="realname" name="realname" type="text" />
        <div id="realnamediv">使用者的真實姓名</div>
      </div>
      <div id="regbirthdaydiv"><b>出生日期:</b>
        <input id="birthday" name="birthday" type="text" />
        <div id="birthdaydiv">使用者的出生日期。格式:YYYY-MM-DD</div>
      </div>
      <div id="regtelephonediv"><b>聯絡電話:</b>
        <input id="telephone" name="telephone" type="text" />
        <div id="telephonediv">使用者的聯絡電話</div>
      </div>
      <div id="regqqdiv"><b>QQ號 碼:</b>
        <input id="qq" name="qq" type="text" />
        <div id="qqdiv">使用者QQ號</div>
      </div>
    </div>
    <div id="btndiv2">
      <button id="regbtn" disabled="disabled"> </button>
      <button id="morebtn"> </button>
      <button id="logbtn"> </button>
    </div>
  </div>
  <div id="imgdiv" style=" visibility: hidden;"> </div>
</div>
</body>
</html>

 

註冊Ajax

register.js

// JavaScript Document
function $(id){
    return document.getElementById(id);
}
window.onload = function(){
    $('regname').focus();
    var cname1,cname2,cpwd1,cpwd2,cemail;
    //設定啟用按鈕
    function chkreg(){
        if((cname1 == 'yes') && (cname2 == 'yes') && (cpwd1 == 'yes') && (cpwd2 == 'yes') && (cemail == 'yes')){
            $('regbtn').disabled = false;
        }else{
            $('regbtn').disabled = true;
        }
    }
    //驗證使用者名稱
    $('regname').onkeyup = function (){
        name = $('regname').value;
        cname2 = '';
        if(name.match(/^[a-zA-Z_]*/) == ''){
            $('namediv').innerHTML = '<font color=red>必須以字母或下劃線開頭</font>';
            cname1 = '';
        }else if(name.length < 2){
            $('namediv').innerHTML = '<font color=red>註冊名稱必須大於等於2位</font>';
            cname1 = '';
        }else{
            $('namediv').innerHTML = '<font color=green>註冊名稱符合標準</font>';
            cname1 = 'yes';
        }
        chkreg();
    }
    //驗證是否存在該使用者
    $('regname').onblur = function(){
        name = $('regname').value;
        if(cname1 == 'yes'){
            xmlhttp.open('get','chkname.php?name='+name,true);
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4){
                    if(xmlhttp.status == 200){
                        var msg = xmlhttp.responseText;
                        if(msg == '1'){
                            $('namediv').innerHTML="<font color=green>恭喜您,該使用者名稱可以使用!</font>";
                            cname2 = 'yes';
                        }else if(msg == '2'){
                            $('namediv').innerHTML="<font color=red>使用者名稱被佔用!</font>";
                            cname2 = '';
                        }else{
                            $('namediv').innerHTML="<font color=red>"+msg+"</font>";
                            cname2 = '';
                        }
                    }
                }
                chkreg();
            }
            xmlhttp.send(null);
        }
    }
    //驗證密碼
    $('regpwd1').onkeyup = function(){
        pwd = $('regpwd1').value;
        pwd2 = $('regpwd2').value;
        if(pwd.length < 6){
            $('pwddiv1').innerHTML = '<font color=red>密碼長度最少需要6位</font>';
            cpwd1 = '';
        }else if(pwd.length >= 6 && pwd.length < 12){
            $('pwddiv1').innerHTML = '<font color=green>密碼符合要求。密碼強度:弱</font>';
            cpwd1 = 'yes';
        }else if((pwd.match(/^[0-9]*$/)!=null) || (pwd.match(/^[a-zA-Z]*$/) != null )){
            $('pwddiv1').innerHTML = '<font color=green>密碼符合要求。密碼強度:中</font>';
            cpwd1 = 'yes';
        }else{
            $('pwddiv1').innerHTML = '<font color=green>密碼符合要求。密碼強度:高</font>';
            cpwd1 = 'yes';
        }
        if(pwd2 != '' && pwd != pwd2){
            $('pwddiv2').innerHTML = '<font color=red>兩次密碼不一致!</font>';
            cpwd2 = '';
        }else if(pwd2 != '' && pwd == pwd2){
            $('pwddiv2').innerHTML = '<font color=green>密碼輸入正確</font>';
            cpwd2 = 'yes';
        }
        chkreg();
    }
    //驗證確認密碼
    $('regpwd2').onkeyup = function(){
        pwd1 = $('regpwd1').value;
        pwd2 = $('regpwd2').value;
        if(pwd1 != pwd2){
            $('pwddiv2').innerHTML = '<font color=red>兩次密碼不一致!</font>';
            cpwd2 = '';
        }else{
            $('pwddiv2').innerHTML = '<font color=green>密碼輸入正確</font>';
            cpwd2 = 'yes';
            chkreg();
        }
    }
    //驗證email
    $('email').onkeyup = function(){
        emailreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        $('email').value.match(emailreg);
        if($('email').value.match(emailreg) == null){
            $('emaildiv').innerHTML = '<font color=red>錯誤的email格式</font>';
            cemail = '';
        }else{
            $('emaildiv').innerHTML = '<font color=green>輸入正確</font>';
            cemail = 'yes';
        }
        chkreg();
    }

    //顯示/隱藏詳細資訊
    $('morebtn').onclick = function(){
        
        if($('morediv').style.display == ''){
            $('morediv').style.display = 'none';
        }else{
            $('morediv').style.display = '';
        }
    }
    //登入按鈕
    $('logbtn').onclick = function(){
        window.open('login.php','_parent','',false);
    }
    //正式註冊
    $('regbtn').onclick = function(){
        $('imgdiv').style.visibility = 'visible';
        url = 'register_chk.php?name='+$('regname').value+'&pwd='+$('regpwd1').value+'&email='+$('email').value;
        url += '&question=' +$('question').value+'&answer='+$('answer').value;
        url += '&realname=' +$('realname').value+'&birthday='+$('birthday').value;
        url += '&telephone='+$('telephone').value+'&qq='+$('qq').value;
        xmlhttp.open('get',url,true);
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4){
                if(xmlhttp.status == 200){
                    msg = xmlhttp.responseText;
                    if(msg == '1'){
                        alert('註冊成功,請到您的郵箱中獲取啟用碼!');
                        location='index.php';
                    }else if(msg == '-1'){
                        alert('您的伺服器不支援Zend_mail,或者郵箱填寫錯誤。請仔細檢查!!');
                    }else{
                        alert(msg);
                    }
                    $('imgdiv').style.visibility = 'hidden';
                }
            }
        }
        xmlhttp.send(null);
    }
}

註冊後臺處理,傳送驗證郵件需要下載Zend Framework

register_chk.php

<?php
    include_once 'conn/conn.php';
    require_once 'Zend/Mail.php';                        //呼叫傳送郵件的檔案
    require_once 'Zend/Mail/Transport/Smtp.php';        //呼叫SMTP驗證檔案
    $reback = '0';
    $url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']).'/activation.php';
    $url .= '?name='.trim($_GET['name']).'&pwd='.md5(trim($_GET['pwd']));
    
    //傳送啟用郵件
    $subject="啟用碼的獲取";
    $mailbody='註冊成功。您的啟用碼是:'.'<a href="'.$url.'" target="_blank">'.$url.'</a><br>'.'請點選該地址,啟用您的使用者!';
//定義郵件內容
    $envelope="[email protected]";        //定義登入使用的郵箱

    $config = array('auth' => 'login',
            'username' => 'mrsoft8888',
            'password' => 'mrsoft8888');                //定義SMTP的驗證引數
    $transport = new Zend_Mail_Transport_Smtp('smtp.sohu.com', $config);        //例項化驗證的物件
    $mail = new Zend_Mail('GBK');            //例項化傳送郵件物件
    $mail->setBodyHtml($mailbody);                //傳送郵件主體
    $mail->setFrom($envelope, '明日科技典型模組程式測試郵箱,恭喜您使用者註冊成功!');    //定義郵件傳送使用的郵箱
    $mail->addTo($_GET['email'], '獲取使用者註冊啟用碼');        //定義郵件的接收郵箱
    $mail->setSubject('獲取註冊使用者的啟用碼');                //定義郵件主題
    $mail->send($transport);                                //執行傳送操作
    
/*   網路版傳送郵件方法  */    
    $birthday='0000-00-00';
    if($_GET['birthday']!='')
        $birthday=$_GET['birthday'];
    $sql = "insert into tb_member(name,password,question,answer,email,realname,birthday,telephone,qq) values('".trim($_GET['name'])."','".md5(trim($_GET['pwd']))."','".$_GET['question']."','".$_GET['answer']."','".$_GET['email']."','".$_GET['realname']."','".$birthday."','".$_GET['telephone']."','".$_GET['qq']."')";
        $num = $conne->uidRst($sql);
        if($num == 1){
            $reback = '1';
        }
    echo $reback;
?>

驗證使用者名稱是否被佔用

chkname.php

<?php
include_once "conn/conn.php";
$sql = "select * from tb_member where name='".$_GET['name']."'";
$num = $conne->getRowsNum($sql);
if($num == 1){
    echo '2';
}else if($num == 0){
    echo '1';
}else{
    echo $conne->msg_error();
}
?>


找回密碼的頁面
found.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=gb2312" />
<title>找回密碼</title>
<link rel="stylesheet" href="css/style.css" />
<script language="javascript" src="js/found.js"></script>
<script language="javascript" src="js/xmlhttprequest.js"></script>
</head>
<body>
<div id="fdbgdiv" >
    <div id="top"> >>密碼找回</div>
    <div id="foundnamediv">找回賬號: <input id="foundname" type="text" style=" width: 100px; height:15px; border:1px #000000 solid;" /></div>
    <div id="foundnamediv">密保問題: <input id="fdquestion" type="text" style=" width: 100px; height:15px; border:1px #000000 solid;" /></div>
  <div id="foundnamediv">密保答案: <input id="fdanswer" type="text" style=" width: 100px; height:15px; border:1px #000000 solid;" /></div>
  <div id="foundnamediv" align="center"><button id="step1"></button></div>
</div>
</body>
</html>

找回密碼的Ajax

found.js

// JavaScript Document
function $(id){
    return document.getElementById(id);
}
window.onload = function(){
    $('foundname').focus();
    $('step1').onclick = function(){
        if($('foundname').value != '' && $('fdquestion').value != '' && $('fdanswer').value != ''){
            xmlhttp.open('get','found_chk.php?foundname='+$('foundname').value+'&question='+$('fdquestion').value+'&answer='+$('fdanswer').value,true);
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                    msg = xmlhttp.responseText;
                    if(msg == '1'){
                        alert('找回密碼成功,請登入郵箱註冊郵箱!');
                        window.close();
                    }else{
                        alert('填寫的資訊錯誤');
                    }
                }
            }
            xmlhttp.send(null);
        }else{
            alert('請填寫完成資訊');
            $('foundname').focus();
            return false;
        }
    }
}

found_chk.php

找回密碼的後臺,傳送包含密碼的郵件,與註冊時類似

<?php
    include_once 'conn/conn.php';
    require_once 'Zend/Mail.php';                        //呼叫傳送郵件的檔案
    require_once 'Zend/Mail/Transport/Smtp.php';        //呼叫SMTP驗證檔案
    $reback = '0';
    $name = $_GET['foundname'];
    $question = $_GET['question'];
    $answer = $_GET['answer'];
    $sql = "select email from tb_member where name = '".$name."' and question = '".$question."' and answer = '".$answer."'";
    $email = $conne->getFields($sql,0);
    if($email != '')
    {
        $rnd = rand(1000,time());
        $sql = "update tb_member set password = '".md5($rnd)."' where name = '".$name."' and question = '".$question."' and answer = '".$answer."'";
        $tmpnum = $conne->uidRst($sql);
        if($tmpnum >= 1)
        {
            $subject="找回密碼";
            $mailbody='密碼找回成功。您帳號的新密碼是'.$rnd;
            $reback='1';
            $envelope="[email protected]";
            $config = array('auth' => 'login',
            'username' => 'mrsoft8888',
            'password' => 'mrsoft8888');                //定義SMTP的驗證引數
            $transport = new Zend_Mail_Transport_Smtp('smtp.sohu.com', $config);        //例項化驗證的物件
            $mail = new Zend_Mail('GBK');            //例項化傳送郵件物件
            $mail->setBodyHtml($mailbody);                //傳送郵件主體
            $mail->setFrom($envelope, '明日科技典型模組程式測試郵箱,修改使用者註冊密碼!');    //定義郵件傳送使用的郵箱
            $mail->addTo($email, '獲取使用者新密碼');        //定義郵件的接收郵箱
            $mail->setSubject($subject);                //定義郵件主題
            $mail->send($transport);    
        }
        else
        {
            $reback = $sql;
        }
    }
    echo $reback;
?>

利用郵件的超連結啟用頁面

activation.php

<?php
session_start();
header('Content-Type:text/html;charset=gb2312');
include_once("conn/conn.php");
if (!empty($_GET['name']) && !is_null($_GET['name'])){                //啟用註冊使用者
    $num=$conne->getRowsNum("select * from tb_member where name='".$_GET['name']."' and password = '".$_GET['pwd']."'");
    if ($num>0){
        $upnum=$conne->uidRst("update tb_member set active = 1 where name='".$_GET['name']."' and password = '".$_GET['pwd']."'");
        if($upnum > 0){
            $_SESSION['name'] = $_GET['name'];
            echo "<script>alert('使用者啟用成功!');window.location.href='main.php';</script>";
        }else{
            echo "<script>alert('您已經啟用!');window.location.href='main.php';</script>";
        }
        
    }else{
        echo "<script>alert('使用者啟用失敗!');window.location.href='register.php';</script>";
    }
}
?>


------------修改自<<PHP開發典型模組大全>>-------------------

詳細↓

http://www.cnblogs.com/xchaos/archive/2012/03/10/2389479.html