PHP很难理解这个功能

时间:2013-01-30 15:37:33

标签: php function user-defined-functions

我有这个函数由另一个程序员编写,但他很久以前就退出了。

我试图理解他写的这个功能,但我并不完全理解它。

来自 csv文件令牌,数量应取决于我选择的数量。但 force_plaform 变量的作用是什么? 那么 $ _ GET x参数和 $ _ COOKIE x参数呢?

// Get a purchaselink from a given token (if one exists) and a given csv
function getPurchaseLink($token, $csvfile, $force_platform = "", $quantity = 1)
{
$fp = fopen($csvfile, 'r', true);
$columns = fgetcsv($fp, 1024, ',');

while (($row = fgetcsv($fp, 1024, ","))) {
    $row = array_combine($columns, $row);
    if ($row['purchaseToken'] == $token) {
        $purchaseLink = $row;
        break;
    }
}

// Default to pucharse link
$url = http://www.purchase.com//?p1=zzM5X9k4dF%2BDzrTnixoaKw3Fg7wZ8h5pkjp%2BLEidTXBg40xOjcFj5PtPlQniMskLm0W3ti65mE2KafUifZe9dZAcE&ref=purchase.com&style=146&ga=UA-2689090-1&C1=C01507;1;0&C2=C0508;0;0;
if ($url) {     
  // Convert GET x-parameters to cmp, mkey1 and mkey2 if they are present
  $append = "";
  if((isset($_GET["x-source"])) && ($_GET["x-source"] != "")) {
    $append = "&cmp=source_" . $_GET["x-source"];
    $nextParameter = "x-" . $_GET["x-source"];
    $counter = 1;
    $searching = TRUE;
    while(($searching) && ($counter <= 2)) {
      if((!isset($_GET[$nextParameter])) || ($_GET[$nextParameter] == "")) {
        $searching = FALSE;
      }
      else {
        $append .= "&mkey" . $counter . "=" . str_replace("x-", "", $nextParameter) . "_" . $_GET[$nextParameter];
        $nextParameter = "x-" . $_GET[$nextParameter];
    $counter += 1;
      }
    }
  }
  // Convert COOKIE x-parameters to cmp, mkey1 and mkey2 if they are present and there are no GET x-parameters
  if(($append == "") && (isset($_COOKIE["x-source"])) && ($_COOKIE["x-source"] != "")) {
    $append = "&cmp=source_" . $_COOKIE["x-source"];
    $nextParameter = "x-" . $_COOKIE["x-source"];
    $counter = 1;
    $searching = TRUE;
    while(($searching) && ($counter <= 2)) {
      if((!isset($_COOKIE[$nextParameter])) || ($_COOKIE[$nextParameter] == "")) {
        $searching = FALSE;
      }
      else {
        $append .= "&mkey" . $counter . "=" . str_replace("x-", "", $nextParameter) . "_" . $_COOKIE[$nextParameter];
        $nextParameter = "x-" . $_COOKIE[$nextParameter];
    $counter += 1;
      }
    }
  }
  // Return upclick purchase link   
  return $url . $append;
}

if ($quantity != 1) {
    $url .= "&quantity=" . $quantity;
}

// Pass on tracking parameters from URI query and cookies
$append = "";
foreach($_GET as $parameter => $value) {
    if(strpos($parameter, "x-") === 0 || $parameter == "tracking" || $parameter == "serial" || $parameter == "affiliate") {
        $append .= "&" . $parameter . "=" . $value;
    }
}
if (empty($append)) {
    foreach($_COOKIE as $parameter => $value) {
        if(strpos($parameter, "x-") === 0) {
            $append .= "&" . $parameter . "=" . $value;
        }
    }
}

$append = str_replace("x-tracking", "tracking", $append);
$append = str_replace("x-affiliate", "affiliate", $append);
$append = str_replace("&serial=", "&x-serial=", $append);

return $url . $append;
 }

1 个答案:

答案 0 :(得分:0)

该函数正在构建一个查询字符串(URL)并将其返回。程序在调用该URL时所执行的操作不符合此代码,而是调用该页面上的代码。此代码只生成一个URL。对于force_plaform,它似乎根本没有在函数中使用(仅在函数头中)。这是一个未使用的变量。对于$_GET$_COOKIE变量,代码只是检查它们是否存在,然后将它们附加到查询字符串中。如果是这样的话。