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

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.

Related

Unable to get query parameters from laravel url get request

this is my url and i am unable to get the query parameters i dont know why.
anyone can tell me whats going on?
http://localhost:8000/senders/verify#/activate?user=qaisar72477247#email.com&option=AccountActivated
if I use
http://localhost:8000/senders/verify?user=qaisar72477247#email.com&option=AccountActivated
I can successfully get the varaibles but after putting #/activate i can't. i dont know why.
Because paths are not the same. So your route file cannot run from same line.
for this
http://localhost:8000/senders/verify#/activate?user=qaisar72477247#email.com&option=AccountActivated
First part => senders
Second part => verify#
Third part => activate
And these are the params => user=qaisar72477247#email.com&option=AccountActivated
For this one:
http://localhost:8000/senders/verify? user=qaisar72477247#email.com&option=AccountActivated
First part => senders
Second part => verify
Params => user=qaisar72477247#email.com&option=AccountActivated
didnt have the third part.
The issue is the order of the URL. The # is an anchor or fragment component, and is the last part of the URL. The query string should come before the fragment component, as anything after that will be ignored.
scheme:[//authority]path[?query][#fragment]
src: https://en.wikipedia.org/wiki/URL#Syntax
I'm guessing you're using some sort of framework such as Vue, Angular, or React, which means anything after the # is actually handled by that, and is not passed to Laravel. You'll need to manage the request with whichever framework you're using.
If you want to get the value after the hash mark (#) as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in $_SERVER["REQUEST_URI"] or similar predefined variables). You would need some sort of JavaScript magic on the client side, e.g. to include this value as a POST parameter.
So when you call :
localhost:8000/senders/verify#/activate?user=qaisar72477247#email.com&option=AccountActivated
Your server get this :
localhost:8000/senders/verify
The browser delete everything from the hash (#) of your url, then it pass to the server http process

How to access url after # in 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

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.

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