duanmao1919 2015-12-01 16: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 17: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;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题