Register, activate mail practice

Source: Internet
Author: User

This article refer to the video of 18 elder brother

Database:

CREATE TABLE User (UID int primary key Auto_increment,uname char () NOT NULL default ' ', email char (+) NOT null default ' ', pass char (+) NOT null default ", status tinyint NOT NULL default 0) engine MyISAM charset UTF8;


CREATE TABLE Activecode (CID int primary key Auto_increment,uname char () NOT NULL default ' ', code char (+) NOT NULL Defau Lt ', expire int not NULL default 0) engine MyISAM charset UTF8;


Php

conn.php: Database connection

<?php


$conn =mysql_connect (' localhost ', ' root ', ' root ');


mysql_query (' Use Tempemail ', $conn);


mysql_query (' Set names UTF8 ', $conn);


?>



01.php: Impersonate a registered user, create an activation code, send an activation code

<?php


Require ('./conn.php ');

Require ('./phpmailer/class.phpmailer.php ');


$str = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456 ';


$uname =substr (Str_shuffle (substr ($str, 0,52)), 0,8);//Generate 8-bit random user names

$email = ' [email protected] ';


Demo Registration

$sql = "INSERT into user (Uname,email) VALUES (' $uname ', ' $email ')";

mysql_query ($sql, $conn);


Generate Activation Code

$code =substr (Str_shuffle (substr ($str, 0,52)), 0,8);

$expire =time () +5*24*3600;


$sql = "INSERT into Activecode (Uname,code,expire) VALUES (' $uname ', ' $code ', ' $expire ')";

mysql_query ($sql, $conn);


Send activation message


$phpmailer = new Phpmailer ();


Select SMTP

$phpmailer->issmtp ();

$phpmailer->host= ' smtp.163.com ';

$phpmailer->smtpauth=true;

$phpmailer->username= ' php0620 ';

$phpmailer->password= ' pwd ';


We can send a letter.

$phpmailer->from= ' [email protected] ';

$phpmailer->fromname= ' FromName ';

$phpmailer->subject= $uname. ' Welcome to register ';

$phpmailer->body= ' Please click: http://localhost/02.php?code= '. $code. ' To activate ';


Set recipient

$phpmailer->addaddress (' [email protected] ', ' first ');


$phpmailer->ADDCC (' [email protected] ', ' second ');


Letter

echo $phpmailer->send ()? ' Ok ': ' No ';



?>


02.php

<?php

Require ('./conn.php ');


$code =$_get[' code ';

if (strlen ($code)!=8) {

Exit (' Activation Code error ');

}



$sql = "SELECT * from Activecode where code= ' $code '";

$rs =mysql_query ($sql, $conn);


$row =mysql_fetch_assoc ($RS);


if (empty ($row)) {

Exit (' Activation Code error ');

}


if (Time () > $row [' expire ']) {

Exit (' Activation code expires ');

}


Activating user Update


$sql = "Update user set Status=1 where uname= ' $row [uname] '";

mysql_query ($sql, $conn);


void Activation Code

$sql = "Update activecode set expire=0 where code= ' $code '";

mysql_query ($sql, $conn);




?>


Register, activate mail practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.