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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question problem with the $_GET thingy

    hello people a newbie here with a simple question:


    index.php?code=abcd
    $code = $_GET['code'];
    echo "$code"; returns abcd


    index.php?code=abcd&efgh
    $code = $_GET['code'];
    echo "$code"; also returns abcd

    how can i fix this problem. i know "&" is the problem so i want it to be ignored and treated as just another character.

    thanks in advance

    by the way, what other characters could potentially create the same problem?

  • #2
    New Coder
    Join Date
    Feb 2008
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Write the & sign as & #38;

    Just remove the space between the & and the # in the thing above (in cannot write it as it should be otherwise the browser actually converts it to a read & sign).



    That 38 thing is the "html entity" for the & sign

    php can do this automatically for you with the function urlencode

    see http://www.php.net/manual/en/function.urlencode.php

    So your link should be

    index.php?code=abcd& #38;efgh

    (once again just remove the space between & and # and it will work)
    Last edited by deamonlizard; 02-19-2008 at 10:31 PM.

  • #3
    New Coder
    Join Date
    Aug 2005
    Location
    Groningen, Netherlands
    Posts
    57
    Thanks
    0
    Thanked 6 Times in 6 Posts
    And another small explanation: the & is a separator between the arguments in the $_GET request. Therefore, you should always use urlencoding (like deamonlizard talks about).

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Examples of the above suggestions. This is how it will look when using
    then you can decode it using urldecode.
    PHP Code:
    $code urlencode('abcd&efgh');
    echo 
    $code// returns abcd%26efgh 
    PHP Code:
    <?php
    $code 
    '';
    if(isset(
    $_GET['code']))
    {
        
    $code urldecode($_GET['code']);
        echo 
    $code// returns abcd&efgh
    }
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    the & don't create problem, the problem is that you don't know that the query string must be one or more pair name=value separated by & or ;

    in your second example if you use:

    Code:
    ndex.php?code=abcd&efgh=myvalue
    $code = $_GET['code'];
    echo "$code"; also returns abcd // you don't need to quote $code, is a variable
    $efgh = $_GET['efgh'];
    echo $efgh; will return  'myvalue'
    @jaap: urlencode is used to encode specials chars in url, not &

    Edit:
    sorry, I missunderstand what you want. After _Aerospace_Eng_ I understand that you are talking about a single value which contain &.

    @jaap: you are right in this case, & is special char inside a value, and urlencode work,


    best regards
    Last edited by oesxyl; 02-19-2008 at 10:57 PM.

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    If its a string that gets passed to the url then yes you can encode and yes it does encode &.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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