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

用Php編寫注冊後Email激活驗證的實例代碼

編輯:關於PHP編程

總共需兩個頁面,register.php 和 verify.php

1. 用戶注冊表格 register.php

復制代碼 代碼如下:
 <html>

 <body>

   <form action="register.php" method="post" name="register">

      用戶名:<input type="text" name="username" />

      密碼:<input type="password" name="password" />

      電子郵件:<input type="text" name="email" />

      <input type="submit" value="注冊" />

   </form>

 </body>

 </html>

2. 創建用戶數據表格 Users
復制代碼 代碼如下:
 CREATE TABLE IF NOT EXISTS `users` (

   `id` int(11) NOT NULL auto_increment,

   `status` varchar(20) NOT NULL,

   `username` varchar(20) NOT NULL,

   `password` varchar(20) NOT NULL,

   `email` varchar(20) NOT NULL,

   `activationkey` varchar(100) NOT NULL,

   PRIMARY KEY  (`id`),

   UNIQUE KEY `username` (`username`),

   UNIQUE KEY `email` (`email`),

   UNIQUE KEY `activationkey` (`activationkey`)

 ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

3. 創建驗證碼 用戶注冊信息存入數據表
我們使用狀態‘verify' 來表示尚未激活的用戶。

復制代碼 代碼如下:
 $activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();

 $username = mysql_real_escape_string($_POST[username]);

 $password = mysql_real_escape_string($_POST[password]);

 $email = mysql_real_escape_string($_POST[email]);  

 $sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";

4. 發送驗證碼
復制代碼 代碼如下:
 echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";

 ##Send activation Email

 $to      = $_POST[email];

 $subject = " YOURWEBSITE.com Registration";

 $message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team";

 $headers = 'From: noreply@ YOURWEBSITE.com' . "\r\n" .  

     'Reply-To: noreply@ YOURWEBSITE.com' . "\r\n" .  

     'X-Mailer: PHP/' . phpversion();  

 mail($to, $subject, $message, $headers);

5. 驗證激活代碼 verify.php
如果驗證碼相同,則激活用戶。

復制代碼 代碼如下:
 $queryString = $_SERVER['QUERY_STRING'];

 $query = "SELECT * FROM users";

 $result = mysql_query($query) or die(mysql_error());

 while($row = mysql_fetch_array($result)){  

     if ($queryString == $row["activationkey"]){

        echo "Congratulations!" . $row["username"] . " is now the proud new owner of a YOURWEBSITE.com account.";

        $sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";         

        if (!mysql_query($sql)) {

           die('Error: ' . mysql_error());

        }          

         // 到這裡,用戶已經完全激活了賬號,你可以將頁面跳轉到登陸後的界面了  

     }

   } // end of while

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