php - urlencode 问题

标签 php mysql pdo

我在过滤系统中编码&符号时遇到问题。
例如,如果第一个过滤器用于品牌且品牌名称为“海飞丝”,则 url 应类似于 website/category/brand-head+%26+shoulders,但仅当 url 为 website/category/时过滤器才有效brand-head-%252526+shoulders.
如果选择了另一个过滤器,例如高度,如果 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)."'";
}

这是来 self 的 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]

最佳答案

PHP 在启动期间填充 $_GET[] 时自动解析查询字符串和 URL 解码值。

生成 URL 时 urlencode() 值是正确的,但您不能 urldecode()$_GET[].

更新:

正如 Apache documentation 中所解释的那样:

mod_rewrite has to unescape URLs before mapping them, so backreferences are unescaped at the time they are applied

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

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

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

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

关于php - urlencode 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349868/

相关文章:

mysql - 列变量更改后恢复 MYSQL 值

php - 选择插入的最后 7 条记录的 MAX 和 MIN 值

PHP MYSQL PDO 列的 SUM

带有 jquery ajax 的 Php pdo UPDATE 函数

php - PDO MySQL 产品循环

php - 按降序对表记录进行排序 - symfony

php - 为 PDF 动态创建 map

php - Laravel 查询,显示一个表中的结果,其 id 不在另一个表中

php - Guzzle 异步请求不起作用

javascript - 为什么 SQL 语句将 "missing"列的值更新为 NULL?