php+mysql开发账号注册/登录/登陆后才可以查看/登陆后才可以下载功能

几乎大部分的网站都有注册账号,登录系统,这是非常需要的一个模块。
本人这次的案例就是做一个注册账号,登录账号,登陆后才可以查看某些资源的功能,其实就是简单的php操作数据库。

首先讲注册账号。
注册账号的逻辑是,先有个表单,输入账号和密码,然后通过post方式提交给php页面处理(验证表单,例如是否为空,数据库是否存在一样的数据,是否为中文、英文、数字等),若没有问题,就插入数据库,若有问题就重新返回填写,除了输入账号密码,我还特别加了一个邀请码,需要输入正确的邀请码才可以注册账号,如果不想要邀请码这个,可以自行去掉,只是我自己的项目需要用这个。
下面是注册表单:
register.php

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=56441686" charset="UTF-8"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <title>里客云 - 注册</title>
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">  
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="likeyun.css">
</head>
<body>

<form role="form" action="regcheck.php" method="post" name="form1">
    <div class="form-group">
    <div class="alert alert-danger">你需要注册后,才可以登录!</div>
        <input type="text" class="form-control" id="name" name="username" placeholder="请输入账号">
        <input type="password" class="form-control" id="name" name="password" placeholder="请输入密码">
        <input type="text" class="form-control" id="name" name="yaoqing" placeholder="请输入邀请码">
        <input type="Submit" name="Submit" class="btn btn-default" onclick="check()" value="注册"/>
        <p>没有邀请码?<a href="yaoqingma.html">立即获取</a></p>
    </div>
</form>
    

    <script>
function check(){
var username=document.form1.name;
var checkinput=document.form1.checknameinput;
for(i=0;i<document.getElementsByName("username")[0].value.length;i++){
var c = document.getElementsByName("username")[0].value.substr(i,1);
var ts = escape(c);
if(ts.substring(0,2) == "%u"){
alert("不支持中文账号");
document.getElementsByName("username")[0].value = "";
document.getElementsByName("username")[0].focus();
}
}
}
</script>
</body>
</html>

clipboard.png

上面表单可以看出来表单提交后直接给regcheck.php进行处理,验证。

regcheck.php

<?php  
header("Content-type:text/html;charset=utf-8");
if(isset($_POST["Submit"]) && $_POST["Submit"] == "注册")  
{  
$user = $_POST["username"];  
$psw = $_POST["password"];  
$yqm = $_POST["yaoqing"];  
if($user == "" || $psw == "" || $yqm == "")  
{  
echo "<script>alert('请不要留空!'); history.go(-1);</script>";  
}  
else  
{  
if($yqm == "LiKeYuNbA2017") //邀请码
{  
mysql_connect("数据库地址","数据库账号","数据库密码");   //连接数据库  
mysql_select_db("数据库名");  //选择数据库  
mysql_query("SET NAMES 'utf8'");//设定字符集  
$sql = "select username from 表名 where username = '$_POST[username]'"; //SQL语句  
$result = mysql_query($sql);    //执行SQL语句  
$num = mysql_num_rows($result); //统计执行结果影响的行数  
if($num)    //如果已经存在该用户  
{  
echo "<script>alert('用户名已存在'); history.go(-1);</script>";  
}  
else //不存在当前注册用户名称  
{ 
$sql_insert = "insert into user (username,password,phone,address) values('$_POST[username]','$_POST[password]','','')";  
$res_insert = mysql_query($sql_insert);  
//$num_insert = mysql_num_rows($res_insert);  
if($res_insert)  
{  
echo "<script>alert('注册成功'); history.go(-1);</script>";  
}  
else  
{  
echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>";  
}  
}  
}  
else  
{  
echo "<script>alert('邀请码不正确'); history.go(-1);</script>";  
}  
}  
}  
else  
{  
echo "<script>alert('注册失败!'); history.go(-1);</script>";  
}  
?>  

上面就是整个注册的过程了。

接着就是登录了。
登录就是通过php进行数据库字段对比验证,若存在该字段则登录成功,不存在则无法登陆。

登录表单
login.php

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
              <script type="text/javascript" src="http://tajs.qq.com/stats?sId=56441686" charset="UTF-8"></script>
    <meta name="format-detection" content="telephone=no">
    <title>里客云 - 登录</title>
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">  
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="likeyun.css">
</head>
<body>

<form role="form" action="logincheck.php" method="post">
    <div class="form-group">
    <div class="alert alert-danger">你需要登陆后,才可以获取本站资源!</div>
        <input type="text" class="form-control" id="name" name="username" placeholder="请输入账号">
        <input type="password" class="form-control" id="name" name="password" placeholder="请输入密码">
        <input type="submit" name="submit" class="btn btn-default" value="登陆" onclick="fn()"/>
        <p>没有账号?<a href="register.php">立即注册</a></p><br/>
    </div>
</form>
</body>
</html>

clipboard.png

登录处理页面。在登录过程,会通过session_start()在浏览器储存登录行为。
logincheck.php

<?php
//页面编码
header("Content-type:text/html;charset=utf-8");
//隐藏报错信息
error_reporting(E_ALL^E_NOTICE^E_WARNING);
//储存登录行为
session_start();
//直接把用户名赋给echo
$_SESSION['echo']="$_POST[username]";

if(isset($_POST["submit"]) && $_POST["submit"] == "登陆")  
    {  
        $user = $_POST["username"];  
        $psw = $_POST["password"];  
        if($user == "" || $psw == "")  
        {  
            echo "<script>alert('用户名或密码不能为空'); history.go(-1);</script>";  
        }  
        else  
        {  
            mysql_connect("数据库地址","账号","密码");   //连接数据库  
            mysql_select_db("数据库名");  //选择数据库  
            mysql_query("SET NAMES 'utf8'");//设定字符集  
            $sql = "select username,password from 表名 where username = '$_POST[username]' and password = '$_POST[password]'";  
            $result = mysql_query($sql);  
            $num = mysql_num_rows($result);  
            if($num)  
            {  
                $row = mysql_fetch_array($result);  
                //验证通过后跳转 
                echo "<script>window.location.href='index.php';</script>";
            }  
            else  
            {  
                echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>";  
            }  
        }  
    }  
    else  
    {  
        echo "<script>alert('登录失败'); history.go(-1);</script>";  
    }
?>

这样就完成了登录。

下面就看看如何实现登录后才可以查看某些内容,很多网站都这样做,要登录后才可以查看或者下载一些资源的。

例如index.php是该网站首页,首页有一些内容,未登录之前是显示的,有一些内容需要登录后才可以显示。

<?php
header("Content-type:text/html;charset=utf-8");
session_start();
 if(isset($_SESSION['echo'])){
//如果已经登陆了,那么就输出
echo "你已经登录,该网站的网址是:https://segmentfault.com/";

}else{
//如果还没登录,那么就输出
echo "你还没登录,不可以查看网址。";

//强制中断程序的执行
exit();
}
?>

就这么简单实现了。

既然有登录,那就要有退出登录。
exitlogin.php

<?php
header("Content-type:text/html;charset=utf-8");
session_start();
unset($_SESSION['echo']);//直接unset,或者用session里面的函数,我没试过
echo "<script>window.location.href='index.php';</script>";
?>

评论

暂无评论