search
HomeBackend DevelopmentPHP TutorialHow CodeIgniter records error logs

How CodeIgniter records error logs

Jun 02, 2018 am 10:31 AM
codeignitermethodlog

This article mainly introduces the method of CodeIgniter recording error logs, analyzes the file structure and corresponding functions of the CodeIgniter framework in detail, analyzes the implementation techniques of CodeIgniter framework recording error logs with examples, and analyzes hidden index files and data. The summary of transmission and other techniques is very comprehensive. Friends who need it can refer to

. The example in this article describes the method of CodeIgniter recording error logs. Share it with everyone for your reference, the details are as follows:

CI workflow:

All entries are entered from index.php in the root directory, determine the directory where the application is located Finally, load the codeigniter/CodeIgniter.php file, which will load the following files sequentially to execute the entire process.

index.php: Detect the file path and load the codeigniter.php file

codeigniter.php: Load the Common/constants.... file . Get file mode, set timer, instantiate class (error class, extension class, hook class, system extension, configuration class, encoding class, routing class, process class, output class, security class, language class, controller), load Request method, render output view.

A class of CodeIgniter will be saved as a php file. The class name has the same name as the file name. Its core application class will have "CI_" in front of the class name.

system/core/common.php: Contains detecting php version, file permissions, loading core classes, obtaining configuration parameters, loading exception/error classes, obtaining http request status, etc. Public functions

application/config/constants.php: Set file permission constants, application macro definition files

system/core/Benchmark.php:Used to record execution time

system/core/Hooks.php: Detect whether there is a hook object call

system/core/Config.php: Provide methods for managing configuration files and detecting application/config/config.php parameters

application/config/config.php: Configuring global parameters

system/core/URI.php: Parse url parameters

system/core/Router.php: Detect routing configuration and parse HTTP requests to determine who will handle it

system/core/Output.php: Check whether there is a cache file, and if it exists, output the content directly.

system/core/Input.php: Filter HTTP requests and any user-submitted data

system/core/Long.php: Initialization Prompt language variable

system/core/controller.php:Control output class

Record error log:

Default program Do not record error logs. If necessary, you can set it:

1. Set it in application/config/config.php:

$config['log_threshold'] = 1//(可设置:1/2/3/4)

If it is 0, it means no error log is output. For details, please see the introduction inside;

2. Call the global function log_message('level','message') on the page where errors need to be written. There are three levels, one The first one is error, that is, the PHP running error. The second one is debug, system debugging. CI itself has also added its own system debug on many pages. The third one is info, which introduces some messages during operation. The message content is written by yourself;

3. By default, the error log is stored in application/logs/log-[time].php. It stores files by date. For example: log-2011-6-26 means that today’s log content is stored. Generally, it is hidden. The log content must move this address. You can set the path in $config['log_path']. It is best to use the complete path information as required.

Set your own global variables/configuration:

Sometimes you need to define your own full process variables for use in other places, such as customized sessions, etc., in CI This job is also very easy.

1. Create your own config file in application/config/, and pay attention to the location of the file. For example, create your own configuration file mysetting.php, content,

$config['try'] = 'this is my trying';

2. Use $this->config where you need to call custom global variables. ->load('settingfile') function, for example:

$this->config->load('mysetting');

If necessary, it can also be set through application/config/autoload.php Load automatically.

3. Next, use the

$this->config->item('varname')

function on the same page, for example: $this->config->item(' try'); will output: this is my trying;
As can be seen from the above, The function call in CI is in the form of: $this->filename. It can also be seen that CI looks at the entire system. into a large class, and then obtain the corresponding methods through loading, inheritance, etc.
More custom variable reference: http://codeigniter.org.cn/user_guide/libraries/config.html

Hide index.php and load external files:

其实不管是在用CI还是ZF都有同样一个问题,就是路径的问题。前期,我在用ZF做CMS时,我在.htaccess文件中设置了如遇到js,css,img等资源文件都不重定向。但今天在用CI时,却忘记了,弄了半天都没搞好,登陆CI的中国官方网,终于在论坛高手的帮助下把问题觖决了,在这里把它贴出来,供大家分享。

