PHP在URL中显示%20的空格?

时间:2014-01-25 17:56:46

标签: php url escaping

这是一个有点奇怪的问题,我以前从未遇到过这个问题,但我们在这里:

我创建了一个简单的PHP / MYSQL商店。一切正常等等

但是当我添加产品并且类别名称中有space OR / OR -时,当我点击前端的类别名称时,它不会显示该类别中的产品,当我查看地址栏中的URL时,它将显示如下:

category_list.php?category=tooth-white%20range 

您可以看到它已将tooth-white range转换为tooth-white%20range

如果我写了像toothwhiterange这样的类别名称,它将会正常工作!!

以下是我如何显示类别:

<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "config/connect.php"; 
$dynamicList2 = "";
$sql = "SELECT DISTINCT category FROM products";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $category = $row["category"];
             $dynamicList2 .= '<li>
       <a href="category_list.php?category=' . $category . '">' . $category . '</a>
      </li>';
    }   
} else {
    $dynamicList2 = "NO Cats Yet!";
}
?>

有人可以帮助我吗?

由于

编辑:

抱歉,我真的不知道怎么解释这个,但我们又来了:

问题是当我在space类别的名称中添加name of the category时,该类别的名称将显示在前端,但是当我点击它时,不会有任何类型该类别下的产品,即使数据库中属于该类别的某些产品。

但是,当我从类别名称中移除空格或任何特殊字符(例如-/)并将其写为nameofthecategory时,它就可以正常运行并显示其下的产品类别!

第二次编辑:

以下是我根据所选类别获取/显示每个类别下的产品的方法:

<?php 
if (isset($_GET['category'])) {
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "config/connect.php"; 

$category = preg_replace('~[^a-zA-Z0-9]+~', '', $_GET['category']);

$cList2 = "";
$sql = "SELECT * FROM products WHERE category='".$category."'  LIMIT 35" ;
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $category = $row["category"];
             $stock = $row["stock"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $cList2 .= '<table style="float:left; margin-top:10px;" width="25%" border="0" cellspacing="0" cellpadding="0">
      <tr>
    <td align="center"><a href="product.php?id=' . $id . '"><img  style="border:solid 1px #999;" src="inventory_images/' . $id . 'Image1.jpg" width="124" height="124" /></a></td>
  </tr>
  <tr>
    <td id="nameHolder" class="nameHolder" style="text-align:left; padding-left:5px;" rowspan="" height="34"><a href="product.php?id=' . $id . '">' . $product_name . '</a></td>
  </tr>
  <tr>
    <td style="text-align:left; padding-left:5px; color:#C33; font-size:17px;" height="30"><strong style="color:#999;">Price:</strong> £' . $price . '</td>
  </tr>
  <tr>
    <td style="text-align:left; padding-left:5px; color:#C33; font-size:17px;" height="30">' . $stock . '</td>
  </tr>
  <tr>
    <td style="text-align:left; padding-left:5px;" height="15"><a href="product.php?id=' . $id . '">view</a></td>
  </tr>
  <tr>
    <td height="50" align="left"><form method="post" action="product.php?id=' . $id . '"><input type="submit" class="button" name="button" id="button" value="Add to cart" /></form></td>
  </tr>
</table>';
    }
}
} else {
    $cList2 = "We have no products listed in our store yet";
}

?>

3 个答案:

答案 0 :(得分:1)

添加%20是正常的,因为空格不是真正的url友好且在文件命名约定中不鼓励,20是空格char的十六进制(base16)表示。

如果它影响您的代码,您可以随时使用urldecode()将其转换回来。

为什么必须编码?

当您向Web发出请求时转到HTTP层,您的浏览器将打开一个到服务器的套接字并执行HTTP请求:

GET category_list.php?category=tooth-white%20range HTTP/1.1 //好

GET category_list.php?category=tooth-white range HTTP/1.1 //错误

如果你有空格,这将导致请求中断,因为协议在HTTP请求中的url之后有空格。

答案 1 :(得分:1)

当编码网站URL时,%20是空格的ASCII代码,您只能传输ASCII字符。在PHP中,所有Superglobals($ _POST,$ _GET等)默认都是urldecoded(更进一步,你不应该尝试再次解码它们)。它只是您价值的不同表现形式。在构建链接的代码中,您可以轻松地:

$dynamicList2 .= '<li> <a href="category_list.php?category=' . urlencode($category) . '">' . $category . '</a> </li>';

这将确保您的链接以正确的编码方式构建。如果你曾经引用超级全局,请不要对它们进行urldecode。 PHP文档中的警告特别提到了这一点。

http://us2.php.net/manual/en/function.urldecode.php

答案 2 :(得分:0)

网址不能包含空格。它们应编码为%20。在PHP获取它之前,您的浏览器或服务器正在修复它。

使用urldecode将内容从网址转换为文字。