ホームページ  >  記事  >  バックエンド開発  >  登録後のメールによるphpのアクティベーションと検証のサンプルコード

登録後のメールによるphpのアクティベーションと検証のサンプルコード

怪我咯
怪我咯オリジナル
2017-07-07 10:24:181406ブラウズ

メール認証の有効化方法を使用すると、悪意のあるスパムや登録ロボットのアクセスを効果的に防ぐことができます。 PHP を使用して登録後の電子メールの検証とアクティベーションの手順を記述するのは非常に簡単で、数分で習得できると思います。

register.php と verify.php の合計 2 ページが必要です

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のアクティベーションと検証のサンプルコードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。