/    Sign up×
Community /Pin to ProfileBookmark

$_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[/CODE]

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>”;
[/CODE]

The output looks like:

[CODE]org=CompanyName&last=Jones&phone=+52%209999999999&[email protected]&select=org&count=10&sid=0.9767910006196713

‘ 52 9999999999’

‘ 52 9999999999’

‘+52+9999999999’[/CODE]

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

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiSep 18.2009 — I'm guessing you must have a config issue somewhere, because I ran your code verbatim and got the following:

<i>
</i>select=org&amp;action=and&amp;org=CompanyName&amp;last=Jones&amp;phone=&amp;#37;2B52%209999999999&amp;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?
Copy linkTweet thisAlerts:
@MindzaiSep 18.2009 — 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.
Copy linkTweet thisAlerts:
@dataspongeauthorSep 18.2009 — 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));
[/CODE]


But I'd much rather it behave like your output and I want to understand why this is happening.

Phil
Copy linkTweet thisAlerts:
@MindzaiSep 18.2009 — 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.
Copy linkTweet thisAlerts:
@dataspongeauthorSep 19.2009 — 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.[/QUOTE]

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
Copy linkTweet thisAlerts:
@dataspongeauthorSep 19.2009 — 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
×

Success!

Help @datasponge spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...