How to access url after # in php - php

I am trying to pass some parameters after # in the url like http://developer.rohitkhatri.com/test.php#embed=sdkhfjshdkfhhjk, But I don't know how to access it, I tried many solution from the stackoverflow, here are some examples what I've tried:
$_SERVER['REQUEST_URI'] gives me /test.php
$_SERVER['QUERY_STRING'] gives empty string
$_SERVER['HTTP_REFERER'] gives empty string
also tried printing the whole $_SERVER array but I did not find anything useful.
Any help is appreciated.

Well, there's no way to achieve this, because the part you are trying to access using the php, never goes to the server, what you can do is, just grab the part using the javascript and send it the the server.
Like there can be a middle page, which will redirect to the final url, and while redirecting, It can grab the part after # and send it using ajax.

The browser doesn't send anything that comes after the hash(#) to the server because it is resolved within the browser. You can try by mentioned code.
$hash = '<script>document.write(document.location.hash)</script>';
echo $hash;
output :
//#embed=sdkhfjshdkfhhjk

Related

Obtain browser url value in php

How to get address in the browser using php.
I want a way in which I can fetch the url value that is present in the browser. If I manually add a #tag to the existing url then I want to retrieve that as well.
I have used this code till now, but I want to retrieve https or http whatever value is in the browser.
Also this is my url:
http://example.com/xyz/?p=65
but suppose I build up the 2nd url manually then I would like to retrieve that as well
http://example.com/xyz/?p=65#fsgsg
$Path=$_SERVER['REQUEST_URI'];
echo $URI= 'http://'.$_SERVER['SERVER_NAME'].$Path;
The part behind the # is not delivered to the browser. You could however run a tiny javascript that sends you that information since it is available to the DOM (But do you really want that?) via the window object.
For getting has parameter,use below --
$url = 'http://amitbera.com/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
More details in http://www.php.net/manual/en/function.parse-url.php
Also,For gettting arg value use $_SERVER['QUERY_STRING']
Your only option is to handle that parameter in javascript because the # (hash) part wont get sent to the backend side, You can just detect click of the target element in JS and then glue the # part as a parameter like '&hashValue=fsgsg'.
I hope that helps You in some way.

Get URL variable in PHP

I m in a situation where i am redirecting user to another page with following jQuery code
window.location = "/#/customer/email?isEmail=true&eid=1&template=2";
i have some url re-writing , and so complete url becomes is
https://demo.qa.com/#/customer/email?isEmail=true&eid=1&template=2
but in PHP when i try to get full page url using this
echo $_SERVER['REQUEST_URI'];
it just gives me this
/
i just want to get variable IsEmail
$_GET['IsEmail']
value in PHP page,
I think the
#
in between the URL is creating the problem, is there any way to get it, please advise..
The fragment is never sent to the server, so if you want access to the query parameters you need to bring them forward:
https://demo.qa.com/?isEmail=true&eid=1&template=2#/customer/email
^ ^
query fragment
The anchor fragment portion of the URL (anything after #) isn't sent to the server at all. It only lives client-side. The server has no knowledge of it, and therefore PHP has no knowledge of it.
If you want to do anything with the anchor fragment, you must do it client-side.

PHP $_GET not reading my AJAX URL correctly

I'm not sure what the issue is, but my guess is that the $_GET variable won't read my ajax URL correctly, because of my deep linking plugin.
The final URL looks something like:
/dashboard.php#/projectSetup.php?mode=edit&getJobNumber=2012-30
HERE is my PHP:
$getJobNumber = $_GET['getJobNumber'];
$mode = $_GET["mode"];
When I echo $mode or $getJobNumber I am not getting a result. I believe the issue has to do with the format of the URL. Notice the 2 .php files and the # in the middle.
Please let me know if anyone knows of a work around.
You're exactly right. Anything following the # in a url is considered the "document fragment", and is not sent to the server.
Do a "find" for "fragment" in The URL RFC and you'll quickly see how using # in your urls for anything else is not compatible with the internet, in general.

Need to parse weird URL (contains /#/?)

I am asked to work with a service that changes my websites url to: http:://example.com/#/?id=9
I cannot seem to be able to get the id from such URL. $_GET is empty, $_SERVER['REQUEST_URI'] only contains /.
How am I supposed to get to the params?
Things I have tried:
Zend_Debug::dump($_GET); // outputs array(0)
echo $_SERVER['REQUEST_URI']; // outputs /
Zend_Debug::dump(parse_url($_SERVER['REQUEST_URI'])); // outputs array(["path"] => string(1) "/")
I am using Zend Framework but I doubt its something to do with it.
Thanks in advance.
You can't parse that with PHP, for the simple reason that as far as the URL concerns, anything beyond the # (hash) is not part of the URL, that part must be parsed with JavaScript or a similar client side language.
window.location.hash
Will return everything past the hash (including the # character)
In short: not possible in server, go with client. (maybe post an ajax call to a server with the GET data)
Everything including and following the # is for the browser's interest only.
The server never even sees it.

How to obtain anchor part of URL after # in php

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);

Categories