duanmao1919 2015-12-01 08:57
浏览 30
已采纳

无法在标头中传递$变量()

I'm trying to modify a login script from a File Manager I currently use on my website.

Currently, when the login attempt fails (wrong password, blank fields, etc..) an error message is stored in the $errors variable, and at the end of the script a function will include a file.php with $errors as a parameter, so the error message can be echo'd.

What I am unsuccessfully trying to do is replacing this include + parameter($errors) by a header() + $errors.

I have been around every thread dealing with this but so far I haven't found anything that works for me, I need some help : /

Here is the part of the login script that store the error message in $errors and include the file.php :

if(!isset($_SESSION['simple_auth']['username']) || isset($_GET["login"])) {

            session_destroy();
            session_start();

            if(isset($_POST["submit"])) {

                $user = gg::getUser($_POST['username']);

                if (isset($user['permissions']) && !strstr($user['permissions'], 'r')
                        || ($user['username'] == 'guest' && ggconf::get('allow_guests') == false)){
                    $errors = "Access Forbidden";
                    gg::writeLog('auth bad - not activated');
                }

                if (!isset($_POST['username']) || !isset($_POST['password']) || $_POST['username'] == '' || $_POST['password'] == ''){
                    $errors = "Enter username and password.";
                    gg::writeLog('auth bad - blank fields');
                }

                if (isset($user['akey']) && $user['akey'] != '' && strpos($user['akey'], 'otp-') === false){
                    $errors = "Please open your email and click on the link to proceed.";
                    gg::writeLog('auth bad - not activated');
                }

                if(!$errors && $user['username'] == $_POST['username'] && gg::checkPassword($_POST['password'], $user['password'])){
                    $this->loginUser($user);
                }

                if(!$errors){
                    $errors = "Wrong username or password.";
                    gg::writeLog('auth bad - wrong username or password');
                    sleep(1);
                }

            }


            if (!isset($_GET["login"]) && ggconf::get('allow_guests') == true){

                $user = gg::getUser('guest');

                if($user){
                    $this->loginUser($user);
                }
                // reload
                header('Location: '.ggconf::get('base_url'));
                die;


            }


            gg::display("login.php", array('errors' => $errors));
            exit;
        }

As you see at the end of this code is the gg::display function that will include the file and its parameter, the function gg::display is the following :

/* DISPLAY VIEWS CONTROLLER */

    public static function display($view, $params = null){

        require_once ggconf::get('base_path').$view;
    }

And last, here is the code inside the file.php that will display the error :

<?php if (isset($params['errors'])):?>
<div class=>
<?php echo $params['errors'];?>
</div>
<?php endif;?>

How can I get my variable $errors with a header() instead of the require_once currently used in the script ?

I found a lot threads dealing with this but nothing worked for me, also note that I'm not very experienced in php yet.

Anyways, I'm thankful for the help, and any suggestions are welcome.

-apatik

展开全部

  • 写回答

1条回答 默认 最新

  • dongwu9972 2015-12-01 09:12
    关注

    If you are redirecting to another page you should store the parameters via php session

    session_start(); // this should be on top
    $_SESSION['errors'] = $errors; // Add this after writing an error
    

    And here is the view

    <?php if (isset($_SESSION['errors'])):?>
    <div class=>
    <?php echo $_SESSION['errors'];?>
    </div>
    <?php endif;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部