php的登陆系统

参考文章http://blog.csdn.net/qazcxh/article/details/45726911

php的学习一直处于简单的一两个文件之内,今天看到了一个比较多文件的学习文章,于是就照着它动手敲了一遍了,感觉还可以。

第一部分当然就是登陆的界面了:


index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登陆注册修改密码系统主页</title>
    <style type="text/css">
        form{
            text-align:center;
        }
    </style>
</head>
<body>
<!-- 表单 -->
    <form action="enter.php" method="post" οnsubmit="return enter()">
        用户名<input type="text" name="username" id="username"><br/>密码<input type="password" name="password" id="password"><br/><input type="submit" value="登陆"><input type="button" value="注册" οnclick="register();">
    </form>
    <script type="text/javascript">
    //检测数据是否正确,从而判断是否提交表单数据
    function enter(){
        var username=document.getElementById("username").value;
        var password=document.getElementById("password").value;
        var regex=/^[/s]+$/;
        if(regex.test(username)||username.length==0){
            alert("用户名格式不对");
            return false;
        }
        if(regex.test(password)||password.length==0){
            alert("密码格式不对");
            return false;
        }
        return true;
    }
    //转到注册页面
    function register(){
        window.location.href="register.html";
    }
    </script>
</body>
</html>
enter.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登陆系统的后台执行过程</title>
</head>
<body>
    <?php
        session_start();//开启session
        //获取表单中input的name中的数据
        $username=$_REQUEST["username"];
        $password=$_REQUEST["password"];
        //连接服务器
        $con=mysql_connect("localhost","root","root");
        if(!$con){
            die('数据库连接失败'.mysql_error());
        }
        //选择数据库
        mysql_select_db("user_info",$con);
        $dbusername=null;
        $dbpassword=null;
        //查找当前输入的用户名
        $result=mysql_query("select * from user_info where username='{$username}' and isdelete=0;");
        while($row=mysql_fetch_array($result)){
            $dbusername=$row["username"];
            $dbpassword=$row["password"];
        }
        //如果不存在,返回index.html
        //密码不正确,也是返回index.html
        if(is_null($dbusername)){
            ?>
            <script type="text/javascript">
            alert("用户名不存在");
            window.location.href="index.html";
            </script>
    <?php
        }
        else {
            if($dbpassword!=$password){
     ?>
     <script type="text/javascript">
     alert("密码错误");
     window.location.href="index.html";
     </script>
     <?php
        }
        else{
            $_SESSION["username"]=$username;
            //给session一个随机值,防止直接在地址栏内输入wecome的地址
            $_SESSION["code"]=mt_rand(0,100000);
     ?>
     <script type="text/javascript">
     window.location.href="wecome.php";
     </script>
     <?php
        }
    }
    mysql_close($con);
    ?>
</body>
</html>

wecome.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>欢迎登陆界面</title>
</head>
<body>
    <?php
    session_start();//打开session
    //判断是否设置了session【“code”】
    if(isset($_SESSION["code"])){
    ?>
    欢迎登陆<?php
    echo "${_SESSION["username"]}";
    ?><br/>
    您的ip:<?php
    echo "{$_SERVER['REMOTE_ADDR']}";
    ?>
    <br/>
    您的语言:
    <?php
    echo "${_SERVER['HTTP_ACCEPT_LANGUAGE']}";
    ?>
    <br/>
    浏览器版本:
    <?php
    echo "${_SERVER['HTTP_USER_AGENT']}";
    ?>
    <a href="exit.php">退出登陆</a>
    <?php
        }else {
    ?>
    <script type="text/javascript">
    alert("退出登陆");
    window.location.href="exit.php";
    </script>
    <?php
        }
    ?>
    <br/>
    <a href="alter_password.html">修改密码</a>
</body>
</html>

alter_password.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <style type="text/css">
        form{
            text-align:center;
        }
    </style>
</head>
<body>
    <?php
        session_start();
    ?>
    <form action="alter_password.php" method="post" οnsubmit="return alter()">
        用户名<input type="text" name="username" id="username" /><br/>旧密码<input
        type="password" name="oldpassword" id="oldpassword" />新密码<input type="password"
        name="newpassword" id="newpassword" /><br/>确认新密码<input type="password" name="assertpassword" id="assertpassword"/><br/><input type="submit" value="修改密码" οnclick="return alter()">
    </form>
    <script type="text/javascript">
    document.getElementById("username").value="<?php echo "${_SESSION["username"]}";?>";
    </script>
    <script type="text/javascript">
    //判断是否传数据过form的action
    function alter(){
        var username=document.getElementById("username").value;
        var oldpassword=document.getElementById("oldpassword").value;
        var newpassword=document.getElementById("newpassword").value;
        var assertpassword=document.getElementById("assertpassword").value;
        var regex=/^[/s]+$/;
        if(regex.test(username)||username.length==0){
            alert("用户格式不对");
            return false;
        }
        if(regex.test(oldpassword)||oldpassword.length==0){
            alert("密码格式不对");
            return false;
        }
        if(regex.test(newpassword)||newpassword.length==0){
            alert("新密码格式不对");
            return false;
        }
        if(assertpassword!=newpassword||assertpassword==0){
            alert("两次密码输入不一致");
            return false;
        }
        return true;
    }
    </script>
