【问题标题】:Issue with urlencodeurlencode 问题
【发布时间】:2015-05-20 12:37:37
【问题描述】:

我在过滤系统中对 & 符号进行编码时遇到问题。
例如,如果第一个过滤器用于品牌并且品牌名称是“海飞丝”,则 url 应该看起来像 website/category/brand-head+%26+shoulders,但过滤器仅在 url 是 website/category/ 时才有效品牌头-%252526+肩膀。
如果选择了另一个过滤器,例如高度,则如果 url 是 website/category/brand-head-%25252526+shoulders 等,品牌过滤器将起作用,我必须在百分号前再添加 25 个。

这是品牌过滤代码

if($brand == false){
    $data['brands'] = array();
    $brands         = $attributes->getBrands($av_list, $_GET['c']);
    $brandTotalCnt  = 0;

    if(!empty($brands)){
        foreach ($brands as &$brand) {
            if($av_list){
                $cur_av_list  = $av_list  . $brand['brand'];
                $cur_av_uri   = $_GET['av'] . $brand['brand'];
            } else {
                $cur_av_list  = $brand['brand'];
                $cur_av_uri   = $brand['brand'];
            }
            $tmp_uri          = explode('/', $ln_uri);
            $tmp_uri_array    = $attributes->remove_items_by_value($tmp_uri, 'brand');
            $new_uri          = implode('/', $tmp_uri_array);   
            $brandTotalCnt   += $brand['num'];

            $data['brands'][] = array(
                    'brand'           => ucwords(strtolower($brand['brand'])),
                    'num'             => $brand['num'],
                    'href'            => $url->link('/c/'.$_GET['c'], '/brand-'. urlencode(strtolower($brand['brand'].'1')). $new_uri)
                );

        }
    }
    $data['brandTotalCnt'] = $brandTotalCnt;
}

每个过滤器的 URL 解码

if(isset($brand)){
    $sql .= " AND brand = '".urldecode($brand)."'";
}

if(isset($gramaj)){
    $height= $_GET['height'];
    $sql .= " AND height= '".urldecode($height)."'";
}

这是来自我的 htaccess

# rewrite /category/brand-mybrand/country-mycountry/offer-yes/new-yes
# to /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^c/([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA]

# rewrite /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
# converts any /name-val/ to query parameter name=val in every rewrite
# stopping when there is no part left after /index.php
RewriteRule ^(index\.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA]

【问题讨论】:

  • 问题是添加了更多的 25,还是您需要添加这些 25 才能使您的脚本正常工作?
  • 我需要添加才能使我的脚本正常工作
  • 看起来每个过滤器都在执行urldecode。你的脚本中有urldecode吗?
  • 是的,我有 urldecode 的每个过滤器

标签: php mysql pdo


【解决方案1】:

当 PHP 填充 $_GET[] 时,PHP 会在启动期间自动解析查询字符串并对值进行 URL 解码。

生成 URL 时,urlencode() 的值是正确的,但你不能 urldecode()$_GET[] 中选择的值。

更新:

正如 Apache documentation 中所解释的:

mod_rewrite 必须在映射 URL 之前对其进行转义,因此反向引用在应用时不会转义

如果您的重写规则采用路径的一部分并将其放入查询字符串中,则标志 [B](转义反向引用)指示引擎使用未转义的 URL 来匹配规则。

您的问题有两种解决方案:

  1. (简单的解决方案):生成包含非字母数字字符的URL;只使用字母、数字和破折号 (-),您是安全的;
  2. (高级解决方案):在受影响的重写规则中添加[B]标志:

    RewriteRule ^c/([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA,B]
    

【讨论】:

  • print_r($_GET) 返回数组 ([brand] => head [shoulders] => [c] => ingrijirea-parului)
  • 糟糕,这意味着问题出在其他地方。也许在重写规则?如果 Apache 在检查重写规则时使用 URL 解码路径,这听起来很合理。 (路径还需要进行 URL 编码,并且您正确地做到了这一点。Apache 需要先解码路径,然后再将其查找到文件系统中)。如果发生这种情况,则品牌名称将出现在未编码的查询字符串中,并导致此输出。或者它可能对其进行编码?!
  • 我在 Apache 文档中找到了问题和解决方案,并更新了答案。
  • 也许最好在创建 uri 之前替换 & 符号(例如:str_replace('&', '-and-', $uri);)并在查询数据库之前将其反转。我最近遇到了同样的问题,这就是我解决的解决方案。 :)
  • 也许它与其他重写规则冲突。我会选择简单的解决方案:在友好的 URL 中只使用字母、数字和破折号。
猜你喜欢
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多