PHP:使用exit();或者死();在标头("Location:")之后; [英] PHP: Utilizing exit(); or die(); after header("Location: ");

查看:120
本文介绍了PHP:使用exit();或者死();在标头("Location:")之后;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的用户登录/注册系统

I have a user login/registration system that simply uses

// execute queries, set cookies, etc. here
header("Location: " . getenv("HTTP_REFERER"));

我最近读了一篇关于exit();die();的文章,却不知道我应该使用它们.据我了解,它们使PHP终止了吗?那是对的吗?我可以解决此问题的最佳方法是,直接在everHeader()之后直接添加其中一个函数即可;我有死刑吗?

I recently read a post about exit(); and die(); and had no idea that I was supposed to be using these. From what I understand, they make it end the PHP? Is that correct? What's the best way I can work toward this, simply adding one of these functions directly after ever header(); execution I have?

我有AJAX,jQuery读取了我的login.php/register.php,这会以任何方式受到影响吗?

I have AJAX, jQuery reading through my login.php/register.php, will this be affect in any way?

除了header();之后,我还应该在其他地方使用exit();die();函数?

Other than after header();, where else should I be usitilizing the exit(); or die(); functions? And is exit(); more used around PHP whereas die(); more used around Perl?

推荐答案

我也一直在寻找答案.我发现的东西:

I have been looking for an answer on this as well. What I found:

如果您未在header('Location: http://something')之后放置die()或exit(),则脚本可能会继续导致意外行为.例如,这可能会导致泄露您实际上想要通过重定向(HTTP 301)阻止的内容.对于最终用户而言,上述内容可能不直接可见,因为浏览器可能无法呈现(由于301).结论,exit()和die()函数可阻止脚本继续运行.

If you don't put a die() or exit() after your header('Location: http://something') your script may continue resulting in unexpected behaviour. This may for example result in content being disclosed that you actually wanted to prevent with the redirect (HTTP 301). The aforementioned may not directly be visible for an end user as the browser may not render it (due to the 301). Conclusion, the exit() and die() functions stop the script from continuing.

我还想知道功能之间的区别,因为它似乎没有任何区别.但是,在PHP中,Header输出有明显的不同. 在下面的示例中,我选择使用其他标头,但是为了显示出exit()和die()之间的区别无关紧要.

I also wanted to know the difference between the functions as it seems there is none. However, in PHP, there is a distinct difference in Header output. In the examples below I chose to use a different header but for sake of showing the difference between exit() and die() that doesn't matter.

<?php
    header('HTTP/1.1 304 Not Modified');
    exit();
?>

结果:

HTTP/1.1 304 Not Modified 
Connection: Keep-Alive 
Keep-Alive: timeout=5, max=100

Die()起作用

<?php
    header('HTTP/1.1 304 Not Modified');
    die();
?>

结果:

HTTP/1.1 304 Not Modified 
Connection: close

差异

因此, die()关闭连接,而 exit()不会关闭连接.是否要保持打开或关闭连接取决于性能.两者都有优点和缺点,并取决于您的特定要求.

Difference

So, die() closes the connection and exit() doesn't. It depends on performance whether or not you want to keep the connection open or close it. Both have advantages and disadvantages and depends on your specific requirement(s).

Wiki上的HTTP持久连接

这篇关于PHP:使用exit();或者死();在标头("Location:")之后;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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