Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation whats the proper way to pass &s and 's via a url?

    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
    PHP Code:
    $short_title 'a string';
    $short_title=htmlspecialchars($short_title); 
    pass the value in my url
    PHP Code:
    <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
    PHP Code:
    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

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try removing the htmlspecialchars() function, and just use the urlencode()

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    still the same mate.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Just let http_build_query() handle it for you:
    PHP Code:
    <?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 ?>">...

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    or you could just str_replace("\'","'",$_GET['title']);
    and the amps work fine with the urlencode()

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Quote Originally Posted by kbluhm View Post
    Just let http_build_query() handle it for you:
    PHP Code:
    <?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.

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    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

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    well stripslashes($_GET['title']); also works actually.

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    im currently using the stripslashes but its not making any difference

    i have tried this on the price results page

    PHP Code:
    $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.

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    i have remove all the string functions

    so i have this
    PHP Code:
    <a href="price_results.php?title=<?php echo $short_title?>&amp;category=<?php echo $category?>">
    and on the price results page i have this
    PHP Code:
    $searchterm $_GET['title']; 
    but still the slash lol

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yah, just use the replace function. I don't generally pass sting data in the querystring so I've never really had this issue.

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    yeah mate looks like the only way? will try it later

    thanks

  • #13
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    try html_encode() or htmlencode() can't remember which it is
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #14
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by hinch View Post
    try html_encode() or htmlencode() can't remember which it is
    Neither one of those are functions.

  • #15
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    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.

    PHP Code:
    if(isset($_GET['title'])){
    $searchterm stripslashes($_GET['title']);
    $searchterm urldecode($_GET['title']);

    where as i should urldecode the string first then strip the slashes
    PHP Code:
    if(isset($_GET['title'])){
    $searchterm urldecode($_GET['title']);
    $searchterm stripslashes($_GET['title']);

    that dont the trick

    thanks
    Luke


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •