...

View Full Version : whats the proper way to pass &s and 's via a url?



LJackson
09-11-2009, 05:10 PM
Hi All,

OK i am having some trouble passing the & and 's in a url, well not so much the & but im getting a /' for the 's.

here is what i have tried

Set my string

$short_title = 'a string';
$short_title=htmlspecialchars($short_title);

pass the value in my url

<a href="price_results.php?title=<?php echo urlencode($short_title)?>&amp;category=<?php echo $category; ?>">

and on the price_results page i have this

if(isset($_GET['title'])){
$searchterm = stripslashes($_GET['title']);
$searchterm = html_entity_decode($_GET['title']);
}


which i asume is where im going wrong?

any suggestions please
thanks

angst
09-11-2009, 05:12 PM
try removing the htmlspecialchars() function, and just use the urlencode()

LJackson
09-11-2009, 05:20 PM
still the same mate.

kbluhm
09-11-2009, 05:21 PM
Just let http_build_query() handle it for you:


<?php

$short_title = 'a title';
$category = 'a category';

$query_string = http_build_query( array(
'title' => $short_title,
'category' => $category,
) );

?>

<a href="price_results.php?<?php echo $query_string ?>">...

angst
09-11-2009, 05:27 PM
or you could just str_replace("\'","'",$_GET['title']);
and the amps work fine with the urlencode()

angst
09-11-2009, 05:29 PM
Just let http_build_query() handle it for you:


<?php

$short_title = 'a title';
$category = 'a category';

$query_string = http_build_query( array(
'title' => $short_title,
'category' => $category,
) );

?>

<a href="price_results.php?<?php echo $query_string ?>">...



has the exact same result, he just doesn't want the slash in there.

LJackson
09-11-2009, 06:08 PM
yeah the amps work fine with the urlencode() its just the ' always has a backslash infront of it,

thanks for the string replace option, i'll use that as a last resort as there has to be a reason as to why im getting the \ just gotta find the correct solution :)

thanks
Luke

angst
09-11-2009, 06:11 PM
well stripslashes($_GET['title']); also works actually.

LJackson
09-11-2009, 06:22 PM
im currently using the stripslashes but its not making any difference :(

i have tried this on the price results page


$searchterm = stripslashes($_GET['title']);
$searchterm = urldecode($_GET['title']);


because i thought the urlencode was adding the slash but im still getting the slash :(

will check through my code again to see if i have anyother code which may be causing this to happen.

LJackson
09-11-2009, 06:27 PM
i have remove all the string functions

so i have this

<a href="price_results.php?title=<?php echo $short_title?>&amp;category=<?php echo $category; ?>">

and on the price results page i have this

$searchterm = $_GET['title'];

but still the slash lol

angst
09-11-2009, 06:43 PM
yah, just use the replace function. I don't generally pass sting data in the querystring so I've never really had this issue.

LJackson
09-12-2009, 07:32 PM
yeah mate looks like the only way? will try it later :)

thanks

hinch
09-13-2009, 11:31 AM
try html_encode() or htmlencode() can't remember which it is

kbluhm
09-13-2009, 12:10 PM
try html_encode() or htmlencode() can't remember which it is

Neither one of those are functions.

LJackson
09-13-2009, 03:13 PM
Hi Guys, its ok i had a brain wave and managed to sort it out :)

the reason why it wasnt working was because i was trying to strip the slashes from a urlencoded string and then urldecode afterwards.


if(isset($_GET['title'])){
$searchterm = stripslashes($_GET['title']);
$searchterm = urldecode($_GET['title']);
}

where as i should urldecode the string first then strip the slashes

if(isset($_GET['title'])){
$searchterm = urldecode($_GET['title']);
$searchterm = stripslashes($_GET['title']);
}

that dont the trick :D

thanks
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum