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¶m1=a¶m2=b
In this case, your URL will be available in $_GET['url'].
Related
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), '/'));
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.
I am using a script to check links on a given page. I am using simple html DOM to parse the information into an array. I have to check the href of all the a tags to find if they contain a file or something like # or JS.
I tried the following without success.
if(preg_match("|^(.*)|iU", $href)){
save_link();
}
I dont know it my pattern is wrong or if there is a better method to complete this function.
I want to be able to detect if $href contains .com .php .file extensions. This way it will filter out items like # "function()" and other items used in the href attribute.
EDIT:
parse_url will not work stop posting it. The value # returns as a valid url like I stated above I am trying to look for any string followed by .* with no more than 4 chars following the .
I believe that the function you're looking for is parse_url().
This function will take a URL string, and return an array of components, which will allow you to work out what kind of URL it is.
However note that it has issues with incomplete URLs in PHP versions prior to 5.4.7, so you need to have the very latest PHP to get the best out of it.
Hope that helps.
See http://php.net/manual/en/function.parse-url.php
I'm assuming you don't want to match fragments (#) because you are not concerned with following internal anchors.
parse_url breaks up the different parts of the url into an array. You can see the path component of the URL in this array and run your check against that.
You can use parse_url() , like this :
$res = parse_url($href);
if ( $res['scheme'] == 'http' || $res['scheme'] == 'https'){
//valid url
save_link();
}
UPDATE:
I've added code to filter only http and https urls, thanks to Baba for spotting this.
I want to get only URL string in parameter.
So I coded below :
//In my HTML file.
<script>
document.write('<iframe id="ifr1"
src="http://{domain}/prj.php?url=https://www.google.co.kr/search?sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8&q=url+utf8+encoding#sclient=psy-ab&hl=ko&newwindow=1&prmdo=1&tbm=klg&q=google&oq=google&aq=f&aqi=g10&aql=&gs_l=serp.3..0l10.2207859.2208404.3.2208459.6.4.0.1.1.3.496.1353.2-2j1j1.4.0...0.0.O1lbexylHQM&pbx=1&prmdo=1&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=b8fee9fdb4e7f90&biw=1440&bih=828&a=' +'asdf'+" width="100%" height="100%" frameborder="0"> </iframe>');
</script>
//In my php file
$message = $_GET['url'];
echo("$message");
But I only get 'https://www.google.co.kr/search?sugexp=chrome,mod=3'.
What's wrong?
I want to get whole URL string. Including &,?,or everything else.
You need to use encodeURIComponent to encode the url parameter.
The reason is that you use an encoding into another encoding.
The first is:
prj.php?url=[somedata]
where [somedata] is an URL with another key/value collection:
https://url/etc/search?key1=value1&key2=value2
If the url parameter is not urlencoded there is no way to distinguish the parameters of /prj.php from the parameters of /search, it is assumed that all the parameters defined with &key=value&key=value... syntax belong all to the first page.
The trick is to url-encode the url parameter, but how you do it depends on the context.
If the url is dinamically generated via PHP, use the urlencode() function.
If your url is hard-coded (not generated via PHP), you can write for yourself a little PHP tool to do the job, or use an online encoder like http://www.opinionatedgeek.com/dotnet/tools/urlencode/Encode.aspx
I am trying to fetch a variable address from my current URL using JRequest::getVar('address') method.
But if the address value has a (#) character, the part after the # character is not retrieved.
I understand that URI is a combination of query + fragment and the part after a hash symbol is treated as a fragment.
I have tried to use urlencode method but it still doesn't solve the problem.
Can anyone please tell me how to solve the issue?
What is the problem with using urlencode? It should replace # with %23 and all should be well. You can try JRequest::getVar(str_replace('#', '%23', 'address')) which should do the trick. Can you post an example URL that doesn't get properly urlencoded?
I guess you will have to replace the hash-symbol on your own. For example:
str_replace($the_url, '#', '-');
I don't know, where exactly you have to do that, because I don't know how the Joomla!-Framework handles links and urls. But I am sure, that someone else can help here any further...
Encode the Hash in the URL with a %23 replacement
http://twitter.com/home?status=I+believe+in+%23love
"I believe in #love"
The part after # is never sent to Apache/PHP, and can therefore not be retrieved by a PHP script. What you need to do, is to url encode the ADDRESS parameter of the URL.
test.com/index.php?ADDRESS=<?= urlencode('101 Street #6 City') ?>
That code will generate the following url
test.com/index.php?ADDRESS=101+Street+%236+City
Now on this URL, you can retrieve address with JRequest::getVar('ADDRESS')
Check this Joomla doc out. You can retrieve what Joomla call the 'fragment' by doing:
$uri = 'http://fredbloggs:itsasecret#www.example.com:8080/path/to/Joomla/index.php?task=view&id=32#anchorthis';
$u =& JURI::getInstance( $uri );
echo 'Fragment is ' . $u->getFragment();