To Retrieve string followed by # in a url - php

I have a URL of the form:- http://www.sboxeppp.com:88/phones.php?iden=true#6786
Now i want to retrieve number (6786) followed by # in server side. How can i do that?

anything behind the hash can only be accessed by client side scripts, since it won't be sent to the server you can use the parse_url() function
more here: http://php.net/manual/en/function.parse-url.php

You cannot do that. The part of the url after the hash is called a fragment, and it does not get sent to the server. It's only available to client scripting.
The only way that you could do this is by retrieving the fragment from JavaScript (using window.location.hash) and communicating this information to the server with an AJAX request specifically made for this purpose. Of course this means that the server will have to render the page first and get notified of the fragment later, which is a totally different workflow than what you want.

You can't do that, because it's a directive for browser only. You can use AJAX requests to send the required info to server.

Right, it didnt let me post that as an answer -
var hashNumber = window.location.hash should work.
hashNumber = hashNumber.substring(1)
See:
How can you check for a #hash in a URL using JavaScript?

Use parse_url:
parse_url('http://www.sboxeppp.com:88/phones.php?iden=true#6786',
PHP_URL_FRAGMENT);
Notice that the fragment doesn't get sent to the server if it's in the form's target property. Instead, just write the information in the fragment in a hidden element, like this:
<input type="hidden" name="_fragment" value="6786" />
And read the fragment from $_POST['_fragment'].
If the fragment is generated client-side (or somehow determined by the user), you'll have to create that element on the client. You can access the current fragment in JavaScript with window.location.hash.

Related

URL with query string and hastag navigation [duplicate]

How to get the full URL including the string parameter after hash tag? I try to echo
$url = $_SERVER['REQUEST_URI'];
echo $url;
the string after the hash tag wont read.
Pekka's comment should be an answer. The string parameter after the hash tag is not sent to the server, it's for the browsers eyes only.
This means that serverside code (PHP, in your case) does not have this info. The clientside code (the browser, javascript, ...) does.
Ideally,
the part after the ? is info for the server. Put everything your
server needs here
the part after the # is info for the client. Put everything your
client needs here. It's called the Fragment Identifier (Thanks Tim).
Historically, the part after the # was most often used to have your browser quicky scroll to a defined anchor on the page. Nowadays, it is more often used to hold state information for the client.
You could have javascript send this info to the server, or perform different actions based on this info. AJAX is your friend.
The hash (the string including the #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI'] variable will contain the rest however.
If you really need to know what the hash is, you will have to use the document.location.hash JavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).You can pass up the full URL, including the anchor (the part after the #), using a Javascript onload function that sends that URL to an Ajax endpoint.
You can also take a look here Get entire URL, including query string and anchor
use urlencode() and urldecode() functions
In this short example, I will show you how to pass Hash value to the server and make it redirect to the hash value.
Firstly encode the Hash value in the link button
redirect to Link1
Now to redirect to the link from the server
mylink.php
if ($_GET["redirect"] != null )
{
header("location: urldecode($_GET["redirect"]);
}

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 a # from a url in php

I'm trying to code with the facebook API
here it says : http://developers.facebook.com/docs/authentication/javascript
to get the access_token thing, but it's after a # and not a ?
so how can I get it ?
http://www.example.com/callback#access_token=...&expires_in=...
You won't be able to do it in PHP - you can only access it in javascript - the fragment/hash never reaches the server, it is processed by the browser
in Javascript, you can access the fragment using
window.location.hash
The fragment identifier is only used client side. The browser doesn't send it to the server, so you can't access it with PHP.
To quote the page you link to:
Your JavaScript library can read the token from the URL and store it in a cookie for future use.
You have to use JS to read it before it can be sent to the server.

PHP Zend Framework - How to Get Request URI Fragment from Request Object?

Say e.g. i have a URI http://127.0.0.1/somecontroller/someaction#12345 that takes me to the someAction() action of the someController controller. From there, i am able to retrieve the Request object via $this->getRequest().
i am also able to retrieve various information regarding the URI from the Request object.
But, how can i retrieve the fragment (i.e. the "12345" part after the # in the e.g.)? Neither getRequestUri() nor getParams() turn up the fragment part.
Thanks!
The fragment part of the URL is never sent to the server via GET requests (or any kind of HTTP request for that matter), the only way you can get it is if you write a Javascript snippet that parses the URL and sends the fragment back to the server via Ajax for instance.
This can't be done with PHP alone.
According to HTTP protocol specification, the fragment part is ignored. However, browsers do support redirects with hash.
If you generate hashes automatically, you may pass the id as the request parameter:
http://127.0.0.1/somecontroller/someaction/id/12345/#12345
and then:
$this->getRequest()->getParam('id')
But this will hot handle the case with the hash only, e.g. when user enters the URL manually.
You cannot use:
explode("#",$_SERVER['REQUEST_URI'])
because when you call $_SERVER['REQUEST_URI'], you never get the word after #. For example your link www.example.com/about#test, and when you call $_SERVER['REQUEST_URI'], you just get www.example.com/about.
Couldn't you use the php function(s) explode("#",$_SERVER['REQUEST_URI'])? Maybe I've misunderstood the question.
I agree with Alix Axel, but there is a way, although very dirty and ugly, this can be done, IF, and only IF, you are calling it from a web browser.
<?php
if (isset($_GET['token'])) {
die(var_dump($_GET['token']));
}
?>
<form id="teste" action="?" method="get"><input type="hidden" name="token" id="token" value=""></form>
<script>
document.querySelector('#token').value = window.location.hash;
document.querySelector('#teste').submit();
</script>

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