这段 PHP 代码到底做了什么?

发布于 2024-08-30 10:12:59 字数 815 浏览 11 评论 0原文

好吧,我的朋友给了我这段代码,用于请求标头并将它们与标头应该是什么进行比较。它工作完美,但我不知道为什么。这是代码:

    $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.

我用与以下问题相关的一些数字进行了评论:

  1. 这里到底发生了什么?是否将 $headers 设置为 $header AND $value?

  2. 再说一次,我不知道这里发生了什么。

  3. 为什么将变量设置为不同的变量?这是唯一使用该变量的区域,那么是否有理由将其设置为其他值?

  4. 与 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:

  1. What exactly is happening here? Is it setting the $headers as $header AND $value?

  2. Again, don't have any idea what is going on here.

  3. 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?

  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一片旧的回忆 2024-09-06 10:12:59
 $headers = apache_request_headers(); 

获取标头数组。

    $customheader = "Header: 7ddb6ffab28bb675215a7d6e31cfc759"; 

定义了它将搜索的“自定义标头”。

    foreach ($headers as $header => $value) { // 1
        $custom .= "$header: $value"; // 2
    }

循环并创建一个 $custom 变量来保存扩展的 $key=>$value 标头。

    $mystring = $custom;  // 3
    $findme   = $customheader; // 4
    $pos = strpos($mystring, $findme); 

在展开的字符串中查找 $customheader

    if ($pos !== false) {
// Do something
} else{ exit(); } //If it doesn't match, exit.

实际上不需要重新分配变量。本质上,它获取标头数组并将其转换为一个大字符串,然后搜索该字符串以查看 $customheader 文本是否存在。

 $headers = apache_request_headers(); 

Gets the header array.

    $customheader = "Header: 7ddb6ffab28bb675215a7d6e31cfc759"; 

Defined a "customheader" it will search for.

    foreach ($headers as $header => $value) { // 1
        $custom .= "$header: $value"; // 2
    }

Loop through and create a $custom variable to hold the expanded $key=>$value header.

    $mystring = $custom;  // 3
    $findme   = $customheader; // 4
    $pos = strpos($mystring, $findme); 

Look for the $customheader in the expanded string.

    if ($pos !== false) {
// Do something
} else{ exit(); } //If it doesn't match, exit.

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.

妳是的陽光 2024-09-06 10:12:59
  1. 它迭代 $headers,将每个元素的键分配给 $header,将值分配给 $value。因此,在块内,我们在单独的变量中获取标头的名称及其值。
  2. 在此步骤中,我们使用点运算符将所有标头连接在一个字符串中。本质上,我们将标头从数组转换为字符串。
  3. 除非这些变量在其他地方使用,否则没有理由重新分配。

免责声明:我是红宝石人,所以如果我错了,请纠正我。

  1. It's iterating over $headers, assigning the key of each element to $header and the value to $value. So, inside the block, we get the name of the header and its value in separate variables.
  2. On this step we concatenate all headers in a single string using the dot operator. Essentially, we're converting the headers from an array into a string.
  3. Unless these variables are used elsewhere, there's no reason for reassignment.

disclaimer: I'm a ruby person, so please correct me if I'm wrong.

征棹 2024-09-06 10:12:59

apache_request_headers() 返回所有的关联数组当前请求中的 >HTTP 标头,如果失败则返回 false。因此,最好将返回值检查为:

$headers = apache_request_headers(); 
if(! $headers) {
 die("Error fetching headers");
}

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 returns false if it fails. So its good to check the return value as:

$headers = apache_request_headers(); 
if(! $headers) {
 die("Error fetching headers");
}

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 returns false 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文