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.
Related
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"]);
}
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.
I made a bitly url shrinker, and I currently have a Soundcloud Javascript API that outputs a url link of a song. Im trying to shrink it using my shrinker. The shrinker works using this:
<?php echo $bitly->shorten('http://google.com'); ?> //Equals google.com in short url format
The javascript code I'm trying to implement it in is this: Ill go ahead and give you what I tried to do already, that didn't work.
Before I edited:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
After I tried:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
Any suggestions, I'm open to anything. And would love to make this work!
That has been already explained but in case you're new to this concept, there is a simplified explanation.
<?php tags in your code are processed on server before your page is sent to user's browser. Actually browser never receives those tags - they're replaced with PHP output on server and then the resulting page is sent to user.
As a result of some mistake sometimes PHP code makes into user's browser but it behaves as any other non-standard tag - content between <?php and ?> would be invisible to visitor.
JavaScript, on the other hand, operates in user browser with (in our case) what PHP has already output. When you change the page with JavaScript, it's not sent back to server - actually, server is totally unaware of that, so it can't execute the PHP code you're outputting by your JavaScript.
In order to achieve a similar result you need to send an AJAX request from your JavaScript code. It'll basically be another "page request" initiated by your JavaScript, but happening at the background with PHP output not replacing your current page, but arriving into your JavaScript code. This way your JavaScript is outputting PHP output and not PHP code, that's why it is possible.
You cannot call PHP on a string that is generated via javascript since PHP is server side and executed before JavaScript which is client side.
If you want to shorten this string, you'll have to make an ajax call to a php page that will return the shrunk url.
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.
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);