通过邮箱激活账号、找回密码

首先要引入自己的:PHPMailer/Email.class.php文件

注册HTML页面:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>注册页面</title>
</head>
<body>
	<center>
		<form action="index.php?act=register" method="post">
			<table border="1">
				<tr>
					<td>用户名</td>
					<td><input type="text" name="username"></td>
				</tr>
				<tr>
					<td>密码</td>
					<td><input type="password" name="password"></td>
				</tr>
				<tr>
					<td>邮箱</td>
					<td>
						<input type="text" name="email">
					</td>
				</tr>
				<tr>
					<td>问题</td>
					<td>
						<input type="text" name="question">
					</td>
				</tr>
				<tr>
					<td>答案</td>
					<td>
						<input type="text" name="answer">
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="注册"></td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>


登录HTML页面:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>登录页面</title>
</head>
<body>
	<center>
		<form action="index.php?act=dologin" method="post">
			<table border="1">
				<tr>
					<td>用户名:</td>
					<td><input type="text" name="username"></td>
				</tr>
				<tr>
					<td>密码:</td>
					<td><input type="password" name="password"></td>
				</tr>
				<tr>
					<td></td>
					<td>
						<button>登录</button>
						  |  
						<a href="index.php?act=forget">忘记密码</a>
					</td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

找回密码HTML页面:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>找回密码页面</title>
</head>
<body>
	<cneter>
		<table border="1">
			<form action="index.php?act=dofoget" method="post">
				<tr>
					<td>用户名</td>
					<td><input type="text" name="username"></td>
				</tr>
				<tr>
					<td>邮箱</td>
					<td><input type="text" name="email"></td>
				</tr>
				<tr>
					<td>验证码</td>
					<td>
						<input type="text" name="code">
						<input type="button" id="btn" value="免费获取验证码" οnclick="settime(this)" /> 
					</td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="找回密码"></td>	
				</tr>	
			</form>
		</table>
	</cneter>
</body>
</html>
<script type="text/javascript" src="../public/jquery.js"></script>
<script type="text/javascript">
	$(function(){
		$("#btn").click(function(){
			var email = $("input[name='email']").val();
			var url = "index.php?act=code";
			$.get(url,{'email':email},function(msg){
			});
		});
	});

	//验证码获取倒计时
	var countdown=60;
	function settime(obj) 
	{ 
        if (countdown == 0) 
        { 
            obj.removeAttribute("disabled");    
            obj.value="免费获取验证码"; 
            countdown = 60; 
            return;
        } 
        else 
        { 
            obj.setAttribute("disabled", true); 
            obj.value="重新发送(" + countdown + ")"; 
            countdown--; 
        } 
    	setTimeout(function() 
    	{ 
        	settime(obj) 
        }
        ,1000) 
	}

</script>

显示密码HTML页面:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>密码页面</title>
</head>
<body>
	您的密码是:<?php echo base64_decode($row['password'])?>
	<a href="index.php?act=login">去登录</a>
</body>
</html>

PHP页面:

