管理员用户失去管理员状态

时间:2016-02-03 00:02:20

标签: php

Okey,所以我正在我的网站上工作,我有两个页面,我不希望每个成员都看到,我试图为他们添加管理员检查。 现在的问题是,当我第一次登录时(作为管理员),我只能在菜单中看到2个管理页面。如果我点击它们,它会把我扔回默认页面。然后将菜单作为默认用户加载。

这是我的登录,页面数组和页面加载:

$database = new DatabaseObject($host, $username, $password, $database);

    if(!isset($_SESSION['user_id']) && $_POST['login']){
        $username = $database->clean($_POST['username']);
        $password = $database->clean($_POST['password']);

        $result = $database->query("SELECT id, admin, username, password FROM users WHERE username='$username' LIMIT 1");
        try{
            if($database->num_rows($result) == 0){
                throw new Exception ('User does not exist!');
            }

            $user = $database->fetch($result);
            $user_type = $user['admin'];
            if(sha1($password) != $user['password']){
                throw new Exception('Invalid password!');
                echo "sha1($password)";
                echo ".$user.['password']";
            }
            $_SESSION['user_id'] = $user['id'];
        }   
        catch (Exception $e){
            echo $e->getMessage();
        }
    }

// If user is logged in, load data and select page
if(isset($_SESSION['user_id'])) {
    require('user.php');
    $player = new user($_SESSION['user_id'], $database);
    $default_page = 'profile';

        // Check if user is admin
        if($user_type == 1){
        $pages = array(
            'profile' => array(
                'name' => 'Profile',
                'file' => 'profile.php',
                'function' => 'profile',
            ),
            'arena' => array(
                'name' => 'Battle Arena',
                'file' => 'arena.php',
                'function' => 'arena',
            ),
            'weapon_shop' => array(
                'name' => 'Weapon Shop',
                'file' => 'weaponShop.php',
                'function' => 'weaponShop',
            ),
            'alchemy_store' => array(
                'name' => 'Alchemy Store',
                'file' => 'healingShop.php',
                'function' => 'healingShop',
            ),
            'create_monster' => array(
                'name' => 'Create Monster',
                'file' => 'monsterPages.php',
                'function' => 'createMonster',
            ),
            'create_weapon' => array(
                'name' => 'Add New Weapon',
                'file' => 'weaponPages.php',
                'function' => 'createWeapon',
            ),
        ); 
    }
        else{
            $pages = array(
            'profile' => array(
                'name' => 'Profile',
                'file' => 'profile.php',
                'function' => 'profile',
            ),
            'arena' => array(
                'name' => 'Battle Arena',
                'file' => 'arena.php',
                'function' => 'arena',
            ),
            'weapon_shop' => array(
                'name' => 'Weapon Shop',
                'file' => 'weaponShop.php',
                'function' => 'weaponShop',
            ),
            'alchemy_store' => array(
                'name' => 'Alchemy Store',
                'file' => 'healingShop.php',
                'function' => 'healingShop',
            ),
            );
        }
            if(!empty($_GET['page'])) {
        $page = strtolower(trim($_GET['page']));

        if(isset($pages[$page])) {
            require($pages[$page]['file']);
            echo "<p class='pageTitle'>" . $pages[$page]['name'] . "</p>";
            $self_link = '?page=' . $page;
            $pages[$page]['function']();
        }
        else {
            require($pages[$default_page]['file']);
            echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
            $self_link = '?page=' . $default_page;
            $pages[$default_page]['function']();
        }
    }
    else {
        require($pages[$default_page]['file']);
        echo "<p class='pageTitle'>" . $pages[$default_page]['name'] . "</p>";
        $self_link = '?page=' . $default_page;
        $pages[$default_page]['function']();
    }

    }

我用这个用链接填充我的菜单:

    <div id='menu'>
        <ul>
<?php   foreach($pages as $id => $page){
            echo "<li><a href='?page={$id}'>" . $page['name'] . "</a></li>";
        } ?>
        </ul>
    </div>

任何帮助解决这个问题将非常感谢! 感谢您的时间和阅读。

0 个答案:

没有答案