如果用户是admin,则禁用维护,如果用户正常则启用维护

时间:2016-03-01 21:14:42

标签: php mysqli

我正在为DJ制作DJ面板(哦,真的)。有时技术人员想要更新网站,因此需要有一个"维护脚本"禁用整个网站。

// The website is connected to DB and session is running

// if maintenance is enabled, throw the maintenance page in there and exit everything else
if($maintenance) {include("inc/maintenance.php");exit();}

// fetch the db info for the maintenance script
$statussen1 = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM status3 WHERE naam='onderhoud'");
$statussen  = mysqli_fetch_array($statussen1);

// check if 'maintenance' row in the DB is set to AAN (means ON in Dutch)
if(htmlentities($statussen['status']) == 'AAN') {

// if maintenance is AAN, check if the user is an staff member
if(htmlentities($gegevens['rang']) == '1' // owner
OR htmlentities($gegevens['rang']) == '2' // head dj
OR htmlentities($gegevens['rang']) == '3' // assistant head dj
OR htmlentities($gegevens['rang']) == '4' // pilot dj
OR htmlentities($gegevens['rang']) == '5' // test dj
OR htmlentities($gegevens['rang']) == '6' // something else, not sure what 6 is called :P
OR htmlentities($gegevens['nieuwsreporter']) == '1'
OR htmlentities($gegevens['moderator']) == '1') {
    // disable the maintenance script if staff
    $maintenance = false;
} else {
    // or if the user is no staff, enable the maintenance script
    $maintenance = true;
}
}

我认为我已经做了很好的工作,试图在剧本中解释这一切是什么,所以你会更好地理解它。但是,当DB中的维护设置为AAN时,脚本会给我一个空白页。

我已经100次检查了脚本,但无法找到与问题相关的任何内容。是的,我已将错误报告设置为E_ALL并且只给出了这个错误:

  

注意:未定义的变量:维护   第7行xxx

第7行是

if($maintenance) {include("inc/maintenance.php");exit();}

但这不应该是造成空白页面的真正问题。我一定在某处做错了。请帮帮我。

2 个答案:

答案 0 :(得分:0)

将第7行更改为

if( isset($maintenance) ) {include("inc/maintenance.php");exit();}

然后编辑其余代码,使其如下所示:

// The website is connected to DB and session is running


// you can't use maintenance up here, php knows nothing about this variable.
// it needs to be set before you can use it

// fetch the db info for the maintenance script
$statussen1 = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM status3 WHERE naam='onderhoud'");
$statussen  = mysqli_fetch_array($statussen1);

// check if 'maintenance' row in the DB is set to AAN (means ON in Dutch)
if(htmlentities($statussen['status']) == 'AAN') {

    // if maintenance is AAN, check if the user is an staff member
    if(htmlentities($gegevens['rang']) == '1' // owner
    OR htmlentities($gegevens['rang']) == '2' // head dj
    OR htmlentities($gegevens['rang']) == '3' // assistant head dj
    OR htmlentities($gegevens['rang']) == '4' // pilot dj
    OR htmlentities($gegevens['rang']) == '5' // test dj
    OR htmlentities($gegevens['rang']) == '6' // something else, not sure what 6 is called :P
    OR htmlentities($gegevens['nieuwsreporter']) == '1'
    OR htmlentities($gegevens['moderator']) == '1') {
        // disable the maintenance script if staff
        $maintenance = false;
    } else {
        // or if the user is no staff, enable the maintenance script
        $maintenance = true;
        // if maintenance is enabled, throw the maintenance page in there and exit everything else
        include("inc/maintenance.php");exit();
    }
}

答案 1 :(得分:0)

您在设置之前检查$maintenance的值。将其向下移过else

// fetch the db info for the maintenance script
$statussen1 = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM status3 WHERE naam='onderhoud'");
$statussen  = mysqli_fetch_array($statussen1);

// check if 'maintenance' row in the DB is set to AAN (means ON in Dutch)
if(htmlentities($statussen['status']) == 'AAN') {

    // if maintenance is AAN, check if the user is an staff member
    if(htmlentities($gegevens['rang']) == '1' // owner
    OR htmlentities($gegevens['rang']) == '2' // head dj
    OR htmlentities($gegevens['rang']) == '3' // assistant head dj
    OR htmlentities($gegevens['rang']) == '4' // pilot dj
    OR htmlentities($gegevens['rang']) == '5' // test dj
    OR htmlentities($gegevens['rang']) == '6' // something else, not sure what 6 is called :P
    OR htmlentities($gegevens['nieuwsreporter']) == '1'
    OR htmlentities($gegevens['moderator']) == '1') {
        // disable the maintenance script if staff
        $maintenance = false;
    } else {
        // or if the user is no staff, enable the maintenance script
        $maintenance = true;
    }
    // if maintenance is enabled, throw the maintenance page in there and exit everything else
    if($maintenance) {
        include("inc/maintenance.php");
        exit();
    }
}