php header 302重定向失效问题

E:\html\pim\php_weili_activities\application\controllers\user.phpphp

public function login()
    {
        if ($this->check_login()) {
            redirect(base_url('user/index'));
            //redirect(base_url('user/apps'));
        }
        //redirect($this->_manager_base_url);

        //处理scrf攻击的问题。
        /* $value = "aspire_DefenseSCRF";
         if(!$_COOKIE['cookie_scrf']){
             setcookie("cookie_scrf", $value, time()+3600);
         }
         $hash = md5($_COOKIE['cookie_scrf']);
        */
        $hash_post = $this->input->post('hash', true);
        $data = array(
            'error' => '',
            'fill_name' => '',
            'hash' => $hash_post,
        );

        //解决主机头攻击的问题
        $host = 'http://' . $_SERVER['HTTP_HOST'];
        /* if ((!strpos($host, '10086dg.cn')) &&(!strpos($host, 'weili.cn')) && (!strpos($host, 'dghui.com')) && (!strpos($host, 'gitdg')) && (!strpos($host, 'cmpower.cn'))) {
             $name = '';
             $this->_load_login_view($data, $name, '你的请求url不合法');
             return;
         } */

        if (!$this->input->post()) {
            $token = md5(uniqid(rand(), TRUE));
            $this->wlsession->set('token_login_cache', $token);
            $this->wlsession->write_sid_to_cookie();
            //$this->cache->save('token_login_cache', $token,1800);
            //$this->cache->save('token_time', time(),1800);
            $data = array(
                'error' => '',
                'fill_name' => '',
                'hash' => $token,
            );
            $this->_load_login_view($data);
            return;
        }


        // 验证登陆
        $name = strval($this->input->post('n_input', true));
        $password = strval($this->input->post('p_input', true));


        //处理scrf攻击的问题。
        /*if ( $hash_post != $this->wlsession->get('token_login_cache')) {
            $name = '';
            $token = md5(uniqid(rand(), TRUE));
            $this->wlsession->set('token_login_cache', $token);
            $this->wlsession->write_sid_to_cookie();
            //$this->cache->save('token_login_cache', $token,1800);
            //$this->cache->save('token_time', time(),1800);
            $data = array(
                'error' => '',
                'fill_name' => '',
                'hash' => $token,
            );
            $this->_load_login_view($data, $name, '你的请求不合法');
            //.$hash_post .'!='. $this->cache->get('token_login_cache')
            return;
        }*/

        $token_age = time() - $this->cache->get('token_time');
        if ($token_age >= 300) {
            //$name = '';
            //$this->_load_login_view($data, $name, '你的请求时间不合法'.$this->cache->get('token_time'));
            //return;
        }


        if (empty($name) || empty($password)) {
            $name = strlen($name) == 64 ? $this->_rsa_decode($name) : $name;
            $this->_load_login_view($data, $name, '用户名或密码不能为空');
            return;
        }

        if (strlen($name) != 64 || strlen($password) != 64) {
            $this->_load_login_view($data, $name, '用户名或密码错误,请重试');
            return;
        }

        // rsa 解密用户名及密码
        $name = $this->_rsa_decode($name);
        $password = $this->_rsa_decode($password);
        if (!$name || !$password) {
            $this->_load_login_view($data, $name, '用户名或密码错误,请重试');
            return;
        }

        // 是否须要验证码?
//         if ($this->_check_need_captcha($name)) {
        if (false) {
            $code = strval($this->input->post('code', true));
            if (empty($code)) {
                $this->_load_login_view($data, $name, '验证码不能为空');
                return;
            }

            $this->load->library('Captcha');
            if (!$this->captcha->verify_captcha($name, $code)) {
                $this->_load_login_view($data, $name, '验证码输入错误,请从新输入');
                return;
            } else {
                $this->captcha->destroy_captcha($name);
            }
        }

        $this->load->model('user_model', 'user');
        $user = $this->user->get_user_by_name($name);

        if (false === $user) {
            $this->_raise_error_times($name);
            $this->_load_login_view($data, $name, '用户名或密码错误,请重试');
            return;
        }

        if ($user->password != md5($user->salt . $password)) {
            $this->_raise_error_times($name);
            $this->_load_login_view($data, $name, '用户名或密码错误,请重试');
            return;
        }

        //检查角色组是否处于被禁止使用状态*************************
        $row = $this->db->from("wl_auth_role_user")->where('user_id', $user->id)->get()->row();
        $rolezu = $this->db->from('wl_auth_role')->where('id', $row->role_id)->get()->row();
        if ($rolezu->is_del != 1) {
            $this->_raise_error_times($name);
            $this->_load_login_view($data, $name, '该角色组被禁用,请联系超级管理员解除');
            return;
        }
        //检查用户是否处于被禁止使用状态
        if ($user->status != 1) {
            $this->_raise_error_times($name);
            $this->_load_login_view($data, $name, '该用户被禁用,请联系超级管理员解除');
            return;
        }
        //**********************************************

        // 检查是否已绑定
        $this->load->model('binding_model');
        $binded_wx_name = $this->binding_model->get_name_by_user_id($user->id);


        // 登陆成功, 设置session
        $this->user_id = $user->id;
        $this->user_name = $user->login_name;

        //为了数据共用,缓存user_id都是存父帐号的
        if ($user->pid == 0) {
            $this->wlsession->set('user_id', $user->id);
        } else {
            $this->wlsession->set('user_id', $user->pid);
        }


//         $this->wlsession->set('sub_user_id', '');
        $this->wlsession->set('auth_user_type', AUTH_USER_TYPE_USER);
        $this->wlsession->set('login_name', $user->login_name);
        $this->wlsession->set('wx_name', $binded_wx_name);
        $this->wlsession->set('wx_qr_code', $this->binding_model->get_wx_qr_code_by_user_id($user->id));//显示用户二维码图片
        $this->wlsession->write_sid_to_cookie();
        //设置用户的权限session******15-04-09 ***********************************
        $this->load->model('user_model');
        $this->load->model('auth_role_model');
        $this->load->model('auth_node_model');
        // $this->load->model('auth_role_node_model','auth_role_node_model');
        // $role_name = $this->db->select("name")->from('wl_auth_role')->where('id', $row->role_id)->get()->row();

        // $auth_powers = $this->auth_role_node_model->get_powers(intval($row->role_id));
        // $this->wlsession->set('auth_powers', $auth_powers);//当前用户权限存到session中
        // $this->wlsession->set('user_role', $role_name->name);//所属角色
        $this->wlsession->set('user_pid', $user->pid);
        //*******************************************************************
        // 更新登陆信息
        $last_login_date = date('Y-m-d h:i:s', time());
        $update_user = array(
            'login_times' => $user->login_times + 1,
            'last_login_date' => $last_login_date,
            'last_login_ip' => $_SERVER["REMOTE_ADDR"]
        );
        $this->user->update($user->id, $update_user);
        echo '--hi4--';
        // 重置错误次数控制
        $this->_reset_error_times($name);
        // 若是未绑定,则跳到绑定页
        if (empty($binded_wx_name)) {
            redirect(base_url('bind/index'));
        } else {
            //redirect(base_url('user/index'));
            redirect(base_url('user/apps'));
            exit;
        }
    }