</body>
</html>

alter_password.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>正在修改密码</title>
</head>
<body>
    <?php
    session_start();//打开session
    //提取input标签的一系列数据
    $username = $_REQUEST["username"];
    $oldpassword = $_REQUEST["oldpassword"];
    $newpassword = $_REQUEST["newpassword"];
    //连接服务器
    $con = mysql_connect("localhost","root","root");
    if(!$con){
        die('数据库连接失败'.mysql_error());
    }
    //选择数据库
    mysql_select_db("user_info",$con);
    $dbusername = null;
    $dbpassword = null;
    //查找当前名字的数据
    $result = mysql_query("select * from user_info where username='{$username}' and isdelete=0;");
    while($row=mysql_fetch_array($result)){
        $dbusername = $row["username"];
        $dbpassword = $row["password"];
    }
    //判断是否存在
    if(is_null($dbusername)){
        ?>
        <script type="text/javascript">
            alert("用户名不存在");
            window.location.href="alter_password.html";
        </script>
    <?php
    }
    //判断旧密码和当前所用密码是否相同
    if($oldpassword!=$dbpassword){
    ?>
    <script type="text/javascript">
    alert("密码错误");
    window.location.href="alter_password.html";
    </script>
    <?php
    }
    mysql_query("update user_info set password='{$newpassword}' where username='{$username}'")
    or die("存入数据库失败".mysql_error());
    mysql_close($con);
    ?>
    <script type="text/javascript">
    alert("密码修改成功");
    window.location.href="index.html";
    </script>
</body>
</html>

register.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册系统</title>
<style type="text/css">
    form{
        text-align:center;
    }
</style>
</head>
<body>
    <!-- 表单 -->
    <form action="register.php" method="post" name="form_register" οnsubmit="return check()">
        用户名<input type="text" name="username" id="username"><br/>
        密码<input type="password" name="password" id="password"><br/>
        确认密码<input type="password" name="assertpassword" id="assertpassword"><br/>
        <input type="submit" value="注册">
    </form>
    <script type="text/javascript">
    //检查输入的东西是否符合,不符合不传数据过register.php
    function check(){
        var username=document.getElementById("username").value;
        var password=document.getElementById("password").value;
        var assertpassword=document.getElementById("assertpassword").value;
        var regex=/^[/s]+$/;
        if(regex.test(username)||username.length==0){
            alert("用户名格式不对");
            return false;
        }
        if(regex.text(password)||password.length==0){
            alert("密码格式不对");
            return false;
        }
        if(password!=assertpassword){
            alert("两次密码输入不一致");
            return false;
        }
    }
    </script>
</body>
</html>

register.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>注册用户</title>
</head>
<body>
    <?php
        session_start();//打开session
        //获取传过来的数值
        $username=$_REQUEST["username"];
        $password=$_REQUEST["password"];
        //连接服务器
        $con=mysql_connect("localhost","root","root");
        if(!$con){
            die('数据库连接失败'.mysql_error());
        }
        //选择数据库
        mysql_select_db("user_info",$con);
        $dbusername=null;
        $dbpassword=null;
        //查询当前名字的数据是否存在
        $result=mysql_query("select * from user_info where username='{$username}'and isdelete=0");
        while($row=mysql_fetch_array($result)){
            $dbusername=$row["username"];
            $dbpassword=$row["password"];
        }
        //如果存在
        if(is_null($dbusername)){
        ?>
        <script type="text/javascript">
        alert("用户名已存在");
        window.location.href="register.html";
        </script>
        <?php
            }
            mysql_query("insert into user_info (username,password)values('{$username}','{$password}')")
            or die("存入数据库失败".mysql_error());
            mysql_close($con);
        ?>
        <script type="text/javascript">
        alert("注册成功");
        window.location.href="index.html";
        </script>
</body>
</html>


退出登陆时exit.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <?php
    session_start();//打开session
    session_destroy();//将之前的session数据全部删除掉
    ?>
    <script type="text/javascript">
    window.location.href="index.html";//跳转到index.html
    </script>
</body>
</html>

9.mysql数据库搭建部分



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值