Php get url hash

Get the actual url HASH in PHP

I know that this is impossible via PHP supervars, but see this website: When we navigate on the pages, the title and meta just changes and this affect the facebook too, see: How he is doing it? I have a ajax navigation system too, and need to set og, but I don’t know how to do dinamically as he did. Just a comment, he have hidden INPUTS with values of the meta: But I still don’t know how he can parse this before the website being rendered.

2 Answers 2

The URL fragment is NEVER sent to the server. It is used as a reference by the browser. «Traditionally» it is used to scroll to an element with the ID referenced by the fragment, however more recently it has been given some more exotic uses.

In particular, #! is a shebang, and has the meaning that «this page is being loaded by AJAX, but if you load the following relative to the domain, you will get the full page anyway» — this is particularly useful for search engines.

Читайте также:  Css centre align form

Basically, use AJAX combined with location.hash to get it to work.

I’m not sure what you mean. Facebook is recognising the shebang and retrieving the appropriate URL, which contains the relevant meta tags — possibly through PHP’s echo function.

novomp3.com.br/?_escaped_fragment_=show&id=60 I have a aproppiate url too, the google simulator get the url perfectly, but the facebook doesn’t read as this is. See the code of this page and see there: developers.facebook.com/tools/debug/og/…

  

as you can see he returns false on clicks and acts based on the hashtag value

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Источник

Forum

like this url example but it does not skip to the page part .

I tried two things in my document

You cannot get the #hash part from url in php, because that value isn’t send to server.
It can be used in javascript with: window.location.hash

could you give any example because i try for 5 hours now without any succes
It works if i put the numbes #123 manual after page load but it not works on page load

Do you want to put the #hash in javascript or in php?
What did you try?
In php you cannot get the #hash from current page url, unless it is send with javascript via ajax.

In js code you can get the #hash from current page address like this:

  

I tried some options mostly if not working I delete it
but what |I try I cant get after page load skip
to the has-id in php or with a javascript

Ow yes I do not known anything about javascript
if I do something it is lucky-shots

It must be something like this
home/read.php?id=833&title=klant-is-koning#494

I saw also many articles about hash but did not saw any
url with .php?id111#111 or something like

  

Источник

retrieve window.location.hash url in php

I am using a jquery tabbed interface here http://www.imashdigital.com/#2 and would like to return the tab number in php. Ideally I would like to run a javascript function (on a timer) that continually updates a global php variable with the current tab. Based on this php value, 1 through to 4, I will then load a different sidebar. I would be grateful for any help and some code examples as I am a novice. Kind regards Jonathan

3 Answers 3

The part of an URI that comes after the hash is never sent to the server. There is no way that PHP can access it. Use a querystring parameter ( $_GET ) instead. Or use client side scripting (javascript).

uhm, not sure if you are the one that downvoted me, but the hash goes through just fine using my method

PepperBob: I meant client side scripting alone. Paolo’s example stores the state at the server side. I guess the real question is why Jonathan wants to know the selected tab in PHP?

I would suggest you do not run a timer but instead attach the $.post to the event «tab activation». This will make any tab change applied in real time and it won’t trigger needless requests.

I have used tabbed panels in a couple recent projects, and the solution I’ve used is the following:

// the currently selected tab, or a default tab (don't forget to prepend the #) var tab = location.hash || '#en_en'; // register a click handler on all the tabs $('ul.tabs a').click(function(event)< event.preventDefault(); // prevents the browser from scrolling to the anchor // hide all panels, then use the link's href attribute to find // the matching panel, and make it visible // you can, of course, use whatever animation you like $('div.panel').hide().filter( $(this).attr('href') ).show(); ).filter('[href*='+tab+']').click(); // above: in case of refreshing/bookmarking: find the tab link that contains // the current location.hash, and fire its click handler 

It works well because the server-side code doesn't need to know which tab is selected, but it also supports refreshing or bookmarking a specific tab without requiring the user select the tab again.

Источник

Get fragment (value after hash '#') from a URL [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

How can i select the fragment after the '#' symbol in my URL using PHP?
The result that i want is "photo45". This is an example URL:
http://example.com/site/gallery/1#photo45

PHP cannot obtain a complete URL, including the anchor name. Since anything JavaScript and the DOM can manipulate might need to be processed by PHP, it doesn't make sense that PHP cannot obtain the entire URL with which it is called. Seems to be a very big bug in PHP, unless I'm missing something.

10 Answers 10

If you want to get the value after the hash mark or anchor 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.

If it's only about parsing a known URL from whatever source, the answer by mck89 is perfectly fine though.

just a note: window.location.hash gets you the whole fragment INCLUDING the # [hash] sign. and it is, of course, in JavaScript 🙂

part of your answer is no longer correct in Chrome chromestatus.com/feature/4753419730419712 and Firefox bugzilla.mozilla.org/show_bug.cgi?id=1264178 both do send this to the server

That part is called "fragment" and you can get it in this way:

$url = parse_url("http://example.com/site/gallery/1#photo45 "); echo $url["fragment"]; //This variable contains the fragment 

That is very good but it would have been without any error if you were to add ``` $fragment = isset($url['fragment']) ? '#' . $url['fragment'] : ''; ``` But still Thumbs Up !!

This is true but not the correct answer for original question. The asker doesn't have the full URL string.

The asker didn't specificallt say he hadn't the URL to be honest or that he needed the fragment from the URL currently displayed in the address bar of the browser. He asked how to parse the fragement from an URL, so unless he edited his question, this answer should fine enough.

Actually you can get the fragment in one line: $fragment = parse_url("http://example.com/site/gallery/1#photo45", PHP_URL_FRAGMENT);

A) already have url with #hash in PHP? Easy! Just parse it out !

if( strpos( $url, "#" ) === false ) echo "NO HASH !"; else echo "HASH IS: #".explode( "#", $url )[1]; // arrays are indexed from 0 

Or in "old" PHP you must pre-store the exploded to access the array:

$exploded_url = explode( "#", $url ); $exploded_url[1]; 

B) You want to get a #hash by sending a form to PHP?
=> Use some JavaScript MAGIC! (To pre-process the form)

var forms = document.getElementsByTagName('form'); //get all forms on the site for (var i = 0; i < forms.length; i++) < //to each form. forms[i].addEventListener( // add a "listener" 'submit', // for an on-submit "event" function () < //add a submit pre-processing function: var input_name = "fragment"; // name form will use to send the fragment // Try search whether we already done this or not // in current form, find every var hiddens = form.querySelectorAll('[name="' + input_name + '"]'); if (hiddens.length < 1) < // if not there yet //create an extra input element var hidden = document.createElement("input"); //set it to hidden so it doesn't break view hidden.setAttribute('type', 'hidden'); //set a name to get by it in PHP hidden.setAttribute('name', input_name); this.appendChild(hidden); //append it to the current form >else < var hidden = hiddens[0]; // use an existing one if already there >//set a value of #HASH - EVERY TIME, so we get the MOST RECENT #hash :) hidden.setAttribute('value', window.location.hash); > ); > 

Depending on your form 's method attribute you get this hash in PHP by:
$_GET['fragment'] or $_POST['fragment']

Possible returns: 1. "" [empty string] (no hash) 2. whole hash INCLUDING the # [hash] sign (because we've used the window.location.hash in JavaScript which just works that way 🙂 )

C) You want to get the #hash in PHP JUST from requested URL?

YOU CAN'T !

. (not while considering regular HTTP requests).

Источник

Оцените статью