问题描述
我在datamapper ORM中使用codeigniter,今天我注意到一个奇怪的错误。
我有一个名为category的函数,该函数从项目页面链接。使用类别名称形成类别的url,并使用rawurlencode进行编码。
在类别方法中,我对该URL字符串进行解码,并使用get_by_name查找类别。这很好用,除非类别名称包括括号
假设我有一个项目 ABCDEFGHI属于类别字母(英语) )。现在,运行rawurlencode后的类别给出字符串 Alphabets%20%28English%29,当我运行rawurkdecode时,该字符串将再次变为 Alphabets(英语)。
<?php
$ a =字母(英语);
$ b = rawurlencode( Alphabets(English));
$ c = rawurldecode($ b);
echo($ a == $ c); //返回1个
?>
所以我知道$ a和$ c是相等的。我什至通过替换确切的字符串值 Alphabets(English)来测试下面给出的方法,它可以工作,但使用rawurldecode无效。没有404页。
函数类别($ url ='',$ page = 1){
$ url = rawurldecode($ url );
//回显$ url;
//我尝试在此处回显,它给出了字母(英语)
$ cat = new Category();
$ cat-> get_by_name($ url);
//在上面的调用中将$ url替换为echo $ url
//输出的字符串。这是怎么回事
// $ cat-> get_by_name( Alphabets(English));有效但是
// // $ cat-> get-by_name($ url);不会
if(empty($ cat-> id)){
show_404();
}
else {
//要完成的工作//
}
}
我知道我不应该使用这种方法,我应该给类别名称加标题,并在表中有一个额外的字段。但我现在不希望ro断开链接。
我希望您理解我的问题。请给我一些线索。帮助我找出问题所在。
谢谢!
编辑
我读了Rawurlencode手册,有人在此页面上说
还请注意,目前某些字符已保留,但应被视为不安全:其中包括括号 ()在MIME标头中使用URL时显然是不安全的。
因此,如果有效的URL包含()字符,则应使用一个高层编码,以使用在高层协议中定义的一对不安全字符将URL括起来(例如,MIME头中的一对<>对,因为这些字符不能成为有效URL的一部分)。 ..
我相信无论是codeigniter还是datamapper或两者都不允许使用这些括号字符。如果您找到了解决方案,请提供帮助。
我试图在codeigniter配置中为允许的uri字符添加括号,但仍然没有运气。
if(empty($ genre-> id))
什么是$ genre,您忘了将$ cat-> get_by_name($ url)的返回值设置为$ genre吗?
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.
if(empty($genre->id))
What is $genre, did you forget to set the return of $cat->get_by_name($url) to $genre ?
这篇关于带有括号的URL在datamapper ORM的codeigniter中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!