如果数据库中有特定值,则重定向用户

时间:2017-10-25 02:27:37

标签: php html sql

您好,在我的网站上我有这种维护模式,我可以通过将SQL中的值从0更改为1来激活,它会将所有用户重定向到特定页面。我想对特定用户做同样的事情。所以我在我的SQL中添加了一个名为Banned的列,默认值为0.我试图复制代码进行维护但没有成功。

维护代码:

 <?php
include 'config.php';

if (isset($_COOKIE['hash'])) {
    $sql = $db->query("SELECT * FROM `users` WHERE `hash` = " . $db->quote(filter_var($_COOKIE['hash'], FILTER_SANITIZE_STRING)));
    if ($sql->rowCount() != 0) {
        $row = $sql->fetch();
        $user = $row;
    }
}


$sql22 = $db->query('SELECT websiteOnline FROM website WHERE websiteOnline = 0');
if($sql22->rowCount() == 1 && $user['rank'] != 69)
{
    header('Location: maintenance.php'); 
}

SQL Banned Column

我的特定用户代码:

     <?php
    include 'config.php';

    if (isset($_COOKIE['hash'])) {
        $sql = $db->query("SELECT * FROM `users` WHERE `hash` = " . $db->quote(filter_var($_COOKIE['hash'], FILTER_SANITIZE_STRING)));
        if ($sql->rowCount() != 0) {
            $row = $sql->fetch();
            $user = $row;
        }
    }


    $sql22 = $db->query('SELECT websiteOnline FROM website WHERE websiteOnline = 0');
    if($sql22->rowCount() == 1 && $user['rank'] != 69)
    {
        header('Location: maintenance.php'); 
    }

$sql22 = $db->query('SELECT Banned FROM users WHERE Banned = 0');
    if($sql22->rowCount() == 1 && $user['rank'] != 69)
    {
        header('Location: banned.php'); 
    }

2 个答案:

答案 0 :(得分:0)

我假设您正在使用哈希来确定用户是否已登录,0表示禁用,并且您遇到的问题是登录并被禁止的用户不在被重定向到被禁止的页面。

我不确定您使用哪种方法连接到数据库,因此我将在评论中留下一些您必须自己填写的方法。

您以前的代码只是在数据库中检查banned = 0,如果禁止的记录数是1,那么它只会将所有用户转发到被禁止的页面。

您需要检查该特定用户是否被禁止,如果是,请将该用户转发到被禁止的页面。

希望我能正确理解这个问题并帮助你解答:)     

if (isset($_COOKIE['hash'])) {
$sql = $db->query("SELECT * FROM `users` WHERE `hash` = " . $db->quote(filter_var($_COOKIE['hash'], FILTER_SANITIZE_STRING)));
if ($sql->rowCount() != 0) {
    $row = $sql->fetch();
    $user = $row;
    }
}


$sql22 = $db->query('SELECT websiteOnline FROM website WHERE websiteOnline = 0');
if($sql22->rowCount() == 1 && $user['rank'] != 69)
{
    header('Location: maintenance.php'); 
}
$sql22 = $db->query('SELECT Banned FROM users WHERE Banned = 0 AND hash=:hash');

//bind the value of cookie to :hash here,
//execute the query

if($sql22->rowCount() == 1 && $user['rank'] != 69)
{
    header('Location: banned.php'); 
}

答案 1 :(得分:0)

我还认为您使用cookie哈希来确定用户登录的内容,我想尝试不同的方法。您已经在$ user变量上获得了有关用户的所有信息,因此,不需要第三个查询。

//here you already have everything about the user, so no need for a third query
    if (isset($_COOKIE['hash'])) {
    $sql = $db->query("SELECT * FROM `users` WHERE `hash` = " . $db->quote(filter_var($_COOKIE['hash'], FILTER_SANITIZE_STRING)));
    if ($sql->rowCount() != 0) {
        $row = $sql->fetch();
        $user = $row;
        }
    }

    //This seems to work, so ok, leave it.
    $sql22 = $db->query('SELECT websiteOnline FROM website WHERE websiteOnline = 0');
    if($sql22->rowCount() == 1 && $user['rank'] != 69)
    {
        header('Location: maintenance.php');
        exit(); //just to make sure anything else wont run if the user just cancels the redirect
    }

    //Now we check if the user is banned. removed the type comparison because if $user['banned'] was a string, things would not work
    if($user['banned'] != 0 && $user['rank'] != 69)
    {
      header('Location: banned.php');
      exit(); //just to make sure anything else wont run if the user just cancels the redirect
    }