search
HomeBackend DevelopmentPHP TutorialA comprehensive summary of the method of recording error logs in CodeIgniter, codeigniter recording log_PHP tutorial

Comprehensive summary of CodeIgniter's method of recording error logs, codeigniter's log recording

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

CI workflow:

All entrances are entered from index.php in the root directory. After determining the directory where the application is located, load the codeigniter/CodeIgniter.php file, which will load the following files in sequence to execute the entire process.

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

codeigniter.php: Loads 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 public functions such as detecting PHP version, file permissions, loading core classes, obtaining configuration parameters, loading exception/error classes, and obtaining http request status

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: Provides methods for managing configuration files and detects application/config/config.php parameters

application/config/config.php:Configure global parameters

system/core/URI.php: Parsing url parameters

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

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

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

system/core/Long.php: Initialize prompt language variable

system/core/controller.php:Control output class

Record error log:

The default program does not record error logs, you can set it if necessary: ​​

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

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

If it is 0, it means no error log will be output. Please refer to the introduction inside for details;

2. Call the global function log_message('level','message') on the page where errors need to be written. There are three levels. One is error, which is PHP running error. The second is debug, system debugging. CI itself has many functions. The page also adds its own system debug. The third 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. In general In order to hide the log content, this address must be moved. 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 process variables for use in other places, such as customized sessions, etc. This work is also very easy in CI.

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 the $this->config->load('settingfile') function where you need to call custom global variables, such as:

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

If necessary, you can also set it to automatic loading through application/config/autoload.php.

3. Next use

on the same page
$this->config->item('varname')

Function, 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 regards the entire system as a large class, and then obtains the corresponding information through loading, inheritance, etc. method.
For more custom variable reference: http://codeigniter.org.cn/user_guide/libraries/config.html

Hide index.php and load external files:

In fact, whether you are using CI or ZF, you have the same problem, which is the path problem. In the early days, when I was using ZF as a CMS, I set in the .htaccess file that resource files such as js, css, img, etc. would not be redirected. But when I was using CI today, I forgot about it and couldn't get it right after a long time. I logged into CI's official Chinese website and finally solved the problem with the help of forum experts. I posted it here for everyone to share.

首先,隐藏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

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

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

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

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

echo $css;

2、模型与视图的交互

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

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

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127869.htmlTechArticleCodeIgniter记录错误日志的方法全面总结,codeigniter记录日志 本文实例讲述了CodeIgniter记录错误日志的方法。分享给大家供大家参考,具体如下...
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
PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Debunking the Myths: Is PHP Really a Dead Language?Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AM

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

The PHP vs. Python Debate: Which is Better?The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor