0

我正在尝试将一块经典 ASP (vbScript) 翻译成 PHP。我做了一个诚实的尝试,但我的翻译似乎不正确。有人可以帮帮我吗?

一、vbScript代码:

szTemp = Request.ServerVariables("URL")
aryCrumbs = Split(szTemp,"/")
iMax = Ubound(aryCrumbs)

szCrumbPath = "http://" & Request.ServerVariables("SERVER_NAME")
szTemp = "<a href=""" & szCrumbPath & """ style=""color:#600;"">Home</a>"

For i = 0 To iMax -2
szCrumbPath = szCrumbPath & "/" & aryCrumbs(i)
szTemp = szTemp & "  &gt; <a href=""" & szCrumbPath & """ style=""color:#600;"">" & CleanUp(aryCrumbs(i)) & "</a>"    'Note: The &gt; in this line refers to a server request variable. 
Next

GetCrumbsArticleCategoryLevel = szTemp & "<span style=""color:#600;"">  &gt; " & CleanUp(aryCrumbs(i)) & "</span>"

这是我翻译成 PHP 的尝试:

$szTemp = $_SERVER["PATH_INFO"];    // Get current URL path (doesn't include www)
$aryCrumbs = explode("/",$szTemp);  // Split path name by slashes into an array
$iMax = count($aryCrumbs);          // Count array.
$szCrumbPath = "http://". $_SERVER["HTTP_HOST"];      // Add on http to web server name
$szTemp = '<a href="' . $szCrumbPath . '" style=&quot;color:#600;&quot;>Home</a>'; 

for ($i=0; $i<=($iMax-2); $i++) {

$szCrumbPath = $szCrumbPath . "/" . $aryCrumbs[$i];
$szTemp = $szTemp ." &gt; <a href=&quot;" . $szCrumbPath . "&quot; style=&quot;color:#600;&quot;". ">" . CleanUp($aryCrumbs[$i]) . "</a>";
}

$GetCrumbsArticleCategoryLevel = $szTemp."<span style=&quot;color:#600;&quot;>&gt; ".CleanUp($aryCrumbs[$i])."</span>";
4

1 回答 1

2

在 PHP 中,为了得到一个 " 你需要用 \ 来分隔它,所以 " 变成 \"

例子:

$szTemp = "<a href=\"" . $szCrumbPath . "\" style=\"color: #600\">Home</a>";

翻译

我假设您使用的是 Request.ServerVariables("gt"),在 PHP 中相当于 $_SERVER,否则对于 Request.Form 使用 $_POST 或 $_GET 作为 Request.QueryString。

确保用户是否可以更改您使用 htmlspecialchars() 函数进行 html 编码的值,否则您会为跨站点脚本攻击 [XSS] 留有余地

$szTemp = $_SERVER['REQUEST_URI'];
$aryCrumbs = explode("/", $szTemp);
$iMax = count($aryCrumbs);

$szCrumbPath = "http://". $_SERVER["HTTP_HOST"];
$szTemp = "<a href=\"" . $szCrumbPath . "\" style=\"color: #600\">Home</a>";

for ($i=0; $i <= ($iMax - 2); $i++) {
    $szCrumbPath = $szCrumbPath . "/" . $aryCrumbs[$i];
    $szTemp = $szTemp . " &gt; <a href=\"" . $szCrumbPath . "\" style=\"color: #600;\">" . CleanUp($aryCrumbs[$i]) . "</a>"; //The htmlspecialchars prevents a XSS attack
}
$GetCrumbsArticleCategoryLevel = $szTemp . "<span style=\"color:#600\"> &gt; " . CleanUp($aryCrumbs[$i]) . "</span>";
于 2011-06-27T06:20:15.243 回答