php - 如何在PHP中杀死页面

标签 php mysql oop session

我正在使用PHP开发自己的自定义CMS,我想知道如果用户已经访问过页面,该如何杀死它。

基本上,我有一个名为message.php的文件。当用户第一次登录其帐户时,将弹出欢迎消息。为此,我在数据库中添加了一个名为“ welcome_message”的字段并将其设置为0。因此,仅当“ welcome_message”等于0时,该消息才会出现。运行并将“ welcome_message”更新为1!因此,不再显示任何消息。

问题是,欢迎消息仍然存在。例如,用户可以手动转到该URL,然后再次看到该消息:

http://localhost/site_name/admin/message.php?msg=0


所以我想知道的是,如何禁用此功能?如何在看到此消息后添加查询以删除此消息,使其不再存在并且用户看不到?

注意:我知道可以通过在表中设置新字段并在其中添加消息来实现此功能。但这不是我喜欢使用的方式。如果您知道更好的解决方案,请告诉我,我真的很感激...

这是我的message.php的代码:

    if(isset($_GET['msg'])){
    $update = new Up();
    $update->Update($_SESSION["admin_username"]);
    echo "
    <!-- Content Wrapper. Contains page content -->
    <div class='content-wrapper'>
        <!-- Content Header (Page header) -->
        <section class='content-header'>
            <h1>
                Here you can see your new message
                <small><a href='http://www.zite.daygostar.ir/tutorials/messages.php' target='_blank'>tutorial</a></small>
            </h1>
            <ol class='breadcrumb'>
                <li class='active'>message.php</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class='content'>

            ";include_once 'php/includes/sections/welcome_message.php'; echo"

        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    ";
}


这是Up.class.php

<?php 
class Up
{
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function Update($name)
    {
        if(!empty($name))
        {
            $ins = $this->db->prepare("UPDATE admins SET welcome_message=1 WHERE username = ?");
            $ins->bindParam(1,$name);
            $ins->execute();
        }
    }
}
?>


我正在从此类中的db检索数据:

    <?php 
class Admin
{
    public $db,$username,$password,$id,$group,$Datetime,$msg,$firstname,$lastname,$login,$logout;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }
    public function getAdmin($name)
    {
        if(!empty($name))
        {
            $adm = $this->db->prepare("select * from admins where username=?");
            $adm->bindParam(1,$name);
            $adm->execute();
            while($row = $adm->fetch())
            {
                $this->id           = $row['id'];
                $this->username     = $row['username'];
                $this->password     = $row['password'];
                $this->group        = $row['group'];
                $this->Datetime     = $row['date_joined'];
                $this->msg          = $row['welcome_message'];
                $this->firstname    = $row['firstname'];
                $this->lastname     = $row['lastname'];
                $this->login        = $row['login'];
                $this->logout       = $row['logout'];
            }
        }
        else
        {
            header("Location: maint/php/includes/errors/004.php");
            exit();
        }
    }
    public function getID()
    {
        return $this->id;
    }
    public function getUsername()
    {
        return $this->username;
    }
    public function getPassword()
    {
        return $this->password;
    }
    public function getGroup()
    {
        return $this->group;
    }
    public function gtDate()
    {
        return $this->Datetime;
    }
    public function msg()
    {
        return $this->msg;
    }
    public function firstName()
    {
        return $this->firstname;
    }
    public function lastName()
    {
        return $this->lastname;
    }
    public function login()
    {
        return $this->login;
    }
    public function logout()
    {
        return $this->logout;
    }
}
?>

最佳答案

当用户登录时,只需获取该登录用户的welcome_message字段的值,然后对照该值进行检查即可。

您的message.php应该是这样的

$ welcome_message =获取登录用户在数据库中的存储值

if(!empty($welcome_message)){
    $update = new Up();
    $update->Update($_SESSION["admin_username"]);
    echo "
    <!-- Content Wrapper. Contains page content -->
    <div class='content-wrapper'>
        <!-- Content Header (Page header) -->
        <section class='content-header'>
            <h1>
                Here you can see your new message
                <small><a href='http://www.zite.daygostar.ir/tutorials/messages.php' target='_blank'>tutorial</a></small>
            </h1>
            <ol class='breadcrumb'>
                <li class='active'>message.php</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class='content'>

            ";include_once 'php/includes/sections/welcome_message.php'; echo"

        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    ";
}

关于php - 如何在PHP中杀死页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38302468/

相关文章:

php - jQuery 验证插件 : How to create your own messages

PHP/MySQL 条件插入不插入

php - 查询问题以获取每天多次重复的每周数据?

java - 如何在Java中搜索对象数组中的实例变量数组?

javascript - 从其他类访问静态方法是标准的吗?

php - Ajax 实时搜索框

php - 调用未定义的方法 DB::escape_string()

MySQL 在正则表达式中匹配空格

Java - Tree 是 Node 的一个实例吗?

javascript - 使用 vanilla javascript 将数据发布到 php 并将其取回