Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding $_GET variable URL ignores ampersand "&" character

Tags:

url

php

get

So im trying to get an url from the address bar that looks like this:

http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false

I'm using the $_GET variable to read it right off the URL my code is as follows

$arc = rawurlencode($_GET['name']);
echo "URL: $arc";

This only returns

URL: http://imgur.com/format.jsp?id=738ths3

It 's missing the &secure=false

What i want it to look:

 URL: http://test.com/format.jsp?id=738ths3&secure=false

I have tried urlencode, rawurlencode with no avail, i have looked in google a number of forums and stackoverflow none of the answer help, any ideas? Thanks!

urlencode shows this:

URL: http%3A%2F%2Ftest.com

so i cant have that either!

like image 292
user1294097 Avatar asked Mar 15 '26 02:03

user1294097


1 Answers

You'll need to urlencode() before constructing the URL, ie:

$url = "http://mysite.com/url.php?name=".urlencode('http://test.com/format.jsp?id=738ths3&secure=false');

This way, you will be able obtain the full URL as a name GET parameter from $_GET['name'].

Explanation:

Without urlencode() it when constructing the URL, PHP would treat is as 2 separate parameters, separated by &:

  1. $_GET['name']
    which is http://imgur.com/format.jsp?id=738ths3 for your case

  2. $_GET['secure']
    which is false for your case

Alternatively:

From your comment, it seems that you do not have control for the URL construction. You can get the full $_GET in a single string using http_build_query:

$name = http_build_query($_GET);

You would then obtain:

echo $name; // name=http://test.com/format.jsp?id=738ths3&secure=false

// which you would then may want to strip away the first 'name='
$name = substr($name, strlen('name='));

echo $name; // to obtain http://test.com/format.jsp?id=738ths3&secure=false
like image 75
uzyn Avatar answered Mar 17 '26 16:03

uzyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!