pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <tfoot id='eGjyc'></tfoot>

    <legend id='eGjyc'><style id='eGjyc'><dir id='eGjyc'><q id='eGjyc'></q></dir></style></legend>

    <i id='eGjyc'><tr id='eGjyc'><dt id='eGjyc'><q id='eGjyc'><span id='eGjyc'><b id='eGjyc'><form id='eGjyc'><ins id='eGjyc'></ins><ul id='eGjyc'></ul><sub id='eGjyc'></sub></form><legend id='eGjyc'></legend><bdo id='eGjyc'><pre id='eGjyc'><center id='eGjyc'></center></pre></bdo></b><th id='eGjyc'></th></span></q></dt></tr></i><div class="p8ov3ax" id='eGjyc'><tfoot id='eGjyc'></tfoot><dl id='eGjyc'><fieldset id='eGjyc'></fieldset></dl></div>
  • <small id='eGjyc'></small><noframes id='eGjyc'>

    • <bdo id='eGjyc'></bdo><ul id='eGjyc'></ul>

        使用 PDO 處理錯(cuò)誤的最佳實(shí)踐

        Best practice for error handling using PDO(使用 PDO 處理錯(cuò)誤的最佳實(shí)踐)

        1. <tfoot id='afaAC'></tfoot>

            <legend id='afaAC'><style id='afaAC'><dir id='afaAC'><q id='afaAC'></q></dir></style></legend>

            <small id='afaAC'></small><noframes id='afaAC'>

              <tbody id='afaAC'></tbody>
          • <i id='afaAC'><tr id='afaAC'><dt id='afaAC'><q id='afaAC'><span id='afaAC'><b id='afaAC'><form id='afaAC'><ins id='afaAC'></ins><ul id='afaAC'></ul><sub id='afaAC'></sub></form><legend id='afaAC'></legend><bdo id='afaAC'><pre id='afaAC'><center id='afaAC'></center></pre></bdo></b><th id='afaAC'></th></span></q></dt></tr></i><div class="hsyku8v" id='afaAC'><tfoot id='afaAC'></tfoot><dl id='afaAC'><fieldset id='afaAC'></fieldset></dl></div>
                  <bdo id='afaAC'></bdo><ul id='afaAC'></ul>
                • 本文介紹了使用 PDO 處理錯(cuò)誤的最佳實(shí)踐的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  問(wèn)題:

                  尋找使用 PDO 進(jìn)行錯(cuò)誤處理的最佳實(shí)踐.我在網(wǎng)站、SO、書(shū)籍等上找到的選項(xiàng)

                  Finding best practice for error handling using PDO. The options I have found on websites, SO, books, etc.

                  1. 很多網(wǎng)站都說(shuō)您應(yīng)該在 catch 塊中回顯錯(cuò)誤消息.
                  2. SO 上的大量用戶表示,由于安全風(fēng)險(xiǎn),您永遠(yuǎn)不應(yīng)回顯錯(cuò)誤消息.
                  3. 其他人建議將其記錄到文檔根目錄之外的日志文件中.
                  4. 有些使用錯(cuò)誤處理將其記錄到 SQL 表中.

                  有多種選項(xiàng),很容易被淹沒(méi)在您應(yīng)該使用的選項(xiàng)中.當(dāng)然,您可以使用 MVC 框架并讓它為您處理錯(cuò)誤日志,但是如果您不使用 MVC,它會(huì)是什么樣子.

                  With a multitude of options, it gets quite easy to drown into what option you should be using. Of course you could use a MVC framework and let it handle error logging for you, but how would it look like if you are not using MVC.

                  據(jù)我所知,開(kāi)發(fā)環(huán)境中的錯(cuò)誤處理應(yīng)該如下所示:

                  As I have understood it error handling should like the following in development environment:

                  display_errors = On
                  display_startup_errors = On
                  error_reporting = -1
                  log_errors = On
                  

                  或者如果無(wú)法訪問(wèn) php.ini 文件:

                  Or if no access is available to the php.ini file:

                  error_reporting(-1);
                  ini_set("display_errors", 1);
                  

                  生產(chǎn)環(huán)境:

                  display_errors = Off
                  display_startup_errors = Off
                  error_reporting = E_ALL
                  log_errors = On
                  

                  或者如果無(wú)法訪問(wèn) php.ini 文件:

                  Or if no access is available to the php.ini file:

                  error_reporting(0);
                  

                  生產(chǎn)環(huán)境中的數(shù)據(jù)庫(kù)連接為例.

                  To take an example of a database connection in production environment.

                  代碼:

                  <?php
                    // Error handling
                    error_reporting(0);
                  
                    // Get credentials from outside document root
                    require_once('../settings.php');
                  
                    // Tests connection to database
                    try {
                      $dbh = new PDO(
                              sprintf(
                                'mysql:host=%s;dbname=%s;port=%s;charset=%s',
                                $settings['host'],
                                $settings['name'],
                                $settings['port'],
                                $settings['charset']
                              ),
                              $settings['username'],
                              $settings['password']
                      );
                      // Prevents emulated prepares and activates error handling
                      // PDO::ERRMODE_EXCEPTION
                      $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    }
                    // Catches errors raised by PDO
                    catch (PDOException $e) {
                      // Prints error messages to file
                      file_put_contents('/home/ubuntu/errors.log', 'Error: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
                      // Shows generic error message to user
                      header('Location: 404.php');
                      exit;
                    }
                  ?>
                  

                  問(wèn)題:

                  • 在 PHP 中一般處理錯(cuò)誤的最佳做法是什么?
                  • 在 catch 塊中處理錯(cuò)誤的最佳做法是什么?

                  推薦答案

                  這是一個(gè)很好的問(wèn)題,但一開(kāi)始就有一個(gè)錯(cuò)誤的前提:您將 PDO 的錯(cuò)誤報(bào)告與站點(diǎn)范圍的錯(cuò)誤報(bào)告分開(kāi)了.這毫無(wú)意義:PDO 錯(cuò)誤在各個(gè)方面都與其他錯(cuò)誤相同 - 文件系統(tǒng)錯(cuò)誤、HTTP 錯(cuò)誤等等.因此,沒(méi)有理由建立僅限 PDO 的錯(cuò)誤報(bào)告.您只需要正確設(shè)置站點(diǎn)范圍的錯(cuò)誤報(bào)告.

                  That's a very good question, but there is one wrong premise at the very beginning: you are taking error reporting for PDO separated from site-wide error reporting. Which makes very little sense: PDO errors in every way are the same as other errors - filesystem errors, HTTP errors, and so on. Thus, there is no reason in establishing PDO-only error reporting. All you need is to properly set site-wide error reporting.

                  還有一個(gè)關(guān)于 php.ini 不可訪問(wèn)性的錯(cuò)誤假設(shè):您始終可以使用 ini_set() 函數(shù)設(shè)置任何配置指令.因此,將 error_reporting 設(shè)置為災(zāi)難性級(jí)別 0 的原因不止于此.

                  There is also one wrong assumption regarding php.ini inaccessibility: you can always set any configuration directive using ini_set() function. Thus, here is not a single reason in setting error_reporting to disastrous level of 0.

                  要回答您的其余問(wèn)題,您只需要一點(diǎn)常識(shí)即可.

                  To answer the rest of your questions all you need is a little common sense.

                  很多網(wǎng)站都說(shuō)您應(yīng)該在 catch 塊中回顯錯(cuò)誤消息.SO 上的大量用戶表示,由于安全風(fēng)險(xiǎn),您永遠(yuǎn)不應(yīng)該回顯錯(cuò)誤消息.

                  A great number of websites say you should echo your error messages in your catch block. A large number of users on SO say that you should never echo error messages due to security risks.

                  你自己怎么看?向用戶顯示系統(tǒng)錯(cuò)誤消息有什么好處嗎?向惡意用戶展示系統(tǒng)內(nèi)部結(jié)構(gòu)有什么好處嗎?

                  What you think yourself? Does it any good showing system error messages to user? Does it any good showing system internals to a malicious user?

                  其他人建議將其記錄到文檔根目錄之外的日志文件中.

                  Others are recommending logging it to a log file outside the document root.

                  您對(duì)此有異議嗎?

                  有些使用錯(cuò)誤處理將其記錄到 SQL 表中.

                  Some use error handling to log it to a SQL table.

                  您不認(rèn)為將數(shù)據(jù)庫(kù)錯(cuò)誤記錄到數(shù)據(jù)庫(kù)中的想法很矛盾嗎?

                  Don't you think it's quite contradicting idea - to log database errors into database?

                  在 PHP 中一般處理錯(cuò)誤的最佳做法是什么?

                  What is the best practice for handling errors in general in PHP?

                  您已經(jīng)顯示了它:在開(kāi)發(fā)中顯示并在生產(chǎn)中登錄.所有這些都通過(guò)幾個(gè)簡(jiǎn)單的配置選項(xiàng)在站點(diǎn)范圍內(nèi)進(jìn)行控制.

                  You have shown it already: display in dev and log in prod. All is controlled site-wide through few simple configuration options.

                  在 catch 塊中處理錯(cuò)誤的最佳做法是什么?

                  What is the best practice for handling errors in the catch-block?

                  根本不要使用 try-catch 塊來(lái)報(bào)告錯(cuò)誤.您不會(huì)為應(yīng)用中的每個(gè)查詢編寫(xiě)帶有友好錯(cuò)誤消息的 catch 塊strong>,正如另一個(gè)答案中所建議的那樣,是嗎?

                  NOT to use try-catch block for error reporting at all. You aren't going to write a catch block with a friendly error message for the every query in your app, as it's suggested in the other answer, are you?

                  因此你的代碼必須是

                  <?php
                    // Error handling
                    error_reporting(-1);
                    ini_set('display_errors',0);
                    ini_set('log_errors',1);
                  
                    // Get credentials from outside document root
                    require_once('../settings.php');
                  
                    // Tests connection to database
                      $dbh = new PDO(
                              sprintf(
                                'mysql:host=%s;dbname=%s;port=%s;charset=%s',
                                $settings['host'],
                                $settings['name'],
                                $settings['port'],
                                $settings['charset']
                              ),
                              $settings['username'],
                              $settings['password']
                      );
                      // Prevents emulated prepares and activates error handling
                      // PDO::ERRMODE_EXCEPTION
                      $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                  

                  現(xiàn)在回答您在評(píng)論中提出的問(wèn)題.

                  Now to the question you voiced in the comment.

                  自定義錯(cuò)誤屏幕是另一回事,您的代碼尤其糟糕.它既不應(yīng)該是 404 錯(cuò)誤,也不應(yīng)該使用 HTTP 重定向(這對(duì) SEO 來(lái)說(shuō)非常糟糕).

                  A custom error screen is a very different matter and your code is especially bad with it. Neither it should be a 404 error nor an HTTP redirect have to be used (thats very bad for SEO).

                  要?jiǎng)?chuàng)建自定義錯(cuò)誤頁(yè)面,您必須使用您的網(wǎng)絡(luò)服務(wù)器功能(首選)或 PHP 腳本中的錯(cuò)誤處理程序.

                  To create a custom error page you have to use either your web-server features (preferred) or an error handler in PHP script.

                  當(dāng)遇到致命錯(cuò)誤(未捕獲異常就是其中之一)時(shí),PHP 響應(yīng)的不是 200 OK HTTP 狀態(tài)而是 5xx 狀態(tài).每個(gè)網(wǎng)絡(luò)服務(wù)器都可以捕獲此狀態(tài)并顯示相應(yīng)的錯(cuò)誤頁(yè)面.例如.對(duì)于 Apache,它將是

                  When encountering a fatal error (and uncaught exception is one), PHP responds not with 200 OK HTTP status but with 5xx status. And every web-server can catch this status and show an according error page. E.g. for Apache it would be

                  ErrorDocument 503 server_error.html
                  

                  你可以寫(xiě)任何你想要的借口.

                  where you can write whatever excuses you want.

                  或者你可以在 PHP 中設(shè)置一個(gè)自定義的錯(cuò)誤處理程序來(lái)處理所有的 PHP 錯(cuò)誤,一個(gè)例子可以在我寫(xiě)的關(guān)于這個(gè)問(wèn)題的文章中看到:try..catch 的(im)正確使用.

                  Or you can set up a custom error handler in PHP which would handle all PHP errors as well, an example can be seen in the article I wrote on the matter: The (im)proper use of try..catch.

                  這篇關(guān)于使用 PDO 處理錯(cuò)誤的最佳實(shí)踐的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語(yǔ)句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無(wú)法識(shí)別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問(wèn)被拒絕)

                          <bdo id='ZtWEu'></bdo><ul id='ZtWEu'></ul>

                          <small id='ZtWEu'></small><noframes id='ZtWEu'>

                              <tbody id='ZtWEu'></tbody>
                          1. <i id='ZtWEu'><tr id='ZtWEu'><dt id='ZtWEu'><q id='ZtWEu'><span id='ZtWEu'><b id='ZtWEu'><form id='ZtWEu'><ins id='ZtWEu'></ins><ul id='ZtWEu'></ul><sub id='ZtWEu'></sub></form><legend id='ZtWEu'></legend><bdo id='ZtWEu'><pre id='ZtWEu'><center id='ZtWEu'></center></pre></bdo></b><th id='ZtWEu'></th></span></q></dt></tr></i><div class="bxezfzf" id='ZtWEu'><tfoot id='ZtWEu'></tfoot><dl id='ZtWEu'><fieldset id='ZtWEu'></fieldset></dl></div>
                            <legend id='ZtWEu'><style id='ZtWEu'><dir id='ZtWEu'><q id='ZtWEu'></q></dir></style></legend>
                          2. <tfoot id='ZtWEu'></tfoot>
                            主站蜘蛛池模板: 中开泵,中开泵厂家,双吸中开泵-山东博二泵业有限公司 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 合金耐磨锤头_破碎机锤头_郑州市德勤建材有限公司 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 天助网 - 中小企业全网推广平台_生态整合营销知名服务商_天助网采购优选 | 制样机-密封锤式破碎机-粉碎机-智能马弗炉-南昌科鑫制样 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 浇钢砖,流钢砖_厂家价低-淄博恒森耐火材料有限公司 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 户外环保不锈钢垃圾桶_标识标牌制作_园林公园椅厂家_花箱定制-北京汇众环艺 | 样品瓶(色谱样品瓶)百科-浙江哈迈科技有限公司 | 上海盐水喷雾试验机_两厢式冷热冲击试验箱-巨怡环试 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 福建成考网-福建成人高考网| 技德应用| 薄壁轴承-等截面薄壁轴承生产厂家-洛阳薄壁精密轴承有限公司 | 智慧食堂_食堂管理系统_食堂订餐_食堂消费系统—客易捷 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 27PR跨境电商导航 | 专注外贸跨境电商 | 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 合肥钣金加工-安徽激光切割加工-机箱机柜加工厂家-合肥通快 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 临朐空调移机_空调维修「空调回收」临朐二手空调 | 硫化罐-胶管硫化罐-山东鑫泰鑫智能装备有限公司 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 天津电机维修|水泵维修-天津晟佳机电设备有限公司 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 扒渣机厂家_扒渣机价格_矿用扒渣机_铣挖机_撬毛台车_襄阳永力通扒渣机公司 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 睿婕轻钢别墅_钢结构别墅_厂家设计施工报价| 微波萃取合成仪-电热消解器价格-北京安合美诚科学仪器有限公司 | 贵州水玻璃_-贵阳花溪闽兴水玻璃厂 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 |