php header 302重定向失效問題


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

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.php

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.php

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();
控制您的瀏覽器cache


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()
說明:這個函數經常使用,效率很高。
2、ob_start :打開輸出緩沖區
函數格式:void ob_start(void)
說明:當緩沖區激活時,所有來自PHP程序的非文件頭信息均不會發送,而是保存在內部緩沖區。為了輸出緩沖區的內容,可以使用ob_end_flush()或flush()輸出緩沖區的內容。
3 、ob_get_contents :返回內部緩沖區的內容。
使用方法:string ob_get_contents(void)
說明:這個函數會返回當前緩沖區中的內容,如果輸出緩沖區沒有激活,則返回 FALSE 。
4、ob_get_length:返回內部緩沖區的長度。
使用方法:int ob_get_length(void)
說明:這個函數會返回當前緩沖區中的長度;和ob_get_contents一樣,如果輸出緩沖區沒有激活。則返回 FALSE。
5、ob_end_flush :發送內部緩沖區的內容到瀏覽器,並且關閉輸出緩沖區。
使用方法:void ob_end_flush(void)
說明:這個函數發送輸出緩沖區的內容(如果有的話)。
6、ob_end_clean:刪除內部緩沖區的內容,並且關閉內部緩沖區
使用方法:void ob_end_clean(void)
說明:這個函數不會輸出內部緩沖區的內容而是把它刪除!
7、ob_implicit_flush:打開或關閉絕對刷新
使用方法:void ob_implicit_flush ([int flag])
說明:使用過Perl的人都知道$|=x的意義,這個字符串可以打開/關閉緩沖區,而ob_implicit_flush函數也和那個一樣,默認為關閉緩沖區,打開絕對輸出后,每個腳本輸出都直接發送到瀏覽器,不再需要調用 flush()


二、深入了解:

1. 關於Flush函數:
這個函數在PHP3中就出現了,是一個效率很高的函數,他有一個非常有用的功能就是刷新browser的cache.我們舉一個運行效果非常明顯的例子來說明flush.
Example 2.

程序代碼 程序代碼
<?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系列函數做些什么……

一、 靜態模版技術

簡介:所謂靜態模版技術就是通過某種方式,使得用戶在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);
?>


這樣,所謂的靜態模版就很容易的被實現了……

二、 捕捉輸出

以上的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;
}
}
?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM