Capture data after the # character in the browser URL

2

I have the following URL http://urldosite.u/home#6 that when I step into a variable, it simply overrides everything after # . And I really want to get just what this after this and this character, in the example, the number 6

I have tried in many ways and I have not achieved anything.

$sitehost = $_SERVER['HTTP_HOST']; 
$siteuri = urldecode($_SERVER['REQUEST_URI']); 
$sitescript = $_SERVER['SCRIPT_NAME']; 
$siteparametro = $_SERVER['QUERY_STRING']; 
$siteprotocolo = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === false) ? 'http' : 'https'; $siteurl = $siteprotocolo.'://'.$sitehost.'/';
    
asked by anonymous 24.07.2014 / 19:29

2 answers

1

You will never be able to use PHP to capture the hash as they are not passed through the browser. Hashes are not passed to the server, they are interpreted by the browser only as 'location' on the page.

  

link
  In practice you will move the window to the ID element: 6


You will only be able to catch using js. See if the link helps: link

  

link
  Using JS to capture the hash, you will can load the clients page

    
24.07.2014 / 23:01
3

With parse_url () you get this and many other information about a given URL:

<?php

$url = 'http://urldosite.u/home#6';

print '<pre>'; print_r( parse_url( $url ) );

This small fragment would return you:

Array
(
    [scheme] => http
    [host] => urldosite.u
    [path] => /home
    [fragment] => 6
)

You need the fragment index. If the other information does not need to be used in any other way, you can enter as a strong argument to function the PHP_URL_FRAGMENT native constant.

In this way, only the fragment value will be returned. In the example, number 6.

In this particular case, if you do not have the desired part in the input URL, you would get a NULL , which does not happen without the second argument because at least the scheme and host .

Unless the URL is poorly constructed, in which case the function would return FALSE .

On request from the topic author, a slightly more extended example. Look what a bozinho face I am: P:

$url = 'http://urldosite.u/home#6';

$fragment = parse_url( $url, PHP_URL_FRAGMENT );

if( $fragment !== FALSE && ! is_null( $fragment ) ) {

    // Faz alguma coisa com $fragment
}
  

I do not have much information on how parse_url () considers a URL to be badly worded. That's the only reason I've made conditions instead of one.

    
24.07.2014 / 19:36