使用PHP的错误处理(转) 没话好说了。。都到这份上了。。出了错没找我。。找它就得了。
错误|错误处理
使用PHP的错误处理
在web 上所有常见的错误之一就是无效的链接。一旦从其它站点上出现了无效的链接,你会重新安排你
的站点。人们会将他们喜欢的站点存成书签,如果在三个月后再次访问时,仅仅发现的是'404 Not Fount '
时,将不会给他们任务帮助,告诉他们如何从你的站点去查找原始的信息。让我们解决这个问题,或者至少
给你的用户一个友好的帮助,一旦他们遇到'a 404' 错误时,能够得到一些痕迹。你可以创建普通的页面来
报告在处理你的页面时所遇到的所有的错误。
PHP 与Apache一起可以很自由地让你创建自已的出错页面,但是需要重新进行配置,并且要编少量的代
码。先让我们学习配置部分。
Apache的ErrorDocument指示用来指出在出现错误时Apache应重定向到哪一个文档(或URI)。它允许你
为每一个你的用户可能遇到的错误代码指定一个资源。通过在你的服务器配置中增加一个ErrorDocument 404
/error.php指示。这个将在用户访问一个不存在的页面时,重定向到'error.php'中,下面我们就会写出
'error.php'页面。不要忘了重新启动Apache以使改动生效。
接着,我们写出一个简单的error.php:
你所请求的文件 (=$REDIRECT_URL?>) 在这个服务器上不存在。
请查找你想要的文件从 前页。
现在试着读取一个在你服务器上不存在的页面,怎么样,你可以看到error.php了,它有着一个良好和
友好的消息,并且还有一个到前页的链接。
让我们把它扩展一下。正如你所见,我在error.php中使用了REDIRECT_URL变量。这个变量是Apache在
执行了一个ErrorDocument指示时所设置的,并且给出了一种可能来找到原始的资源。在这种情况下,Apache
还设置了一些别的变量,所有的变量可以在这里找到。使用这些变量可能创建一个很好的出错页面,用于给
用户一个不错与友好的出错页面,而代替Apache给出的缺省页面。
从PHP页面中输出错误
从一个PHP页面输出错误与模拟Apache对ErrorDocument指示所做的很象,你只要简单地将用户重定向,
通过使用query-string变量,而Apache则通常是设置在环境变量里面。这样就可以使用同一个出错页面来处
理各种错误。下面是一个例子:
--------------------------------------------------------------------------------
function throw_error($message) {
$error_page = "/err/error.php";
$error_url = $error_page;
$error_url .= "?REDIRECT_ERROR_NOTES=$message";
$error_url .= "&REDIRECT_URL=" . $GLOBALS["PHP_SELF"];
$error_url .= "&REDIRECT_REQUEST_METHOD=$REQUEST_METHOD";
$error_url .= "&REDIRECT_STATUS=501";
Header("Status: 501");
Header("Location: $error_url");
exit;
}
ob_start();
// 使用输出缓冲以便在这页中的任何地方输出错误
if(!condition) {
throw_error("the condition failed");
}
ob_end_flush();
// 页面处理完毕,刷新输出缓冲
?>--------------------------------------------------------------------------------
使用PHP4的输出缓冲特性对生成一般的出错报告功能也会有帮助。但是在你确认整个出错页面处理完毕
时,不要忘记刷新缓冲区,你可以在你的代码中的任可地方通过Header调用来进行重定向。
读者可以自行设计,实现自已的出错页面来适应他/她的站点。不要忘了,你可以包含一个带email的提
交表单在出错页面中,这样用户可以反馈给你一些意见。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

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.

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.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
