0

I have this url https://www.industrialstores.com/search_results/Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve/46

but in above, %23 is # before Gas in url and making all the things after this unreadable in php.

When I trying to fetch the value of s & o from the query string, I am not getting any value for o and for s, only Asco+8214G30+3%252F4%5C%5C%5C%220%252F5 returns but it should return Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve

I am using httaccess rewrite to make this url RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([^/]+)?$ index.php?page=$1&s=$2&o=$3 [L]

And code for fetching the querystring value as :

if(isset($_GET['s'])){

$search_keyword=str_replace("_"," ",$_GET['s']);
}
if(isset($_GET['o']) && $_GET['o'] !=''){
$searchin = $_GET['o'];
}

And setting query string values as below:

$keyword = urlencode(str_replace("/","%2F",$_REQUEST['search_keyword']));
zzlalani
  • 19,534
  • 16
  • 41
  • 72
Inder
  • 17
  • 6
  • Why do you have this URL in the first place, can you clarify what goes wrong where? – Pekka Nov 21 '13 at 14:17
  • possible duplicate of [Can PHP read the hash portion of the URL?](http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url) – mcuadros Nov 21 '13 at 14:22
  • can you go here https://www.industrialstores.com/search_results/Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve/46 I am passing the value of s & o to ajax, you can check what parameters are passed to know the exact problem – Inder Nov 21 '13 at 14:23
  • urldecode is also returning this "Asco 8214G30 3/4\\\\\\\"0/5" not full text – Inder Nov 21 '13 at 14:24

3 Answers3

2

You cannot retrieve nothing after the #, any browser will remove because this is a anchor.

This is explained at: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

You you want get this # you must slugify/urlize your url with some library like Slugify this will encode the "strange" chars.

Example:

use Cocur\Slugify\Slugify;

$slugify = new Slugify();//for iconv translit
echo $slugify->slugify('Hello World!'); // hello-world

I think that this is better than let to the browser make a simple url encode.

Community
  • 1
  • 1
mcuadros
  • 3,854
  • 2
  • 33
  • 43
0

try use rawurlencode() function.

mEstrazulas
  • 138
  • 1
  • 10
  • yes. this function escape special chars. on destination code, use rawurldecode. echo rawurlencode('123#321'); // 123%23321 - %23 is # in hexadecimal – mEstrazulas Nov 21 '13 at 14:31
  • # is already coming as "%23" hexadecimal in the url https://www.industrialstores.com/search_results/Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve/46 but when I decode it, all the text after # is not returned – Inder Nov 21 '13 at 14:40
  • what the original text? when i try: [`code`]echo rawurldecode("Asco+8214G30+3%252F4%5C%5C%5C%220%252F5%23+Gas+Shutoff+Valve/46"); php return: [`code`]Asco+8214G30+3%2F4\\\"0%2F5#+Gas+Shutoff+Valve/46 – mEstrazulas Nov 21 '13 at 14:44
  • oriinal text is Asco 8214G30 3/4\"0/5# Gas Shutoff Valve – Inder Nov 21 '13 at 14:46
  • yo can go here https://www.industrialstores.com and put this text in search box and then click on search button, no record is found On this page, I am passing this query string parameters to ajax, you can check in ajax call, params are wrong – Inder Nov 21 '13 at 14:47
  • when i use: `rawurlencode("8214G30 3/4\"0/5# Gas Shutoff Valve")` return: `8214G30%203%2F4%220%2F5%23%20Gas%20Shutoff%20Valve` and rawurlDEcode works perfectly – mEstrazulas Nov 21 '13 at 14:49
  • ahhhhh, in ajax you need use `escape` in javascript request and rawurldecode in PHP – mEstrazulas Nov 21 '13 at 14:50
  • please can you go here industrialstores.com and put this text in search box and then click on search button, no record is found On this page, I am passing this query string parameters to ajax, you can check in ajax call, params are wrong – Inder Nov 21 '13 at 14:50
  • `escape (JS) = rawurlencode (PHP) ` `unescape (JS) = rawurldecode (PHP)` – mEstrazulas Nov 21 '13 at 14:51
  • you need send params in ajax escaped or your params sended broken – mEstrazulas Nov 21 '13 at 14:52
  • send in ajax url coded and in your requested page use rawurldecode – mEstrazulas Nov 21 '13 at 14:54
  • I tried but it is not working, when I am fetching the value of $_GET['s'], i am getting only string before the # i am using this echo ">>>>>".$search_keyword=rawurldecode(str_replace("_"," ",$_GET['s'])); – Inder Nov 21 '13 at 14:56
  • you can go here https://www.industrialstores.com/search_results/Asco%208214G30%203%252F4%5C%5C%5C%220%252F5%23%20Gas%20Shutoff%20Valve/46 you can see what we are getting from $_GET['s'], i echoed the value on page – Inder Nov 21 '13 at 14:57
  • I corrected this by converting 3 to %23 before urlencoding the url using this str_replace("#","%23",$_REQUEST['search_keyword']); – Inder Nov 21 '13 at 15:07
0

I corrected this by converting # to %23 before urlencoding the url using the code below,

str_replace("#","%23",$_REQUEST['search_keyword']); 
Nirav Madariya
  • 1,292
  • 2
  • 18
  • 34
Inder
  • 17
  • 6