<?php
	header("content-type:text/html;charset=utf-8");
	session_start();
	$pdo = new PDO('mysql:host=www.liuyang.com;dbname=think','root','root');
	$pdo->exec("set names utf-8");
	$act = isset($_GET['act']) ? $_GET['act'] : '';
	if($act == 'register')
	{
		//注册页面
		$username = $_POST['username'];
		$password = base64_encode($_POST['password']);
		$email = $_POST['email'];
		$question = $_POST['question'];
		$answer = $_POST['answer'];
		//激活状态
		$active = 0;
		//校正码
		$validate = md5(md5($username));
		//注册sql
		$sql = "INSERT INTO user VALUES(null,'$username','$password',$active,'$validate','$question','$answer','$email')";
		if($pdo->exec($sql))
		{
			//注册成功,但是没有激活
			//发送邮件到用户的邮箱中进行激活
			require 'PHPMailer/Email.class.php';
			$user = "用户注册中心";
			$title = "用户注册激活";
			$url1 = "http://www.liuyang.com/php7/emali1/index.php?act=jihuo";
			$str = "&username=".urlencode(base64_encode($username))."&validate=".$validate."&time=".urlencode(base64_encode(time()));
			$url = $url1.$str;
			$content = "请点击以下链接进行完成用户注册激活:"."<a href='{$url}'>".$url."</a>";
			$address = $email;
			if(Mail::send($user,$title,$content,$address))
			{
				header("refresh:3;url=http://mail.163.com/");
				echo '您以成功注册,请去邮箱激活!';
			}
			else
			{
				echo $mail->ErrorInfo;
			}
		}
	}
	elseif($act == 'dologin')
	{
		//用户登录
		$username = $_POST['username'];
		$password = base64_encode($_POST['password']);
		//以接收的用户名查询用户的信息
		$userInfo = $pdo->query("SELECT * FROM user WHERE username='$username'")->fetch(PDO::FETCH_ASSOC);
		//判断用户是否存在
		if($userInfo['username'] != $username)
		{
			header("refresh:3;index.php?act=login");
			die('用户名不存在!');
		}
		//判断密码是否正确
		if($userInfo['password'] != $password)
		{
			header("refresh:3;index.php?act=login");
			die('密码错误!');
		}
		//判断用户是否激活
		if($userInfo['active'] != 1)
		{
			header("refresh:3;url=http://mail.163.com/");
			die('您的账号尚未激活,请去邮箱进行激活!');
		}
		include 'a.html';

	}
	elseif($act == 'login')
	{
		//提供登录页面
		include 'login.html';
	}
	elseif($act == 'jihuo')
	{
		//进行邮箱激活
		$username = urldecode(base64_decode($_GET['username']));
		$validate = $_GET['validate'];

		$time = urldecode(base64_decode($_GET['time']));
		//以接收的用户名查询用户的信息
		$row = $pdo->query("SELECT * FROM user WHERE username='$username'")->fetch(PDO::FETCH_ASSOC);

		//判断用户是否通过邮箱激活
		if($validate != $row['validate'])
		{
			die('恶意注册');
		}
		//判断时间超时
		if(time()-$time > 120)
		{
			header("refresh:3;index.php");
			die('激活失败,请重新注册!');
		}
		//判断用户是否已经激活
		if($row['active'] == 1)
		{
			header("refresh:3;index.php?act=login");
			die('您已经激活,请直接登录!');
		}
		//修改状态
		$sql = "UPDATE user SET active=1 WHERE username='$username'"; 
		if($pdo->exec($sql))
		{
			header("refresh:3;index.php?act=login");
			echo "激活成功,请登录!";
		}
		else
		{
			header("refresh:3;index.php");
			echo "激活失败,请重新注册!";
		}
	}
	elseif($act == "code")
	{ 	
		session_start();
		$username = $_GET['username'];
		$email = $_GET['email'];
		$mcode = rand(1000,9999);
		$_SESSION['mcode'] = $mcode;
		require 'PHPMailer/Email.class.php';
		$user = "用户注册中心";
		$title = "用户找回密码";	
		$content = "您的验证码是:".$mcode;
		$address = $email;
		Mail::send($user,$title,$content,$address);
	}
	elseif($act == "dofoget")
	{
		//密码验证
		$username = $_POST['username'];
		$email = $_POST['email'];
		$code = $_POST['code'];
		$mcode = $_SESSION['mcode'];

		$row = $pdo->query("SELECT * FROM user WHERE username='$username'")->fetch(PDO::FETCH_ASSOC);
		if($row['username'] != $username)
		{
			header("refresh:3;index.php?act=forget");
			die("您输入的用户名不存在,请重新输入!");
		}
		if($mcode != $code)
		{
			header("refresh:3;index.php?act=login");
			die("您的验证码输入有误,请重新输入!");
		}
		else
		{	
			include 'mi.html';
		}
	}
	elseif($act == "forget")
	{
		//找回密码页面
		include "forget.html";
	}
	else
	{
		include 'form.html';
	}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值