class.phpmailer.php
class.smtp.php
PHPMailerAutoload.php
<?php
function error_login($log){
echo 'Log:'.$log.'<br />';
}
try {
$config_set['db_connection']['dsn'] = 'mysql:dbname=school;host=127.0.0.1;charset=utf8';
$config_set['db_connection']['user_name'] = 'root';
$config_set['db_connection']['password'] = '';
$dbh = new PDO (
$config_set['db_connection']['dsn'],
$config_set['db_connection']['user_name'],
$config_set['db_connection']['password'],
array (
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
}
catch(PDOException $error) {
echo "Something Error!!<br />";
error_login($error->getMessage());
}
date_default_timezone_set("Asia/Taipei");
session_start();
?>
var pass = document.getElementById("pass");
if(pass.value==""){
alert("不能為空!");
return false;
}
var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Email
if(!preg.test(email.value)){
alert("Email格式错误!");
return false;
}
<form action="register.php" method="post" onsubmit="return chk_form();">
<p>E-mail:<input type="text" class="input" name="email" id="email"></p>
<p>學 號:<input type="text" class="input" name="sid" id="sid"></p>
<p>密 碼:<input type="password" class="input" name="password" id="pass"></p>
<p><button class="btn">註冊</button></p>
</form>
index.html or index.php
require("db_connection.php");
$email = stripslashes(trim($_POST['email'])); //trim() 去除前後空白
$sid = stripslashes(trim($_POST['sid'])); //stipslashes() 去除\ 等攻擊性字元符號
//檢測帳號是否存在
$sql = $dbh->prepare("select email, sid from member where email = :email or sid = :sid ");
$sql->execute(array('$email' => $email, ':sid' => $sid));
if($sql->rowCount() == 1) {
echo "<script>alert("該信箱或該學號已註冊");window.history.go(-1);</script>";
exit;
}
register.php
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$token = md5($email.$password.$regtime); //激活碼
$token_exptime = time()+60*60*24;//24小時後過期
$regtime = time(); //現在時間
$insert = $dbh->prepare("INSERT INTO member(sid, psw, iden, email, token, token_exptime, status, regtime)
VALUES (:sid, :password', 1, :email, :token, :token_exptime, 0, :regtime)");
$insert->execute(
array(
':sid' => $sid,
':password' => $password,
':email' => $email,
':token' => $token,
':token_exptime' => $token_exption,
':regtime' => $regtime
)
);
register.php
if($insert->rowCount() == 1){
require('PHPMailerAutoload.php');
mb_internal_encoding('utf-8');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com:465";
$mail->Port = 465;
$mail->Username = "nicky.smtp.nutc@gmail.com";
$mail->Password = 'a1111111111';
$mail->SMTPSecure = 'ssl';
$mail->FromName = '系統註冊信件';
$mail->addAddress($email, 'Web User');
$mail->isHTML(true);
$mail->Subject = '標題內容';
$mail->Body = "親愛的用戶:<br/>感谢您註冊了新帳號。<br/>請點擊連結來啟用您的帳號。<br/><a href='http://127.0.0.1/register/active.php?verify=".$token."' target='_blank'>http://127.0.0.1/register/active.php?verify=".$token."</a><br/>如果以上連結無法點擊,請將它複製到您的網址列,該連結24小時內有效。<br/><p style='text-align:right'>--------XXXX System敬上</p>";
...
register.php
...
$mail->Subject = '標題內容';
$mail->Body = "親愛的用戶:<br/>感谢您註冊了新帳號。<br/>請點擊連結來啟用您的帳號。<br/><a href='http://127.0.0.1/register/active.php?verify=".$token."' target='_blank'>http://127.0.0.1/register/active.php?verify=".$token."</a><br/>如果以上連結無法點擊,請將它複製到您的網址列,該連結24小時內有效。<br/><p style='text-align:right'>--------XXXX System敬上</p>";
if(!$mail->send()) {
echo '<script>alert("註冊失敗!")</script>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else {
echo '<script>alert("註冊成功,驗證碼已傳至您的信箱!")</script>';
echo '<meta http-equiv="refresh" content="0;url=index.html" >';
}
}
register.php
require("db_connection.php");
$verify = stripslashes(trim($_GET['verify']));
$nowtime = time();
$sql = $dbh->prepare("SELECT no, token_exptime FROM member WHERE status = 0 AND token = :verify");
$sql->execute(array(':verify' => $verify));
if($sql->rowCount()==1){
$rows=$sql->fetch();
if($nowtime>$rows['token_exptime']){
$msg = '您的驗證碼已過期,請登入您的帳號重新發送驗證碼。';
}
else{
$update = $dbh->prepare("update member set status=1 where no='".$rows['no']."' ");
$update->execute();
$msg = '驗證成功!';
}
}else{
$msg = 'Error.';
}
?>
<script>alert('<?php echo $msg;?>')</script>
<meta http-equiv="refresh" content="0;url=index.html" >
active.php