这段 PHP 代码到底做了什么?
好吧,我的朋友给了我这段代码,用于请求标头并将它们与标头应该是什么进行比较。它工作完美,但我不知道为什么。这是代码:
$headers = apache_request_headers();
$customheader = "Header: 7ddb6ffab28bb675215a7d6e31cfc759";
foreach ($headers as $header => $value) { // 1
$custom .= "$header: $value"; // 2
}
$mystring = $custom; // 3
$findme = $customheader; // 4
$pos = strpos($mystring, $findme);
if ($pos !== false) {
// Do something
} else{ exit(); } //If it doesn't match, exit.
我用与以下问题相关的一些数字进行了评论:
这里到底发生了什么?是否将 $headers 设置为 $header AND $value?
再说一次,我不知道这里发生了什么。
为什么将变量设置为不同的变量?这是唯一使用该变量的区域,那么是否有理由将其设置为其他值?
与 3 相同的问题。
如果这是一个糟糕的问题,我很抱歉,但它一直困扰着我,我真的很想知道它为什么有效。好吧,我明白为什么它有效,我想我只是想更具体地了解。感谢您提供的任何见解。
Alright, my friend gave me this code for requesting headers and comparing them to what the header should be. It works perfectly, but I'm not sure why. Here is the code:
$headers = apache_request_headers();
$customheader = "Header: 7ddb6ffab28bb675215a7d6e31cfc759";
foreach ($headers as $header => $value) { // 1
$custom .= "$header: $value"; // 2
}
$mystring = $custom; // 3
$findme = $customheader; // 4
$pos = strpos($mystring, $findme);
if ($pos !== false) {
// Do something
} else{ exit(); } //If it doesn't match, exit.
I commented with some numbers relating to the following questions:
What exactly is happening here? Is it setting the $headers as $header AND $value?
Again, don't have any idea what is going on here.
Why set the variable to a different variable? This is the only area where the variable is getting used, so is there a reason to set it to something else?
Same question as 3.
I'm sorry if this is a terrible question, but its been bothering me, and I really want to know WHY it works. Well, I understand why it works, I guess I just want to know more specifically. Thanks for any insight you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获取标头数组。
定义了它将搜索的“自定义标头”。
循环并创建一个
$custom
变量来保存扩展的$key=>$value
标头。在展开的字符串中查找
$customheader
。实际上不需要重新分配变量。本质上,它获取标头数组并将其转换为一个大字符串,然后搜索该字符串以查看
$customheader
文本是否存在。Gets the header array.
Defined a "customheader" it will search for.
Loop through and create a
$custom
variable to hold the expanded$key=>$value
header.Look for the
$customheader
in the expanded string.There really isn't a need for the reassignment of variables. In essence it's taking the array of headers and turning it into one big string which it then searches through to see if the
$customheader
text exists.免责声明:我是红宝石人,所以如果我错了,请纠正我。
disclaimer: I'm a ruby person, so please correct me if I'm wrong.
apache_request_headers() 返回所有
的关联数组当前请求中的 >HTTP
标头,如果失败则返回false
。因此,最好将返回值检查为:1:您正在迭代获得的关联数组。
2:在数组中形成一串粘合的键值对,键和值之间用冒号分隔。
3 和 4 只是将一个变量分配给另一个变量。您可以直接使用:
$pos = strpos($custom, $customheader);
代替步骤 3 和 4。strpos
返回false
如果它在$custom
中找不到您的$customheader
,则返回找到的位置。总的来说,此代码片段检查您的自定义标头是否存在于
apache_request_headers
返回的标头中。apache_request_headers() returns an associative array of all the
HTTP
headers in the current request and returnsfalse
if it fails. So its good to check the return value as:1: You are iterating your the associative array you got.
2: form a string of glued key value pairs in the array, with the key and value separated by a colon.
3 and 4 are just assigning one variable to another. You could have directly used:
$pos = strpos($custom, $customheader);
in place of steps 3 and 4.strpos
returnsfalse
if it cannot find your$customheader
in$custom
else it returns the found position.Overall this snippet checks if your custom header is present in the headers returned by
apache_request_headers
.