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.
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.
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?
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.
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.
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.
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.
Bookmarks