Get URL string parameters with PHP - php

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.

Related

Create a php variable from URL without question mark

I need to create a variable in PHP from a URL, which does not have a fully formed query string.
e.g. http://search.domain.com/domain2.com
In this example, the variable needs to be
$website='domain2.com'
Is there a way to convert the entered URL in address bar to my ?website= variable?
An example would be the whois.domaintools service, which allows you to query a whois record from their website using the following url format:
http://whois.domaintools.com/domain.com
This then displays info based on the url you specified.
Can i achieve this using a MOD_Rewrite in the .htaccess, or can i use some PHP function like http_build_query to achieve this? I'm going around in circles and surely missing something obvious!
You can use this code to get your array $urlpart
$link = $_SERVER['REQUEST_URI'];
$urlpart = explode('/',trim(parse_url($link, PHP_URL_PATH), '/'));

How can I add a URL to a query string?

I have a small problem with my PHP script. I want to be able to have a URL within a query string so it would look like this:
http://example.com/?url=http://google.com/
This works absolutely fine and $_GET['url'] will return http://google.com.
The problem is when the URL in my query string already has query string, for example:
http://example.com/?url=http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011
will return:
http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8
and I want it to return:
http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011
I am using PHP for server side.
Could anybody please help?
Update
I am using Codeigniter, so if this is the reason why it isn't working as it should then please let me know.
You need to encode the url passed as query argument:
If you send it from PHP, use urlencode or rawurlencode.
If you send it from JS, use encodeURIComponent.
Use urldecode() to pass query string

Pass a URL in a GET for PHP

I need to pass a url using a GET address. To give an example which was I have tried:
http://www.example.com/area/#http://www.example.com/area2/
I've also tried replacing the forward slashes with other characters but that doesn't seem to work. How would you pass a url in a GET?
As I have understood, you should use url_encode() and url_decode().
The function url_encode() lets you create a string that can be used as a link.
You should use it this way:
$link = 'goto.php?link=' . url_encode($_POST['target_site']);
And when you were going to redirect to the user defined site (eg), you can decode the parameter given this way:
$decoded_link = url_decode($_GET['link']);
// Now it's safe to use the given URL (for example I can redirect to there)
header('location: ' . $decoded_link);
Hope it helps.
The # character links to an anchor on the page. The browser will automatically scroll to the element with the id after the point sign. So that's not what you're looking for.
To pass a GET parameter, the syntax would be like this:
http://example.com/area?http://example.com/area2
Then, if you var_dump($_GET), you'll see your URL. But, if you have other fields you also want to pass in your URL, you can use key=value pairs, like so:
http://example.com/area?url=http://example.com/area2&param1=a&param2=b
In this case, your URL will be available in $_GET['url'].

How to encode, send in a query, and retrieve a url

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.

PHP URL in query string using GET

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.

Categories