URL having parentheses not working in codeigniter

2019-09-07 16:05发布

问题:

I'm using codeigniter with datamapper ORM, today I've noticed a strange error.

I have a function named categories, which links from item page. url to category is formed using the name of the category and is encoded using rawurlencode.

In category method I decode this url string and find the category using get_by_name. this works well except when the name of the category includes parentheses

suppose I have a item "ABCDEFGHI" which belongs to category "Alphabets (English)". now category after running rawurlencode gives string "Alphabets%20%28English%29" which when I run rawurkdecode becomes again "Alphabets (English)" as expected.

<?php
$a = "Alphabets (English)";
$b = rawurlencode("Alphabets (English)");
$c = rawurldecode($b);
echo ($a == $c); //returns 1
?>

So I know that $a and $c is equal. I even tested method given below by replacing the exact string value say "Alphabets (English)" and it works but using rawurldecode doesn't.

But still the following method returns nil a 404 page.

function categories($url = '',$page = 1){
    $url = rawurldecode($url);
  //echo $url;
  //I tried echo here and it gives "Alphabets (English)"
    $cat = new Category();
    $cat->get_by_name($url);
  // replacing $url with the string output from echo $url 
  //in above call works. what is happening here?
  // $cat->get_by_name("Alphabets (English)"); works but 
  // $cat->get-by_name($url); doesn't
    if(empty($cat->id)){
        show_404();
    }
    else{
      // work to do//
    }
}

I know I shouldn't have used this approach, i should've titleize the name of categories and have an extra field in table. But I don't want ro break links now.

I hope you understood my problem. Please give me some clue. help me figure out the issue.

Thanks!

Edited

I read manual of rawurlencode, someone said on this page http://php.net/manual/pt_BR/function.rawurlencode.php

Note also that some characters are currently "reserved" but should have instead been considered as "unsafe": this includes the parenthesis "()" which are clearly unsafe when a URL is used in MIME headers.

Because of this, if a valid URL contains "()" characters, one should use an upper-level encoding to either enclose the URL with a pair of "unsafe" characters defined in the upper-level protocol (for example a "<>" pair in MIME headers, because these characters cannot be part of a valid URL)...

I believe either codeigniter or datamapper or both are not allowing these parenthesis characters. if you found some solution please help. I tried adding parentheses to allowed uri characters in codeigniter config and still no luck.

回答1:

if(empty($genre->id))

What is $genre, did you forget to set the return of $cat->get_by_name($url) to $genre ?