I have a URL like:
http://www.google.com/test.html?d=1232&u=32
and I want to add it as a part of a GET query string like:
http://www.mysite.com/index.html?a=123&d=http://www.google.com/test.html?d=1232&u=32
Note the double "d" used. I want the URL sent to be just a url and not be read for it's query string...
What is the best way to do this to avoid problems?
You can use the urlencode() function.
Example:
$url = 'http://www.mysite.com/index.html?a=123&d='
. urlencode('http://www.google.com/test.html?d=1232&u=32');
You can use urlencode() to put that in the URL without having it interfere with anything else you have in there.
URL-encode the second url:
http://mysite.com/index.html?a=123&d=<?php echo urlencode('http://google.com/etc..'); ?>
You can assign a url to a variable and have it be query-string safe by using urlencode() (http://us3.php.net/urlencode). So you could do:
$url = 'http://www.mysite.com/index.html?a=123&d=' . urlencode('http://www.google.com/test.html?d=1232&u=32');
In this example the query-string var 'd' now houses all the contents of the second url. You will have to urldecode() it on the receiving end in order to extrapolate the data.
Related
I have in the adressbar the url phpexample.com/go.php?https://www.phpezzz.com/community/article1.html.
How do I extract from go.php file the www.phpezzz.com/community/article1.html part? This is, without assigning the url to a variable in query string.
Currently I use go.php?m=https://www.phpezzz.com/community/article1.html and extract the link with $m = $_GET['m'];, but I don't want to use m= in the URL.
You should be able to use $_SERVER['QUERY_STRING'] to get the whole string.
There is a string XX&YY and I'm passing it to another page. ie, localhost/sample/XX&YY/1 for some processing. Now when I try getting the name value on the other side I'm able to get only XX and not full XX&YY. How to rectify it? Any ideas?
Note : here is my url localhost/sample.php?name=somevalue&pageno=somevalue has been url re-written to localhost/sample/name/pageno.
You have to escape the URL . You can use rawurlencode() or urlencode() to encode your URL.
sidenote: Difference of the 2 functions
If I'm understanding correctly, this is the URL to your script:
http://localhost/sample/name/pageno
Which is then rewritten by your web server to this:
http://localhost/sample.php?name=somevalue&pageno=somevalue
Then, this is how you should format the URL:
$url = sprintf('http://localhost/sample/%s/%s',
urlencode('XX&YY'),
urlencode('1')
);
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!
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 &:
$_GET['name']
which is http://imgur.com/format.jsp?id=738ths3 for your case
$_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
The original URL, http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false, contains two query-string parameters: name and secure. The & in the query-string belongs to the full URL, not the URL in the name parameter.
If you have control over this value, when declaring the link/URL, use PHP's urlencode() to encode the full name value, such as:
$url = "http://mysite.com/url.php?name=" . urlencode("=http://test.com/format.jsp?id=738ths3&secure=false");
This will properly encode the name parameter and your $arc = $_GET['name']; will work as desired.
If you do not have control over setting the value and are simply parsing something you're receiving, you can split the given string on the name= parameter and assume everything else after it is part of name:
$splitQuery = split('name=', $_SERVER['REQUEST_URI']);
$arc = $splitQuery[1];
To decode the encoded URL, after you've accessed it, use PHP's urldecode():
$arc = urldecode($_GET['name']); // assuming you're properly encoding the `name` parameter
If you cannot encode the URL, you can get the current URI with this code:
$url = $_SERVER["REQUEST_URI"];
that in your case, the $url is :
/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false
Then you can split it with explode and validate it and take the GET params from it.
I am trying to pass a url for creating an iframe as a parameter of a query string. Because the url that I am passing contains an ampersand, I encode the url with 'urlencode' then append it to the query string.
<?php
$url = "http://www.somesite.com/index.php?option=content&view=article&id=1234:some+article";
$url_encoded = urlencode($url);
?>
On the page where I want to create the iframe, I retrieve the url parameter using the $_GET variable.
<?php
$iframe_source = $_GET[$url];
?>
<iframe id="external-link-frame" src="<?php echo $iframe_source ?>"></iframe>
However $_GET only retrieves the part of the parameter value up to the encoded ampersand.
<?php echo $_GET[$url]; //outputs http://www.somesite.com/index.php?option=content ?>
What must I do in order to send the entire url including the parameters that are part of its own query string.
UPDATE: I am able to do it by encoding the url twice
urlencode(urlencode($url));
Take a look at: https://stackoverflow.com/a/2433211/1359529
I think rawurlencode() will encode the ampersands too.
http://us3.php.net/manual/en/function.rawurlencode.php
I believe, how you append it, the $_GET function thinks that the ampersand signs are signifying new values to get. I bet if you did after that $iframe_view = $_GET[$view] it will output article.
If you want it to get the full URL, I think it's best to encode by replacing & signs with something else and then once you get the url, then replace them back to & signs.
I call a php script http://site.com/process.php that takes a url as one of its parameters. for=
http://site.com/process.php?for=http://www.anotherwebsite.com
I then do this and try to parse_url() but parse_url() gives a parse error.
$uri = $_SERVER['REQUEST_URI']; // /process.php?for=http://www.anotherwebsite.com
parse_url($uri);
How can I encode the for parameter either on the sending side (in the url) or on the receiving side (php) so that parse_url() understands that it's just a parameter that happens to look like a url?
Before including your url as a get parameter, use urlencode
$full_url = 'http://site.com/process.php?for=' . urlencode('http://www.anotherwebsite.com');
This function is convenient when
encoding a string to be used in a
query part of a URL, as a convenient
way to pass variables to the next
page.
To reverse the result of urlencode, use urldecode. As mario pointed out in a comment below, $_GET parameters are already urldecoded.
Well, first you must urlencode() the for= parameter, then in process.php, you can simply do
$url = $_GET["for"];
$url = urldecode($url); // http://www.anotherwebsite.com
Here are the functions:
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php