PHP开发之登录后台功能思路

功能的实现需要的是PHP代码的串联,PHP代码写之前需要理清楚思路,否则代码将无从下手,下面我们就先讲解下思路的流程,如图:

1_(Y9AW($LQQE96F@JRTN1N.png

根据思路,我们再来看看代码:

<?php
session_start();
header("content-type:text/html;charset=utf-8");
//连接数据库
$link = mysqli_connect("localhost","root","root","regedit");
if (!$link) {
    die("连接失败: " . mysqli_connect_error());
}
if(isset($_POST)){
    //用户名不能为空
    if(!$_POST['username']){
        echo('用户名不能为空');
        return;
    }
    //密码不能为空
    if(!$_POST['password']){
        echo('密码不能为空');
        return;
    }
    //判断验证码是否填写并且是否正确
    if(!$_POST['code']){
        echo('验证码不能为空');
        return;
    }else if($_POST['code']!=$_SESSION['VCODE']){
        echo('验证码不正确');
        return;
    }  
    $sql="select username,password from form where username = '{$_POST['username']}' and password='{$_POST['password']}'";
    $rs=mysqli_query($link,$sql); //执行sql查询
    $row=mysqli_fetch_assoc($rs);
    if($row) { // 用户存在;
        if ($username == $row['username'] && $pwd == $row['password']) { //对密码进行判断。
            echo "登陆成功,正在为你跳转至后台页面";
            //header("location:index.php");
        }
    }else{
        echo "账号或密码错误" . "<br/>";
        echo "<a href='login.html'>返回登陆页面</a>";
    }
}

首先是要连接数据库,因为登录的账号密码都在数据库里,然后开始对账号密码验证码是否有输入进行判断。然后使用sql语句查询数据库,看看输入的值数据库中是否存在,

若存在且正确,那么可以成功登录,否则,输出“账号或密码错误”,重新登录。

继续学习
||
<?php session_start(); header("content-type:text/html;charset=utf-8"); //连接数据库 $link = mysqli_connect("localhost","root","root","regedit"); if (!$link) { die("连接失败: " . mysqli_connect_error()); } if(isset($_POST)){ //用户名不能为空 if(!$_POST['username']){ echo('用户名不能为空'); return; } //密码不能为空 if(!$_POST['password']){ echo('密码不能为空'); return; } //判断验证码是否填写并且是否正确 if(!$_POST['code']){ echo('验证码不能为空'); return; }else if($_POST['code']!=$_SESSION['VCODE']){ echo('验证码不正确'); return; } $sql="select username,password from form where username = '{$_POST['username']}' and password='{$_POST['password']}'"; $rs=mysqli_query($link,$sql); //执行sql查询 $row=mysqli_fetch_assoc($rs); if($row) { // 用户存在; if ($username == $row['username'] && $pwd == $row['password']) { //对密码进行判断。 echo "登陆成功,正在为你跳转至后台页面"; //header("location:index.php"); } }else{ echo "账号或密码错误" . "<br/>"; echo "<a href='login.html'>返回登陆页面</a>"; } }
提交重置代码