Nhiều khi tự hỏi, không biết cơ chế xác nhận email khi chúng ta đăng ký một tài khoản trên một website như thế nào? Sau một hồi tìm hiểu, thì đã có bài viết lôi về cho anh em cùng học tập đây.
Bài viết được dịch từ 9lession, nội dung chủ yếu giúp chúng ta giải quyết vấn đề: Làm thế nào để gửi một email có nội dung kích hoạt tài khoản cho user mới đăng ký.
Database
Tạo bảng user với các trường sau đây
1 2 3 4 5 6 7 8 | CREATE TABLE IF NOT EXISTS `users` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(300) NOT NULL UNIQUE, `password` varchar(300) NOT NULL, `activation` varchar(300) NOT NULL UNIQUE, `status` enum('0','1') NOT NULL DEFAULT '0', PRIMARY KEY (`uid`) ) |
Bảng CSDL lưu trữ các thông tin của người dùng:
- uid – ID của user
- email – email của user
- password – Password của user
- activation – Mã kích hoạt của user
- status – Trạng thái, đã kích hoạt hoặc chưa
Bộ khung HTML code
Trước tiên phải thiết kế một đoạn code tạo một form đăng ký. Các bạn có thể tham khảo để tự mình thiết kế form đăng ký đẹp mắt. Form này có các trường thông tin cơ bản như: email, password, nút submit và thẻ msg hiển thị thông tin khi đăng ký xong.
1 2 3 4 5 6 7 8 | <form action="" method="post"> <label>Email</label> <input type="text" name="email" class="input" autocomplete="off"/> <label>Password </label> <input type="password" name="password" class="input" autocomplete="off"/><br/> <input type="submit" class="button" value="Registration" /> <span class='msg'><?php echo $msg; ?></span> </form> |
Bạn có thể tùy chỉnh style cho nó đẹp thông qua một số bài viết dưới đây
- Hướng dẫn thiết kế form login đẹp mắt
- Hướng dẫn thiết kế form login giống Facebook
- Thiết kế trang đăng nhập với CSS
DB.php
File này dùng để tạo kết nối tới cơ sở dữ liệu bằng PHP
1 2 3 4 5 6 7 8 | <?php define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'username'); define('DB_PASSWORD', 'password'); define('DB_DATABASE', 'database'); $connection = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); $base_url='http://www.youwebsite.com/email_activation/'; ?> |
Index.php
File này thực hiện kiểm tra xác nhận các trường đã được điền đầy đủ thông tin chưa, kiểm tra định dạng email thông qua regex. Regex là gì thì các bạn có thể tìm hiểu bài viết sau tại đậy. Mã hóa password, mã hóa code kích hoạt…..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php include 'db.php'; $msg=''; if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) ) { // username and password sent from form $email=mysql_real_escape_string($_POST['email']); $password=mysql_real_escape_string($_POST['password']); // kiểm tra định dạng email nhập đúng chưa $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/'; if(preg_match($regex, $email)) { $password=md5($password); // mã hóa password $activation=md5($email.time()); // mã hóa email và thời gian $count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'"); // Kiểm tra email đã có trong cơ sở dữ liệu chưa if(mysqli_num_rows($count) < 1) { mysqli_query($connection,"INSERT INTO users(email,password,activation) VALUES('$email','$password','$activation')"); // sending email include 'smtp/Send_Mail.php'; $to=$email; $subject="Email verification"; $body='Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> <a href="'.$base_url.'activation/'.$activation.'">'.$base_url.'activation/'.$activation.'</a>'; //Gửi mail Send_Mail($to,$subject,$body); $msg= "Registration successful, please activate email."; } else { $msg= 'The email is already taken, please try new.'; } } else { $msg = 'The email you have entered is invalid, please try again.'; } } // HTML Part ?> |
Sendmail.php
File dùng để gửi mail. File này đã được các blogger trên internet chia sẻ rất nhiều, mình leek về đây cho tiện thôi. Hoặc các anh em có thể tham khảo bài gửi mail với app settings trong file web config.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php function Send_Mail($to,$subject,$body) { require 'class.phpmailer.php'; $from = "from@yourwebsite.com"; $mail = new PHPMailer(); $mail->IsSMTP(true); // use SMTP $mail->IsHTML(true); $mail->SMTPAuth = true; // enable SMTP authentication $mail->Host = "tls://smtp.yourwebsite.com"; // SMTP host $mail->Port = 465; // set the SMTP port $mail->Username = "SMTP_Username"; // SMTP username $mail->Password = "SMTP_Password"; // SMTP password $mail->SetFrom($from, 'From Name'); $mail->AddReplyTo($from,'From Name'); $mail->Subject = $subject; $mail->MsgHTML($body); $address = $to; $mail->AddAddress($address, $to); $mail->Send(); } ?> |
Activation.php
File quan trọng nhất, kiểm tra mã kích hoạt từ email của user. File này thực hiện truy vấn trong cơ sở dữ liệu cột activation của uid và trạng thái status xem kích hoạt chưa. Nếu chưa kích hoạt thì cập nhật lại trạng thái và đưa ra thông báo:
UPDATE users SET status='1' WHERE activation='$code'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php include 'db.php'; $msg=''; if(!empty($_GET['code']) && isset($_GET['code'])) { $code=mysql_real_escape_string($_GET['code']); $c=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code'"); if(mysqli_num_rows($c) > 0) { $count=mysqli_query($connection,"SELECT uid FROM users WHERE activation='$code' and status='0'"); if(mysqli_num_rows($count) == 1) { mysqli_query($connection,"UPDATE users SET status='1' WHERE activation='$code'"); $msg="Your account is activated"; } else { $msg ="Your account is already active, no need to activate again"; } } else { $msg ="Wrong activation code."; } } ?> //HTML Part <?php echo $msg; ?> |
Khi đó, user đăng ký sẽ nhận được email như sau
Để cho bảo mật hơn, chúng ta dùng .htaccess để rewrite lại url của link kích hoạt chút
từ
http://website.com/activation.php?code=ACTIVATION_CODE
thành
http://website.com/activation/ACTIVATION_CODE
bằng đoạn sau:
1 2 3 4 | RewriteEngine On RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1 RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1 |
CSS
Cuối cùng là file css để thiết kế cho form của chúng ta. Các bạn có thể tham khảo các bài viết khác về cách thiết kế một form đẹp mặt trên sharecodeweb nhé. Tại đây mình làm đơn giản như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | body { font-family: "Helvetica",Arial,sans-serif; font-weight: 500; color:#333; } label { width:100px; display:block; font-weight:bold; color:#666666; } #main { margin:0 auto; width:800px; } .input { padding:10px; font-size:14px; border:1px solid #999999; width:200px; margin-bottom:10px; } .button { padding:10px; background-color: #5fcf80 !important; border-color: #3ac162 !important; } .msg { font-size:11px; color:#666; padding:10px; } |
Download code xác thực email
Các bạn có thể download code dưới đây nhé. Chúc anh em thành công!
You must log in to post a comment.