Home > Article > Backend Development > PHP user registration email verification and activation account example_PHP tutorial
Nowadays, most websites require users to register by email, and then send an account activation email to the user's registered email. The user can activate the account by clicking on the link. Let me introduce the specific method below.
Functional requirements
PHP program development. Users register on the website and need to activate their account through an email link. After the user registers (user information is written to the database), if the user does not log in to the email to activate the account, it is stipulated that user information without activated account will be automatically deleted after 24 hours. , after the activation link expires, users can also use this information to register on the website
Prepare data sheet
The field Email in the user information table is very important. It can be used to verify users, retrieve passwords, and even for website parties, it can be used to collect user information for email marketing. The following is the table structure of the user information table t_user:
The code is as follows | Copy code | ||||
`id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL COMMENT 'username', `password` varchar(32) NOT NULL COMMENT 'password', `email` varchar(30) NOT NULL COMMENT 'email', `token` varchar(50) NOT NULL COMMENT 'Account activation code', `token_exptime` int(10) NOT NULL COMMENT 'Activation code validity period', `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Status, 0-not activated, 1-activated', `regtime` int(10) NOT NULL COMMENT 'Registration time', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
Place a registration form on the page, and users can enter registration information, including username, password and email.
代码如下 | 复制代码 |
The code is as follows | Copy code |
Necessary front-end verification is required for user input. Regarding the form verification function, it is recommended that you refer to the article on this site: An example explains the application of the form verification plug-in Validation. This article skips the front-end verification code. In addition, there should be There is an input box that requires the user to re-enter the password, so I just skipped it because I was lazy.
register.php
The user submits the registration information to register.php for processing. register.php needs to complete two major functions: writing data and sending emails.
First of all, it contains the two necessary files, connect.php and smtp.class.php. These two files are included in the download package provided outside. You are welcome to download.
The code is as follows | Copy code | ||||||||
include_once("connect.php");//Connect to the database include_once("smtp.class.php");//Email sending class
|
The code is as follows | Copy code |
$password = md5(trim($_POST['password'])); //Encrypt password $email = trim($_POST['email']); //Email $regtime = time(); $token = md5($username.$password.$regtime); //Create an activation code $token_exptime = time()+60*60*24;//The expiration time is 24 hours later $sql = "insert into `t_user` (`username`,`password`,`email`,`token`,`token_exptime`,`regtime`) values ('$username','$password','$email','$token','$token_exptime','$regtime')"; mysql_query($sql); |
In the above code, $token is the constructed activation identification code, which is composed of user name, password and current time and is encrypted by md5. $token_exptime is used to set the expiration time of the activation link URL. The user can activate the account within this time period. In this example, the activation is valid within 24 hours. Finally, these fields are inserted into the data table t_user.
When the data is inserted successfully, call the email sending class to send the activation information to the user's registered mailbox. Pay attention to forming the constructed activation identification code into a complete URL as the activation link when the user clicks. The following is the detailed code:
There is also a very easy-to-use and powerful email sending class to share with everyone: use PHPMailer to send emails with attachments and support HTML content. You can use it directly.
active.php
If nothing goes wrong, the email you filled in when registering your account will receive an email from helloweba. At this time, you can directly click the activation link and let active.php handle it.
active.php receives the submitted link information and obtains the value of the parameter verify, which is the activation identification code. Query and compare it with the user information in the data table. If there is a corresponding data set, determine whether it has expired. If it is within the validity period, set the status field in the corresponding user table to 1, which means it has been activated. This completes the activation function. .
The code is as follows | Copy code | ||||
|
After successful activation, you find that the token field is no longer useful and you can clear it. Next, we will explain the function for users to retrieve passwords, and also use email verification, so stay tuned.
Finally attach the email smtp.class.php to send the class
The code is as follows | Copy code |
class Smtp{ /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass; /* Private Variables */ /* Contractor */ function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass) { $this->smtp_port = $smtp_port; $this->relay_host = $relay_host; $this->time_out = 30; //is used in fsockopen() $this->auth = $auth; //auth $this->user = $user; $this->pass = $pass; $this->host_name = "localhost"; //is used in HELO command $this->sock = false; /* Main Function */ function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { $body = ereg_replace("(^|(rn))(.)", "1.3", $body); $header .= "MIME-Version:1.0rn"; if ($mailtype == "HTML") { $header .= "To: " . $to . "rn"; if ($cc != "") { $header .= "From: $from<" . $from . ">rn"; $header .= "Subject: " . $subject . "rn"; $header .= $additional_headers; $header .= "Date: " . date("r") . "rn"; $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")rn"; list ($msec, $sec) = explode(" ", microtime()); $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">rn"; $TO = explode(",", $this->strip_comment($to)); if ($cc != "") { if ($bcc != "") { $sent = true; foreach ($TO as $rcpt_to) { if (!$this->smtp_sockopen($rcpt_to)) { $sent = false; continue; if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { $sent = false; fclose($this->sock); $this->log_write("Disconnected from remote hostn"); return $sent; /* Private Functions */ function smtp_send($helo, $from, $to, $header, $body = "") { if (!$this->smtp_putcmd("", base64_encode($this->pass))) { if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) { if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) { if (!$this->smtp_putcmd("DATA")) { if (!$this->smtp_message($header, $body)) { if (!$this->smtp_eom()) { if (!$this->smtp_putcmd("QUIT")) { return true; function smtp_sockopen($address) { function smtp_sockopen_relay() { $this->sock = @ fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); return false; $this->log_write("Connected to relay host " . $this->relay_host . "n"); return true; function smtp_sockopen_mx($address) { if (!@ getmxrr($domain, $MXHOSTS)) { return false; foreach ($MXHOSTS as $host) { $this->sock = @ fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); continue; $this->log_write("Connected to mx host " . $host . "n"); return true; $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")n"); return false; function smtp_message($header, $body) { $this->smtp_debug("> " . str_replace("rn", "n" . "> ", $header . "n> " . $body . "n> ")); return true; function smtp_eom() { $this->smtp_debug(". [EOM]n"); return $this->smtp_ok(); function smtp_ok() { $this->smtp_debug($response . "n"); if (!ereg("^[23]", $response)) { fgets($this->sock, 512); $this->log_write("Error: Remote host returned "" . $response . ""n"); return false; return true; function smtp_putcmd($cmd, $arg = "") { else fputs($this->sock, $cmd . "rn"); $this->smtp_debug("> " . $cmd . "n"); return $this->smtp_ok(); function smtp_error($string) { return false; function log_write($message) { if ($this->log_file == "") { $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message; if (!@ file_exists($this->log_file) || !($fp = @ fopen($this->log_file, "a"))) { return false; flock($fp, LOCK_EX); fputs($fp, $message); fclose($fp); return true; function strip_comment($address) { while (ereg($comment, $address)) { return $address; function get_address($address) { $address = ereg_replace("^.*<(.+)>.*$", "1", $address); return $address; function smtp_debug($message) { |
connect数据库连接类
代码如下
|
复制代码 | ||||