PHP退出的最佳实践() [英] Best Practice for PHP exit()

查看:82
本文介绍了PHP退出的最佳实践()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个PHP项目,我有一个名为login.php的文件。该文件包含一个HTML代码,该代码基本上由一个具有2个输入(用于电子邮件和密码)和一个提交按钮的表单组成。在HTML之前,有一个PHP脚本检查输入是否有效(非空,用户存在于数据库中)。我使用隐藏的输入字段(称为formsubmitted)来检查用户是否按下了提交按钮。如果这些值是有效的,那么PHP脚本会将用户重定向到主页,如果不是,PHP脚本会打印一个HTML div,在该HTML div中存储错误消息,然后显示表单的HTML。

我想只打印错误的div,而不是HTML的其余部分,为此我可以使用exit()函数。现在我不知道这是否是一种好的做法,我已经搜索了网页,但没有发现任何有用的信息。如果有人能告诉我是否可以这样做,我将不胜感激。
谢谢!



以下是我的代码简要介绍:

  if(isset($ _ POST ['formsubmitted'])){
//检查电子邮件和密码是否有效
if(valid){
header(Location:home.php);
} else {
echo< div id ='error-section'>;
回显消息;
回声< / div>
}
}
exit();
?>
<!DOCTYPE html>
< html>
...............
< / html>


解决方案

回答您的问题:我个人不会使用 exit()语句中的任何地方(除非它在REST API echo之后(json_encode(array()) echo声明)。它只会导致很多头痛,并伴随调试错误。



(它也可以打破 \ob _ *( )\ 调用以及大部分框架的显示模块 - 因为框架将无法获得它的echo语句。)



< hr>

My Practice:



我总是有一个 .errors 在我的表单中的div:

 <?php if(isset($ errors)&&!empty($ errors) :?> 
<?php foreach($ errors as $ error):?>
< div class =alert alert-dangerrole =alert>
< strong>错误:< / strong><?php echo $ error ['message']?>
< / div&g t;
<?php endforeach; ?>
<?php endif; ?>

< form>
<!---表格在这里 - >
< / form>



这样,您只需将 $ errors 数组传递到您的表单中, honkey dory,你可以避免使用 exit() - 使用 exit()有它的时间和地点,但是因为它不会给你一条消息或记录任何事情,以后可以进行调试。



作为结束语,此方法允许您将数组传递给这样的错误:

  array(
'first_name'=>'需要名字',
'last_name'=>'需要姓氏',
'zip_code'=>'必须输入有效的邮政编码'

这可以让你在呈现表单时向你的输入字段添加一个 .error 类,将帮助你的用户找到哪个输入有问题 - 总是很好:

 < inpu t id =first_nametype =textclass =my_input_class <?php(isset($ errors)&& isset($ errors ['first_name']))? 'error':''?>> 


I am working on a PHP project and I have a file called login.php. This file contains a HTML code that basically consists of a form that has 2 inputs (used for email and password) and a submit button. Before the HTML there is a PHP script that checks if the inputs are valid (not empty, the user exists in the db). I use an hidden input field (called formsubmitted) to check if the user has pressed the submit button. If the values are valid then the PHP script redirects the user to the home page, if not, the PHP script prints a HTML div in which I store the error message and then the HTML for the form.

I would like to print only the error div, not the rest of the HTML, and for this I could use the exit() function. Now I don't know if this is a good practice, I have searched the web but didn't find any useful. I would appreciate if someone could tell me if it is ok to do so. Thanks!

Here is how my code looks briefly:

<?php
if (isset($_POST['formsubmitted'])) {
    //check if the email and password are valid
    if (valid) {
        header("Location:home.php");
    } else {
        echo "<div id='error-section'>";
        echo "message";
        echo "</div>"
    }
}
exit();
?>
<!DOCTYPE html>
<html>
...............
</html>

解决方案

To answer your question: I personally wouldn't use exit() statements anywhere in your code (unless it's after a REST API echo(json_encode(array()) echo statement.) It just can cause a lot of head aches down the road with debugging errors.

(It also can break \ob_*()\ calls as well as the majority of framework's display modules - because the framework won't be able to get to it's echo statements.)


My Practice:

I always have a .errors div in my forms:

<?php if (isset($errors) && !empty($errors): ?>
     <?php foreach ($errors as $error): ?>
          <div class="alert alert-danger" role="alert">
               <strong>Error: </strong> <?php echo $error['message'] ?>
          </div>
     <?php endforeach; ?>
<?php endif; ?>

<form>
     <!--- form here -->
</form>

This way you just pass the $errors array into your form and everything works honkey dory and you can avoid using exit() - using exit() has it's time and place, but because it doesn't give you a message or log anything it can be a royal pain to debug later on.

As a closing note, this method allows you to pass an array to the errors like so:

array(
     'first_name' => 'First Name Required',
     'last_name' => 'Last Name Required',
     'zip_code' => 'Must enter a valid zip code'
)

Which allows you to, when you're rendering your form add an .error class to your input fields which will help your users locate which input had the problem - always nice:

<input id="first_name" type="text" class="my_input_class<?php (isset($errors) && isset($errors['first_name'])) ? ' error' : '' ?>">

这篇关于PHP退出的最佳实践()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