www.webdeveloper.com
Results 1 to 7 of 7

Thread: $_GET removes + in Phone Number

  1. #1
    Join Date
    Sep 2009
    Posts
    6

    $_GET removes + in Phone Number

    PHP $_GET is removing the + sign in a phone number in the URL. I suspect it is converting to a number rather than a string, but I haven't figure out how to get around it.

    The URL looks like:

    Code:
    http://MyServer/quick_select.php?select=org&action=and&org=CompanyName&last=Jones&phone=%2B52%209999999999&email=myemail%40mydomain.com
    The relevant php lines look like:

    Code:
    $strQURL = $_SERVER['QUERY_STRING'];
    echo $strQURL."<br><br>";
    $phone=$_GET["phone"];
    echo "'".$phone."'<br><br>";
    echo "'".urldecode($phone)."'<br><br>";
    echo "'".urlencode($phone)."'<br><br>";
    The output looks like:

    Code:
    org=CompanyName&last=Jones&phone=+52%209999999999&email=myemail@mydomain.com&select=org&count=10&sid=0.9767910006196713
    
    ' 52 9999999999'
    
    ' 52 9999999999'
    
    '+52+9999999999'
    As you can see:
    - The complete URL is decoded properly as a whole with the %2B converted to a plus sign
    - The $_GET['phone'] changes the + to white space
    - urldecode() does not change the behavior.
    - urlencode() puts an extra plus sign at the gap between the country code and area code.

    I tried casting to a (string) - no change.

    I need to handle the plus sign to recognize international phone numbers, but I don't want to parse the query string myself.

    Any suggestions?

    Thanks

    Phil

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    I'm guessing you must have a config issue somewhere, because I ran your code verbatim and got the following:

    Code:
    select=org&action=and&org=CompanyName&last=Jones&phone=&#37;2B52%209999999999&email=myemail%40mydomain.com
    
    '+52 9999999999'
    
    ' 52 9999999999'
    
    '%2B52+9999999999'
    I'm stumped as to what your issue is though, only that it isn't your code. Notice the server query string is returned differently (partially decoded) which I suspect is the problem. Can you give some details on the server you are running etc?
    Last edited by Mindzai; 09-18-2009 at 05:56 PM.

  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    Also are you running this script through a redirect or med_rewrite? I think Apache will evaluate certain encoded characters during rewrite/redirect, which fits your results as a + means a space in it's raw form.

  4. #4
    Join Date
    Sep 2009
    Posts
    6
    Interesting.

    Your $_SERVER['QUERY_STRING'] output still had the %2B in it. Mine had replaced it with '+'.

    What php version are you running? (I'm on PHP 5.2.4.)

    Specifically, are you on PHP 5.3? That would give me one more reason to tackle the build process to upgrade along with all the improved date functions I need.

    I solved my basic problem by doing the urldecode and replacing any added + signs after the first character as in:

    Code:
    $phone=$_GET["phone"];
    $phone=urlencode($phone); // urlencode gets back the + sign
    if (substr($phone,0,1)=="+")
      $phone="+".str_replace("+"," ",substr($phone,1));
    But I'd much rather it behave like your output and I want to understand why this is happening.

    Phil

  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    I'm using 5.2.6 with apache 2 and linux. I really suspect this is a server issue rather than a php issue. Apache at least will decode urls before certain tasks like redirects and rewrites for comparative purposes and I suspect something like that is happening here. You could test it by urlencoding the data twice - if this fixes it you know the server is interfering somewhere.

  6. #6
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by Mindzai View Post
    Also are you running this script through a redirect or med_rewrite? I think Apache will evaluate certain encoded characters during rewrite/redirect, which fits your results as a + means a space in it's raw form.
    I'm opening the page directly. But the comment re: + seems significant.

    I'll try urlencoding twice and see what happens and do a little research into my configuration with an eye on the areas you point out.

    Thanks for the responses.

    Phil

  7. #7
    Join Date
    Sep 2009
    Posts
    6
    urlencoding twice simply replaces + with &#37;2B and replaces the space after the country code with %2B.

    That is, it displays '%2B52%2B9999999999'

    I haven't checked my PHP / Apache configs yet.

    Phil
    Last edited by datasponge; 09-18-2009 at 08:28 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles