PHP激活用户注册验证邮箱
XML/HTML Code
- <div class="demo">
- <form id="reg" action="register.php" method="post" onsubmit="return chk_form();">
- <p>用户名:<input type="text" class="input" name="username" id="user"></p>
- <p>密 码:<input type="password" class="input" name="password" id="pass"></p>
- <p>E-mail:<input type="text" class="input" name="email" id="email"></p>
- <p><input type="submit" class="btn" value="提交注册"></p>
- </form>
- </div>
JavaScript Code
- <script type="text/javascript">
- function chk_form() {
- var user = document.getElementById("user");
- if (user.value == "") {
- alert("用户名不能为空!");
- return false;
- }
- var pass = document.getElementById("pass");
- if (pass.value == "") {
- alert("密码不能为空!");
- return false;
- }
- var email = document.getElementById("email");
- if (email.value == "") {
- alert("Email不能为空!");
- return false;
- }
- var preg = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/; //匹配Email
- if (!preg.test(email.value)) {
- alert("Email格式错误!");
- return false;
- }
- }
- </script>
register.php
PHP Code
- header("Content-type:text/html;charset=utf-8");
- require('../../conn.php');
- $username = stripslashes(trim($_POST['username']));
- //检测用户名是否存在
- $query = mysql_query("select id from t_user where username='$username'");
- $num = mysql_num_rows($query);
- if ($num == 1) {
- echo '<script>alert("用户名已存在,请换个其他的用户名");window.history.go(-1);</script>';
- exit;
- }
- $password = md5(trim($_POST['password']));
- $email = trim($_POST['email']);
- $regtime = time();
- $token = md5($username . $password . $regtime); //创建用于激活识别码
- $token_exptime = time() + 60 * 60 * 24; //过期时间为24小时后
- $sql = "insert into `t_user` (`username`,`password`,`email`,`token`,`token_exptime`,`regtime`) values ('$username','$password','$email','$token','$token_exptime','$regtime')";
- mysql_query($sql);
- if (mysql_insert_id()) {//写入成功,发邮件
- require 'class.phpmailer.php';
- try {
- $smtpserver ="smtp.163.com";
- $emailaddress= "";
- $emailpassword= "";
- $email_address= $email;
- $mail = new PHPMailer(true); //New instance, with exceptions enabled
- $body="亲爱的" . $username . ":<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/><a href='http://www.freejs.net/demo/557/active.php?verify=" . $token . "' target='_blank'>http://www.freejs.net/demo/557/active.php?verify=" . $token . "</a><br/>如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。<br/>如果此次激活请求非你本人所发,请忽略本邮件。";
- $body = preg_replace('/\\/','', $body); //Strip backslashes
- $mail->IsSMTP(); // tell the class to use SMTP
- $mail->SMTPAuth = true; // enable SMTP authentication
- $mail->Port = 25; // set the SMTP server port
- $mail->Host = $smtpserver; // SMTP server
- $mail->Username = $emailaddress; // SMTP server username
- $mail->Password = $emailpassword; // SMTP server password
- $mail->CharSet = "utf-8"; //设置字符集编码
- $mail->IsSendmail(); // tell the class to use Sendmail
- //如果配置了sendmail这里就用sendmail,本地测试把这个注释掉就可以了
- $mail->AddReplyTo($emailaddress,"freejs.net");
- $mail->From = $emailaddress;
- $mail->FromName = "freejs.net";
- $to = $email_address;
- $mail->AddAddress($to);
- $mail->Subject = "用户帐号激活";
- $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
- $mail->WordWrap = 80; // set word wrap
- $mail->MsgHTML($body);
- $mail->IsHTML(true); // send as HTML
- $mail->Send();
- echo "<div class='sendmail_ok'>恭喜您,注册成功!<br/>请登录到您的邮箱及时激活您的帐号</div>";
- } catch (phpmailerException $e) {
- echo $e->errorMessage();
- }
- //发送邮件结束
- }
原文地址:http://www.freejs.net/article_biaodan_557.html