首先,隐藏url中的index.php文件,这样访问其它目录的时候就不会有http://www.xxx.com/index.php/xxx的样式出现,面是直接http://www.xxx.com/xxx形式,在根目录.htaccess文件里设置(作用是隐藏index.php,有时index.php可能不在根目录,则htaccess须移到index.php所在目录),如下:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
#这里排除了images、js、css目录及index.php、robots.txt文件
RewriteRule ^(.*)$ index.php/$1 [L]

这里JS,CSS,IMG等资源文件夹与SYSTEM文件夹放在同一级下,独立放置的好处是不用受htaccess的限制,因为htaccess文件写明Deny from all,即拒绝访问。打开application/config/config.php改写配置:

$config['base_url'] = "http://127.0.0.1/";
$config['index_page'] = "index.php";

如果

$config['base_url'] = http://127.0.0.1;

后面没加'/',则在model_rewrite最后一行应写RewriteRule ^(.*)$ /index.php/$1 [L],在index.php前加一个'/'。然后在JS文件夹中建立ajax.js文件,我在VIEW层中的文件为index.html。这样我要引入JS时,可以用CI自带的BASE_URL来设置,如下:

在controllers里相关控制网页里添加(在其它load之前):

$this->load->helper('url');

在views表现的index.html里:

复制代码 代码如下:

注:这里url是网站相对URL(好处是可以更改根目录后相对地址不用改变)

这里js文件夹没有重定向,所以可以正常访问,而如果是受限制的页面则比较麻烦了。

好了,CI中引入外部的JS与CSS就这么简单。

注别的说明:“ RewriteCond $1 !^(index\.php|images|js|css|robots\.txt) ”这里代码的意思是:任意你想访问的资源都不被重定向时,都可写在这里。有时,网站没有加载CSS,JS(它的路径都是正确的)时,都是被重定向了,这要注意。

具体可查看CI的中国官论坛 http://codeigniter.org.cn/user_guide/helpers/url_helper.html,URL辅助函数一节,
http://codeigniter.org.cn/user_guide/general/urls.html,url设置,
http://codeigniter.org.cn/forums/thread-4-1-2.html,Hex关于隐藏index.php的说明,但他在model_rewrite用了index\\.php,我觉得用双反斜杠有误。

(另外:特别谢谢CI中国官论坛上的Hex 与visvoy )

数据间的传输:

1、将数据从控制器传入视图

由于控制器controllers在ci中扮演交通警察的角色,其是一个大类,而视图view作为controller类中的一个函数中的函数,所以view可以使用controller中的属性。所以可以这样写:

Controller类Test

class Test extends CI_Controller {
 public static $test2=''; //定义一个属性
 public function __construct(){
 parent::__construct();
 self::$test2 = $this->load->view('new','',true); //给$test2这个属性赋值
 }
 public function index() {
 $this->load->helper('url');
 $this->load->view('anchor');
 }
}

View.php

<?php
echo Test::$test2; //直接使用类中的值
?>

这种直接使用controllers类中的值的方法虽然可行,却不是ci所提倡的。一般来说在controller中使用$this->load->view()的时候可以通过参数传值给view视图:

function index()
{
 $data[&#39;css&#39;] = $this->css;
 $data[&#39;base&#39;] = $this->base;
 $data[&#39;mytitle&#39;] = &#39;Welcome to this site&#39;;
 $data[&#39;mytext&#39;] = "Hello, $name, now we&#39;re getting dynamic!";
 $this->load->view(&#39;testview&#39;, $data); //$data通过参数传递到view
}

这里,把需要传递的数值加入至$data数组,ci在核心类中给自动使用extract()函数把数组“解压”出来,成为一个个变量。所以在view中可以直接这样使用变量:

echo $css;

2、模型与视图的交互

在ci中模型总是用以处理数据,模型中数据处理也是通过controller中转到view,所以最好不要试图模型直接与视图联系。手册中有这样一个例子:

class Blog_controller extends CI_Controller {
 function blog() {
 $this->load->model(&#39;Blog&#39;); //载入模型
 $data[&#39;query&#39;] = $this->Blog->get_last_ten_entries(); //使用模型中的方法,将返回值存入$data数组
 $this->load->view(&#39;blog&#39;, $data); //像上例一样,通过参数传给视图view
 }
}

相关推荐:

CI框架(CodeIgniter)实现的数据库增删改查操作总结

CI框架(CodeIgniter)实现的导入、导出数据操作

The above is the detailed content of How CodeIgniter records error logs. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use