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>"; } }
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!