E:\html\pim\php_weili_activities\system\helpers\url_helper.phphtml

if ( ! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location', $http_response_code = 302) {
        if (!preg_match('#^https?://#i', $uri)) {
            $uri = site_url($uri);
        }

        switch ($method) {
            case 'refresh' : 
                header("Refresh:0;url=" . $uri);
                break;
            default : 
                header("Location: " . $uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }

}

F:\data\app_logs\php_weili_activities\log20181030.phpnode

INFO  - 2018-10-30 09:34:05 --> [async.bef.MRDSmiOb](/target/index),([])
ERROR - 2018-10-30 09:34:05 --> Severity: Notice  --> Undefined index: token E:\html\pim\php_weili_activities\application\core\WL_Controller.php 35
INFO  - 2018-10-30 09:34:05 --> [async.aft.MRDSmiOb] exec=0 S
ERROR - 2018-10-30 09:34:05 --> Severity: Notice  --> Use of undefined constant I - assumed 'I' E:\html\pim\php_weili_activities\system\core\Hooks.php 226
INFO  - 2018-10-30 09:34:16 --> [async.bef.5k9aTj6K](/user/login),([])
ERROR - 2018-10-30 09:34:16 --> Severity: Notice  --> Undefined index: token E:\html\pim\php_weili_activities\application\core\WL_Controller.php 35
INFO  - 2018-10-30 09:34:49 --> [async.bef.FAQ8j8DN](/user/login),([])
ERROR - 2018-10-30 09:34:49 --> Severity: Notice  --> Undefined index: token E:\html\pim\php_weili_activities\application\core\WL_Controller.php 35
ERROR - 2018-10-30 09:34:49 --> Severity: Warning  --> Cannot modify header information - headers already sent by (output started at E:\html\pim\php_weili_activities\application\core\WL_Controller.php:1) E:\html\pim\php_weili_activities\application\libraries\WLSession.php 60
INFO  - 2018-10-30 09:34:49 --> [async.aft.FAQ8j8DN] exec=0 S
ERROR - 2018-10-30 09:34:49 --> Severity: Notice  --> Use of undefined constant I - assumed 'I' E:\html\pim\php_weili_activities\system\core\Hooks.php 226
INFO  - 2018-10-30 09:34:56 --> [async.bef.edqxLC3k](/user/login),([])
root@DESKTOP-I4OIMJC /cygdrive/e/html/pim/php_weili_activities
# grep -anR 'ob_start' ./
./application/controllers/dg/qrcode_extend_task.php.bak:135:                    ob_start();
./application/controllers/dg/scene_code_task.php.bak:145:               ob_start();
./application/controllers/scene_code.php.bak:386://             ob_start();
./application/controllers/schedule/qrcode_extend_worker.php.bak:139:                    /*ob_start();
./application/helpers/public_function_helper.php:447:       ob_start();
./application/helpers/public_function_helper.php:476:       ob_start();
./application/helpers/public_function_helper.php:542:            ob_start();
./application/helpers/public_function_helper.php:564:        ob_start();
./static/js/ueditor/php/getRemoteImage.php:55:            ob_start();
./system/core/Exceptions.php:145:               ob_start();
./system/core/Exceptions.php:181:               ob_start();
./system/core/Loader.php:821:           ob_start();
./system/core/Output.php:379:                                   ob_start('ob_gzhandler');
./system/helpers/url_helper.php:341:            ob_start();

root@DESKTOP-I4OIMJC /cygdrive/e/html/pim/php_weili_activities
root@DESKTOP-I4OIMJC /cygdrive/e/html/pim/php_weili_activities
# php -r "phpinfo();"|grep --color 'output_buffering'
output_buffering => 0 => 0

用PHP的ob_start();
控制您的浏览器cachegit


Output Control 函数可让你自由控制脚本中数据的输出。它很是地有用,特别是对于:当你想在数据已经输出后,再输出文件头的状况。输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生影响,只对那些相似于 echo() 和 PHP 代码的数据块有做用。
咱们先举一个简单的例子,让你们对Output Control有一个大体的印象:
Example 1.数据库

程序代码 程序代码
<?php
ob_start(); //打开缓冲区
echo \"Hellon\"; //输出
header("location:index.php"); //把浏览器重定向到index.php
ob_end_flush();//输出所有内容到浏览器
?>浏览器


全部对header()函数有了解的人都知道,这个函数会发送一段文件头给浏览器,可是若是在使用这个函数以前已经有了任何输出(包括空输 出,好比空格,回车和换行)就会提示出错。若是咱们去掉第一行的ob_start(),再执行此程序,咱们会发现获得了一条错误提示:"Header had all ready send by"!可是加上ob_start,就不会提示出错,缘由是当打开了缓冲区,echo后面的字符不会输出到浏览器,而是保留在服务器,直到你使用 flush或者ob_end_flush才会输出,因此并不会有任何文件头输出的错误!缓存


1、 相关函数简介:
一、Flush:刷新缓冲区的内容,输出。
函数格式:flush()
说明:这个函数常用,效率很高。
二、ob_start :打开输出缓冲区
函数格式:void ob_start(void)
说明:当缓冲区激活时,全部来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可使用ob_end_flush()或flush()输出缓冲区的内容。
3 、ob_get_contents :返回内部缓冲区的内容。
使用方法:string ob_get_contents(void)
说明:这个函数会返回当前缓冲区中的内容,若是输出缓冲区没有激活,则返回 FALSE 。
四、ob_get_length:返回内部缓冲区的长度。
使用方法:int ob_get_length(void)
说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents同样,若是输出缓冲区没有激活。则返回 FALSE。
五、ob_end_flush :发送内部缓冲区的内容到浏览器,而且关闭输出缓冲区。
使用方法:void ob_end_flush(void)
说明:这个函数发送输出缓冲区的内容(若是有的话)。
六、ob_end_clean:删除内部缓冲区的内容,而且关闭内部缓冲区
使用方法:void ob_end_clean(void)
说明:这个函数不会输出内部缓冲区的内容而是把它删除!
七、ob_implicit_flush:打开或关闭绝对刷新
使用方法:void ob_implicit_flush ([int flag])
说明:使用过Perl的人都知道$|=x的意义,这个字符串能够打开/关闭缓冲区,而ob_implicit_flush函数也和那个同样,默认为关闭缓冲区,打开绝对输出后,每一个脚本输出都直接发送到浏览器,再也不须要调用 flush()服务器


2、深刻了解:cookie

1. 关于Flush函数:
这个函数在PHP3中就出现了,是一个效率很高的函数,他有一个很是有用的功能就是刷新browser的cache.咱们举一个运行效果很是明显的例子来讲明flush.
Example 2.session

程序代码 程序代码
<?php
for($i = 1; $i <= 300; $i++ ) print(" ");
// 这一句话很是关键,cache的结构使得它的内容只有达到必定的大小才能从浏览器里输出
// 换言之,若是cache的内容不达到必定的大小,它是不会在程序执行完毕前输出的。经
// 过测试,我发现这个大小的底限是256个字符长。这意味着cache之后接收的内容都会
// 源源不断的被发送出去。
For($j = 1; $j <= 20; $j++) {
echo $j."
";
flush(); //这一部会使cache新增的内容被挤出去,显示到浏览器上
sleep(1); //让程序"睡"一秒钟,会让你把效果看得更清楚
}
?>

注:若是在程序的首部加入ob_implicit_flush()打开绝对刷新,就能够在程序中再也不使用flush(),这样作的好处是:提升效率!

2. 关于ob系列函数:
我想先引用个人好朋友y10k的一个例子:
Example 3.

好比你用获得服务器和客户端的设置信息,可是这个信息会由于客户端的不一样而不一样,若是想要保存phpinfo()函数的输出怎么办呢?在没有缓冲区控制以前,能够说一点办法也没有,可是有了缓冲区的控制,咱们能够轻松的解决:
程序代码 程序代码
<?php
ob_start(); //打开缓冲区
phpinfo(); //使用phpinfo函数
$info=ob_get_contents(); //获得缓冲区的内容而且赋值给$info
$file=fopen(\'info.txt\',\'w\'); //打开文件info.txt
fwrite($file,$info); //写入信息到info.txt
fclose($file); //关闭文件info.txt
?>

 

用以上的方法,就能够把不一样用户的phpinfo信息保存下来,这在之前恐怕没有办法办到!其实上面就是将一些"过程"转化为"函数"的方法!
或 许有人会问:"难道就这个样子吗?还有没有其余用途?"固然有了,好比笔者论坛的PHP 语法加亮显示就和这个有关(PHP默认的语法加亮显示函数会直接输出,不能保存结果,若是在每次调用都显示恐怕会很浪费CPU,笔者的论坛就把语法加亮函 数显示的结果用控制缓冲区的方法保留了),你们若是感兴趣的话能够来看看

可能如今你们对ob_start()的功能有了必定的了解,上面的一个例子看似简单,但实际上已经掌握了使用ob_start()的要点。
<1>.使用ob_start打开browser的cache,这样能够保证cache的内容在你调用flush(),ob_end_flush()(或程序执行完毕)以前不会被输出。
<2>. 如今的你应该知道你所拥有的优点:能够在任何输出内容后面使用header,setcookie以及session,这是 ob_start一个很大的特色;也可使用ob_start的参数,在cache被写入后,而后自动运行命令,好比ob_start(\ "ob_gzhandler\");而咱们最经常使用的作法是用ob_get_contents()获得cache中的内容,而后再进行处理……
<3>.当处理完毕后,咱们可使用各类方法输出,flush(),ob_end_flush(),以及等到程序执行完毕后的自动输出。固然,若是你用的是ob_get_contents(),那么就要你本身控制输出方式了。

来,让咱们看看能用ob系列函数作些什么……

1、 静态模版技术

简介:所谓静态模版技术就是经过某种方式,使得用户在client端获得的是由PHP产生的html页面。若是这个html页面不会再被更新,那么 当另外的用户再次浏览此页面时,程序将不会再调用PHP以及相关的数据库,对于某些信息量比较大的网站,例如sina,163,sohu。相似这种的技术 带来的好处是很是巨大的。

我所知道的实现静态输出的有两种办法:
<1>.经过y10k修改的phplib的一个叫template.inc.php类实现。
<2>.使用ob系列函数实现。
对于第一种方法,由于不是这篇文章所要研究的问题,因此再也不赘述。
咱们如今来看一看第二种方法的具体实现:
Example 4.


程序代码 程序代码
<?php
ob_start();//打开缓冲区
?>
php页面的所有输出
<?
$content = ob_get_contents();//取得php页面输出的所有内容
$fp = fopen("output00001.html", "w"); //建立一个文件,并打开,准备写入
fwrite($fp, $content); //把php页面的内容所有写入output00001.html,而后……
fclose($fp);
?>


这样,所谓的静态模版就很容易的被实现了……

2、 捕捉输出

以上的Example 4.是一种最简单的状况,你还能够在写入前对$content进行操做……
你能够设法捕捉一些关键字,而后去对它进行再处理,好比Example 3.所述的PHP语法高亮显示。我的认为,这个功能是此函数最大的精华所在,它能够解决各类各样的问题,但须要你有足够的想象力……
Example 5.

程序代码 程序代码
<?php
Function run_code($code) {
If($code) {
ob_start();
eval($code);
$contents = ob_get_contents();
ob_end_clean();
}else {
echo "错误!没有输出";
exit();
}
return $contents;
?>
}

 

以上这个例子的用途不是很大,不过很典型$code的自己就是一个含有变量的输出页面,而这个例子用eval把$code中的变量替换,而后对输出结果再进行输出捕捉,再一次的进行处理……

Example 6. 加快传输


程序代码 程序代码
<?
/*
** Title.........: PHP4 HTTP Compression Speeds up the Web
** Version.......: 1.20
** Author........: catoc <catoc@163.net>
** Filename......: gzdoc.php
** Last changed..: 18/10/2000
** Requirments...: PHP4 >= 4.0.1
** PHP was configured with --with-zlib[=DIR]
** Notes.........: Dynamic Content Acceleration compresses
** the data transmission data on the fly
** code by sun jin hu (catoc) <catoc@163.net>
** Most newer browsers since 1998/1999 have
** been equipped to support the HTTP 1.1
** standard known as \"content-encoding.\"
** Essentially the browser indicates to the
** server that it can accept \"content encoding\"
** and if the server is capable it will then
** compress the data and transmit it. The
** browser decompresses it and then renders
** the page.
**
** Modified by John Lim (jlim@natsoft.com.my)** based on ideas by Sandy McArthur, Jr** Usage........:** No space before the beginning of the first \'<?\' tag.** ------------Start of file----------** |<?** | include(\'gzdoc.php\');** |? >** |<HTML>** |... the page ...** |</HTML>** |<?** | gzdocout();** |? >** -------------End of file-----------*/ob_start();ob_implicit_flush(0);function CheckCanGzip(){global $HTTP_ACCEPT_ENCODING;if (headers_sent() || connection_timeout() || connection_aborted()){return 0;}if (strpos($HTTP_ACCEPT_ENCODING, \'x-gzip\') !== false) return \"x-gzip\";if (strpos($HTTP_ACCEPT_ENCODING,\'gzip\') !== false) return \"gzip\";return 0;}/* $level = compression level 0-9, 0=none, 9=max */function GzDocOut($level=1,$debug=0){$ENCODING = CheckCanGzip();if ($ENCODING){print \"n<!-- Use compress $ENCODING -->n\";$Contents = ob_get_contents();ob_end_clean();if ($debug){$s = \"<p>Not compress length: \".strlen($Contents);$s .= \"Compressed length: \".strlen(gzcompress($Contents,$level));$Contents .= $s;}header(\"Content-Encoding: $ENCODING\");print \"x1fx8bx08x00x00x00x00x00\";$Size = strlen($Contents);$Crc = crc32($Contents);$Contents = gzcompress($Contents,$level);$Contents = substr($Contents, 0, strlen($Contents) - 4);print $Contents;print pack(\'V\',$Crc);print pack(\'V\',$Size);exit;}else{ob_end_flush();exit;}}?>

相关文章
相关标签/搜索