This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
I have a function which looks like this:
function fullURL(){
$domain = $_SERVER['SERVER_NAME'];
$path = $_SERVER['PHP_SELF'];
$raw_url = $domain.$path;
$url = substr($raw_url, 0, -4);
echo $url;
}
This generates an URL which looks like 127.0.0.1/aura/profile
But, what if I would use this on a page which uses a $_GET-tag?
Then, my URL won't pick it up.
For example I have news?id=1, when using the function above it just becomes 127.0.0.1/aura/news.
What do I need to add to the function to make it output 127.0.0.1/aura/news?id=(id)?
What about this?
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Note that you could also do:
$actual_link = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
Original answer: https://stackoverflow.com/a/6768831/3150271
Related
This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 2 years ago.
I need to take a img link (https://upload.wikimedia.org/wikipedia/en/5/51/Minecraft_cover.png) from this api https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any. How to do it?
I wrote code like this, but I can print :
$img_url = "https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any";
$img_url = str_replace(" ", "%20", $img_url);
$img = json_decode(file_get_contents($img_url));
print_r ($img);
But how to print only img source?
The simplest way would be to use the following.
echo $img->query->pages->{'27815578'}->original->source;
Where 27815578 is the Page ID
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I'm trying to change the url of a link on my wordpress site thru a PHP function. So far echoing out the string url is giving me trouble.
I have the following in my PHP file.
$siteurl = site_url(); // gives me: "http://localhost/testsite"
$teststring = "/catalog";
$entireurl = $siteurl.$teststring; //gives me "http://localhost/testsite/catalog"
function change_site($buffer) {
$in = array('<a href="http://localhost/testsite/type/combined/?case=main" title="Catalog">');
$out = array('<a href="'.$entireurl.'" title="Catalog">');
return (str_replace($in, $out, $buffer));
}
ob_start("change_site");
Currently, the link takes on the link of another href link in the same div. (bizzare)
Try this instead. You might be missing the . concatenator
$out = array('<a href="'.$entireurl.'" title="Catalog">');
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I have an url that I call in this form :
http://MyIP/MyServer/php_scripts/test_sp.php?param1=key¶m2=xyz5
and I would like to retrieve the value of the 'param1' and 'param2' parameters. Up to now, I was not successful, because all I get is an empty value.
Sure that I am certainly doing something wrong there, but is it with the way I am calling the url or in my php code itself? Any help will be highly appreciated. Thanks a lot for reading me.
The code below is the contents of my 'test_sp.php' file :
<html>
<head>
</head>
<body>
<?php
mainProcess();
function mainProcess()
{
$mavalue1 = "";
$mavalue2 = "";
$parts = parse_url($url, PHP_URL_QUERY); // << It looks like I also have a problem with this line.
parse_str($query, $params);
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];
echo "Valeur de mavalue1 : " . $mavalue1; // <<< This is where I'm getting the empty value
echo "Valeur de mavalue2 : " . $mavalue2; // <<< Same problem here
}
?>
</body>
</html>
The $_GET array is an array with all the arguments passed in the URL.
You can use $_GET['param1']; to get the value of "param1" in the URL.
Have a nice day.
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 8 years ago.
I am using google api and in url i am getting like this:
http://website.com/generate-token/#access_token=ya29.AHES6ZTJwb9lzb0lua81oHa47-8ImcJf-8qE-02kIn8JcgEv&token_type=Bearer&expires_in=3600
I tried:
$_SERVER['QUERY_STRING']
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
But what i got is http://website.com/generate-token/
How can i get either the complete url and use regex to get the access_token or any other way in which i can get it?
My two cents (since you tagged it with jQuery i suppose Javascript is a valid solution):
var url = 'http://website.com/generate-token/
#access_token=ya29.AHES6ZTJwb9lzb0lua81oHa47-8ImcJf-8qE-
02kIn8JcgEv&token_type=Bearer&expires_in=3600';
function getParameterByName(name, url) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" :
decodeURIComponent(results[1].replace(/\+/g, " "));
}
alert(getParameterByName('access_token', url));
http://jsfiddle.net/yejwF/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace values in a URI query string
I am using a language switcher in the navigation bar to switch between EN and DE, my url structure is like this:
http://www.mydomain.com/gallery.php?lang=de
http://www.mydomain.com/gallery-item.php?id=100&lang=de
The switch works well on all urls which don't have an id, but it doesn't work with the ids.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
<a href='.$url.'?lang=en">
<a href='.$url.'?lang=de">
?>
What is a good solution to check for $lang=xx or ?lang=xx and then add ?lang=xx or $lang=xx respectively?
$params = $_GET;
$params['lang'] = 'en';
printf('En', $url, http_build_query($params));
$params['lang'] = 'de';
printf('De', $url, http_build_query($params));
Or even very compact:
printf('De',
$url, http_build_query(array('lang' => 'de') + $_GET));
http://php.net/http_build_query