2013-03-02 16 views
0

我遇到了關於正則表達式這種奇怪的情況。我想首先檢查是否ID設置,如果沒有的話重定向到index.php

THIS IS 的index.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 ^([^[email protected]#$%^&*(){}]+)-(\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拍賣5.htm

我的問題是: $cat_name = auto auction;(注意空間)它不起作用,但是如果$cat_name = autoauction;沒有空間它就起作用。

當空間在那裏時,我該如何使它工作?

+0

你是什麼意思「未設置」?你的意思是傳遞的字符串是「category name-」或「category name-0」? 爲什麼你需要使用PHP?你不能只使用你已經在你的.htaccess文件中使用的正則表達式來確定那個數字是否在那裏? – dcasadevall 2013-03-02 02:42:52

+0

可以說$ id = 5,該網址將是mydomain.com/auto拍賣5.htm,如果我運行這個mydomain.com/auto拍賣-2045.htm我想它重定向到index.php自2045年不'不存在。 – Rocks 2013-03-02 02:48:36

回答

0

您的查詢有特殊字符(空格),女巫會導致瀏覽器先編碼您的查詢然後發送它。爲了克服這種情況,你首先要用urldecode來解碼。