This question already has answers here:
Get entire URL, including query string and anchor
(8 answers)
Closed 5 years ago.
Please help me out, if i have a URL like this
http://www.example.com/dashboard.php#settings
How do i get it in full cause. when I use:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
I get http://www.example.com/dashboard.php it excludes the # and the text after it, how do I go about getting everything in full like this http://www.example.com/dashboard.php#settings.
Url fragments are never sent to server side you have to use get or post and send the value to server side in order to perform some actions or you can use your hashtag but also some client-side javascript to send the hashtag value as post or get parameter to the server side.
parse_url() can parse the URL with fragments but it is useless in this case since the fragment is never sent to the server side.
if(window.location.hash) {
// fragment exists send request to server side as get or post
} else {
// fragment does't exists
}
Related
This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 5 years ago.
I am starting to build my own MVC using PHP and I am trying to get the current URL in order to create a routing system. I am following a video and the code is as follows. However, it isn't working.
<?php
// phpinfo();
echo "15";
$url = $_GET['url'];
// echo $url;
You are mistaking $_SERVER['REQUEST_URI'] for $_GET['url']
$_GET['URL'] is a get statment .. Meaning you need to pass a variable through the URL IE http://example.com?URL=myurl
To get the current URL .. Simply use echo $_SERVER['REQUEST_URI'];
$_GET will get you the get parameters, not the entire url. Use $_SERVER['REQUEST_URI'] for that.
This question already has answers here:
Get fragment (value after hash '#') from a URL [closed]
(10 answers)
Closed 6 years ago.
I am very new to PHP and would like to build a web app for my school.
I have got the oauth down, and my redirect URl is
127.0.0.1/authorize
This results in, when succesfulyl authenticated, 127.0.0.1/authorize/#access_token=thenmycodehere
Now, being new to PHP, I cannot figure out how to get the access_token variable.
If the # was a ?, then I could do it.
Is it possible?
exec("python getlinks.py " . $_GET["access_token"]);
You cannot.
Browser won't even send a request with a hash part.
Only way is just send this access_token using some clientside js code.
To somebody saying it's possible - I want to prove that it's not!
Here is the prove:
1) create hash.php file and put this contents:
<?php
echo "SERVER<br/>";
var_dump($_SERVER);
echo "<hr/>";
echo "REQUEST<br/>";
var_dump($_REQUEST);
2) go to link and check: http://num8er.me/hash.php#access_token=blablabla
I want to get the URL using (preferably) PHP or JavaScript. The page was opened using an anchor name (e.g. index.php#aboutme). When I use
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
it returns http://afterimagedesign.tk/index.php without the #home on the end. How can I get this?
PHP cannot ever get this hashtag (that's what it's called), because the browser never sends it to the server in any form.
JavaScript can access it with window.location.hash, but that's client-side.
What are the difference between server-side and client-side programming?
<?
echo parse_url("http://localhost/index.php#aboutme",PHP_URL_FRAGMENT);
?>
Output: aboutme
or with JS
window.location.hash
The only way to access this data with PHP would be to send it to PHP via an AJAX call when the page loads. As the other responses have said, the hashtag is never sent to PHP inside the original request. So you would have to send it afterwards.
If this is information you need, you would have to change your application to not use hashtags and instead append this data to the query string (ie index.php?home rather then index.php#home and manually scroll the page using javascript when it loads based on this.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can one check to see if a remote file exists using PHP?
I want to programatically check if a website is live or not. I know i can do this by opening the url using "cURL" or "fopen" but it takes a lot of time because it needs to fetch the full page.
Furthermore, this method is not reliable because there can be other reasons like unsupported protocols to be able to open the website.
Is there any other way??
You could simply use HEAD request to get only the headers of the page and not the whole page. Still, the website will still generate the full page but at least you won't download everything.
To achieve this, you can use many methods, just check how to change the headers of the request and instead of doing a GET, you can do a HEAD.
fopen() and fread() do not read the entire webpage (not necessarily anyway). You can use that and read only a few bytes to determine the website exists (200 OK).
You could just send a header request and check the http response codes?
$file = 'http://www.test.com/idontexist.jpg';
$file_headers = #get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
While using LightBox mechanism in my project I got an URL
http://nhs/search-panel.php#?patientid=2
I need to collect that patientid from this through GET mechanism, Is that possible in PHP?
Simply put: you can't! Browsers don't send the fragment (the part of the URL after the hashmark) in their requests to the server. You must rely on some client-side javascript: perhaps you can rewrite the url before using it.
Maybe everybody else is right and a simple $_GET is enough but if the # in your URL ( http://nhs/search-panel.php#?patientid=2 ) is supposed to be there you would have to do that with JavaScript (and Ajax e.g. JQuery) because everything after # is not included in the request as far as I know.
If you check your server logs, you should see that no browser actually transmits the #anchor part of the URL the request, so you can't pick it up on the server side.
If you need to know it, you'll need to write some Javascript to extract it from the document.location.href and send it to your server, either by turning it into a regular GET parameter and redirecting the user, or in the background with an XMLHttpRequest/AJAX.
Edit: Whoops, this won't work. The other posters are correct in saying that anything after the hash never reaches your server.
Something along these lines should do you:
//Get complete URI, will contain data after the hash
$uri = $_SERVER['REQUEST_URI'];
//Just get the stuff after the hash
list(,$hash) = explode('#', $uri);
//Parse the value into array (will put value in $query)
parse_str($hash, $query);
var_dump($query);