1

エラーをログに記録するベース コントローラーがあり、ログ メッセージにすべての要求データ、すべてのヘッダー、および要求本文を含めたいと考えています。CodeIgniter でそれを行うにはどうすればよいですか?

4

3 に答える 3

1

すべての Body リクエスト (存在する場合)$this->input を含むと同等のCodeIgniterは、foo からの出力は次のようになります。file_get_contents('php://input')var_dump($this->input)

object(CI_Input)[9]
  public 'ip_address' => boolean false
  public 'user_agent' => boolean false
  public '_allow_get_array' => boolean true
  public '_standardize_newlines' => boolean true
  public '_enable_xss' => boolean false
  public '_enable_csrf' => boolean false
  protected 'headers' => 
    array (size=0)
      empty
  public 'security' => &
    object(CI_Security)[8]
      protected '_xss_hash' => string '' (length=0)
      protected '_csrf_hash' => string '' (length=0)
      protected '_csrf_expire' => int 7200
      protected '_csrf_token_name' => string 'ci_csrf_token' (length=13)
      protected '_csrf_cookie_name' => string 'ci_csrf_token' (length=13)
      protected '_never_allowed_str' => 
        array (size=10)
          'document.cookie' => string '[removed]' (length=9)
          'document.write' => string '[removed]' (length=9)
          '.parentNode' => string '[removed]' (length=9)
          '.innerHTML' => string '[removed]' (length=9)
          'window.location' => string '[removed]' (length=9)
          '-moz-bindin.......//and many other data.

ヘッダー laso には、PHP 5.4 の新しいコマンドがありheaders_list()ます。また、次のような他の多くのデータをCI シードに含めることができます。ip_address, user_agent ,last_activity

于 2013-01-17T22:57:40.403 に答える
0

ポストコントローラーフックを使用できます。

  • application / config/config.phpでフックを有効にします。
  • 次に、application / config/hooks.phpで;

    $hook['post_controller'] = array(
                            'class'    => 'PostController',
                            'function' => 'log_it',
                            'filename' => 'PostController.php',
                            'filepath' => 'hooks',
                            'params'   => array()
                            );
    

  • application / hooks/PostController.phpにファイルを作成します

    <?php
    
    class PostController {
    
    private $ci;
    private $filepath;
    
    public function __construct() {
        $this->ci =& get_instance();
        $log_path = ($this->ci->config->item('log_path') != '') ? $this->ci->config->item('log_path') : APPPATH.'logs/';
        $this->filepath = $log_path.'action_log';
    }
    
    public function log_it($log_message=NULL) {
        if ($log_message == NULL) {
            $log_message = date('d/m/Y H:i:s')."::";
            $log_message .= $this->ci->session->userdata('username')."::";
            $log_message .= $this->ci->input->ip_address()."::";
            $log_message .= $this->get_client_ip()."::";
            $log_message .= $this->ci->router->class."::";
            $log_message .= $this->ci->router->method."::";
            $log_message .= uri_string()."::";
            if (isset($_POST)) {
                foreach ($_POST as $key => $value) {
                    $log_message .= "[$key] => $value;";
                }
            }
            $log_message .= "\r\n";
        }
    
    
        $fp = fopen($this->filepath, FOPEN_WRITE_CREATE);
        fwrite($fp, $log_message);
        fclose($fp);
    }
    
    private function get_client_ip() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
      }
     }
    
    ?>
    
于 2013-01-17T08:11:06.503 に答える
0

ヘッダーの詳細について

$this->load->library('user_agent');
$reqUserIp = $this->input->ip_address();
$userAgent = $this->agent->agent_string();
$reqHeaders = $this->input->request_headers();
于 2013-01-17T08:08:16.057 に答える