0

我遇到了关于正则表达式的奇怪情况。我想先检查是否设置了 ID,如果没有,则重定向到index.php

这是索引.PHP

$title = "This is a title";
$id = "5";
$queryc = $db->prepare("SELECT * FROM categories WHERE id = :id");
$queryc->bindParam(':id', $id, PDO::PARAM_INT); 
$queryc->execute();

/*** fetch the results ***/
$resultc = $queryc->fetchAll();
$count = count($resultc);
if ($count > 0) 
{
foreach($resultc as $rowc)

    {
    $cat_name = htmlentities($rowc['cat_name'], ENT_QUOTES, 'utf-8');
    $cat_name = stripslashes($cat_name);

    }
 }else {
 //redirect is a function 
 redirect("index.php");
 }
 echo "  
 <td width='57%' height='126' class='bord'  ><a href='$cat_name-$id.htm'  

 class='title_style'>" . $title . "</a><br>";

$cat_name可以有两个用空格分隔的单词,或者除了一些字符外,它也可以有重音字符。为此,我使用了.htaccess

  Options +FollowSymlinks
  RewriteEngine on
  RewriteRule ^([^!@#$%^&*(){}]+)-(\d+)\.htm$   file.php?id=$2 [L]

在结果显示的file.php中,我想确保设置了 $id

if( isset($_GET['id']) )
{

$id = (int) $_GET['id'];

    if( $id == 0 )
    {
        redirect("index.php");
        exit;   
    }

}   
else
{
redirect("index.php");
    exit;
}

在浏览器中,它看起来像这样: mydomain.com/auto auction-5.htm

我的问题是:( $cat_name = auto auction;注意空格)它不起作用,但如果$cat_name = autoauction;没有空格它可以工作。

有空间时如何让它工作?

4

1 回答 1

0

您的查询有特殊的字符(空格),女巫会导致浏览器先对您的查询进行编码然后发送。要克服这种情况,您必须首先使用urldecode对其进行解码。

于 2013-03-02T03:07:54.420 回答