允许重定向到不在 htdocs 中的 URL (XAMPP)

Posted

技术标签:

【中文标题】允许重定向到不在 htdocs 中的 URL (XAMPP)【英文标题】:Allow redirect to URL not within htdocs (XAMPP) 【发布时间】:2020-08-02 09:16:40 【问题描述】:

真的很难让我的表单允许用户根据他们选择的单选按钮访问 URL。当我在我的服务器上完成表单时,它会按预期重定向到专用 URL,但是它说找不到对象并且还说“在此服务器上找不到请求的 URL”。我真的不明白如何将文件保存到网站 URL 的 htdocs 文件夹中。用户可能被重定向到的网站选项如下:

http://www.nisra.gov.uk/demography/default.asp28.htm

http://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/names/babies-first-names

http://www.ons.gov.uk/ons/rel/vsob1/baby-names--england-and-wales/index.html

以下是我的代码:

php

<?php

if (isset($_GET['location']))
  

$location = $_GET["location"];

if ($location === "S")

header("Location:  http://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/names/babies-first-names");

   else if ($location === "EW")


header("Location: http://www.ons.gov.uk/ons/rel/vsob1/baby-names--england-and-wales/index.html");

   else if ($location === "NI")


header("Location: http://www.nisra.gov.uk/demography/default.asp28.htm");





?> 

HTML:

<!DOCTYPE html>
<head>
  <title>Baby Name Popularity Tables</title>
</head>
<body>
<h1>Baby Name Popularity Tables</h1>
Please select either England and Wales, Scotland or Northern Ireland.
<form action="" method="get">
<table>
<tr> 
<td><input type="radio" name="location" value="EW"/></td>
<td>England &amp; Wales</td>
</tr>
<tr>
<td><input type="radio" name="location" value="S"/></td>
<td>Scotland</td>
</tr>
<tr>
<td><input type="radio" name="location" value="NI"/></td>
<td>Northern Ireland</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Get Baby Names"/></td>
</tr>
</table>
</form>     
</body>
</html>

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

好的,首先,你应该重构你的代码,使其更加干净和精确,为此,我为你做了,但下次尝试改进:)

redirectForm.php

<?php

$websites = [
    "S" => "http://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/names/babies-first-names",
    "EW" => "http://www.ons.gov.uk/ons/rel/vsob1/baby-names--england-and-wales/index.html",
    "NI" => "http://www.nisra.gov.uk/demography/default.asp28.htm",
    // "ANOTHER_PLACE" => "WEBSITE",
    // "ANOTHER_PLACE" => "WEBSITE",
];

$location = isset($_GET['location']) ? $_GET['location'] : false;

foreach ($websites as $key => $value) 
    if ($key === $location) 
        header("Location: ".$value);
    

HTML(注意我在表单操作中所做的,第 8 行):

<!DOCTYPE html>
<head>
  <title>Baby Name Popularity Tables</title>
</head>
<body>
<h1>Baby Name Popularity Tables</h1>
Please select either England and Wales, Scotland or Northern Ireland.
<form action="redirectForm.php" method="get">
<table>
<tr> 
<td><input type="radio" name="location" value="EW"/></td>
<td>England &amp; Wales</td>
</tr>
<tr>
<td><input type="radio" name="location" value="S"/></td>
<td>Scotland</td>
</tr>
<tr>
<td><input type="radio" name="location" value="NI"/></td>
<td>Northern Ireland</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Get Baby Names"/></td>
</tr>
</table>
</form>     
</body>
</html>



【讨论】:

您好,感谢您的回复,但这也不起作用。在成功将用户重定向到专用 URL 但找不到对象的情况下,它的作用完全相同。不确定是什么导致了问题 tbh

以上是关于允许重定向到不在 htdocs 中的 URL (XAMPP)的主要内容,如果未能解决你的问题,请参考以下文章