首頁  >  文章  >  後端開發  >  php 註冊後用Email啟動驗證的範例程式碼

php 註冊後用Email啟動驗證的範例程式碼

怪我咯
怪我咯原創
2017-07-07 10:24:181461瀏覽

透過使用Email驗證啟動的方法,可以有效的幫你阻止惡意的Spam和註冊機器人的存取。 用php寫註冊後Email驗證啟動的步驟非常簡單,相信幾分鐘之內你就能學會。

總共需要兩個頁面,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 (&#39;$username&#39;, &#39;$password&#39;, &#39;$email&#39;, &#39;$activationKey&#39;, &#39;verify&#39;)";

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 = &#39;From: noreply@ YOURWEBSITE.com&#39; . "\r\n" .  
     &#39;Reply-To: noreply@ YOURWEBSITE.com&#39; . "\r\n" .  
     &#39;X-Mailer: PHP/&#39; . phpversion();  
 mail($to, $subject, $message, $headers);

5. 驗證啟動程式碼verify.php
如果驗證碼相同,則啟動使用者。

 程式碼如下:

 $queryString = $_SERVER[&#39;QUERY_STRING&#39;];
 $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 = &#39;&#39;, status=&#39;activated&#39; WHERE (id = $row[id])";         
        if (!mysql_query($sql)) {
           die(&#39;Error: &#39; . mysql_error());
        }          
         // 到这里,用户已经完全激活了账号,你可以将页面跳转到登陆后的界面了  
     }
   } // end of while

以上是php 註冊後用Email啟動驗證的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn