如何在PHP中杀死页面

时间:2016-07-11 08:34:56

标签: 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;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

当用户登录时,只为该登录用户获取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 -->
    ";
}