Retrieve the hash in the url with php?

Asked
Active3 hr before
Viewed126 times

8 Answers

retrieve
90%

Or in "old" PHP you must pre-store the exploded to access the array:,If you need to parse the actual url of the current browser, you need to request to call the server.,By default, redirecting to an HTML anchor doesn't work, because mod_rewrite escapes the # character, turning it into %23. This, in turn, breaks the redirection.,Find centralized, trusted content and collaborate around the technologies you use most.

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

$url = parse_url("http://domain.com/site/gallery/1#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
load more v
88%

Or in "old" PHP you must pre-store the exploded to access the array:,If you need to parse the actual url of the current browser, you need to request to call the server.,You need to parse the url first, so it goes like this:,A. setup your strings with a div id, so the name anchor goes where its supposed to and the javascript can change the text colors

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

$url = parse_url("http://domain.com/site/gallery/1#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
load more v
72%

This function parses a URL and returns an associative array containing any of the various components of the URL that are present. The values of the array elements are not URL decoded. ,parse_url — Parse a URL and return its components, Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when PHP_URL_PORT is given, in which case the return value will be an int). , If the component parameter is omitted, an associative array is returned. At least one element will be present within the array. Potential keys within this array are:

http: //example.com/foo → query = null, fragment = null
   http: //example.com/foo? → query = "",   fragment = null
   http: //example.com/foo# → query = null, fragment = ""
   http: //example.com/foo?# → query = "",   fragment = ""
load more v
65%

We all know that php can read the url values of query string variables using $_REQUEST but foi yuo need to get the values of hash, PHP can’t really help. Some suggest to use parse_url() function but it can’t get the fragment values. Reason for this is that the browser won’t even send a request with a fragment part. The fragment part is resolved and remains in the browser, i.e. on client side.,And since it’s on the client side, hash variables from the url are accessible using JavaScript. Once inside the Javascriot, we can easily send them in the form of query string to the php script to be retrieved using $_REQUEST.,Get parameter values from URL string of the current page that was constructed and sent by PHP script.,An example of hash variables in url: http://www.domain.com#image=some.jpg&status=success

// Read a page’s GET URL variables and return them as an associative array.

 function getHashUrlVars() {
    var vars = [],
       hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
       hash = hashes[i].split('=');
       vars.push(hash[0]);
       vars[hash[0]] = hash[1];
    }
    return vars;
 }

 // Get all URL parameters
 var allVars = getHashUrlVars();
load more v
75%

I’ve been dealing with this issue also. My goal is to create a single user session environment that can be linked to each page, with dynamic meta data, social sharing and Google SEO crawls. What I found is that hash tag urls don’t allow this, no server side detection of the hash tag url. Noted above.,What I found was a html5 technique, that so far, is doing exactly what i needed. Rather than reading the url with jquery address, change the url using pushState.,Granted, this solution has issues with older browsers. But, I foresee this as the way to go moving forward.,Is there anyway to get the full URL of the current page with hashtag included?

echo $this - > EE - > input - > server('REQUEST_URI');
load more v
40%

https://site.com/path/file.php?p=123

< ? php
// (A) PROTOCOL
echo isset($_SERVER['HTTPS']) ? "https://" : "http://";

// (B) HOST
echo $_SERVER['HTTP_HOST'];

// (C) PORT
echo $_SERVER['SERVER_PORT'];

// (D) PATH + FILE + QUERY
echo $_SERVER['REQUEST_URI'];

// (E) QUERY
echo $_SERVER['QUERY_STRING'];
print_r($_GET);
load more v
22%

someone here in SO told me that the hash (#) in the url could be retrieved by php function parse_url? is this true?,my web site got a lot of ajax effects and i want to be able to pass an url with a hash to the index.php so it could display the information based on the hash values.,EDIT: im very confused..if i use the parse_url i have to type the url myself in php to get the hash? what is the point then...my question if whether my pal can enter the url with # values and php could process it? it seems like a no, am i right?,You'll need to be careful that you use the exact same input to both functions, but otherwise it shouldn't be too painful.

parse_url() can get the hash from a url string. Note the signature:

mixed parse_url(string $url[, int $component = -1])

You must already know the string. For instance, from the docs:

< ? php

$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);

?
>

Outputs

Array
   (
      [scheme] => http[host] => hostname[user] => username[pass] => password[path] => /path [query] => arg = value[fragment] => anchor
   ) /
   path
load more v
60%

Thanks for contributing an answer to Craft CMS Stack Exchange!,Please be sure to answer the question. Provide details and share your research!,Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.,It depends on what you mean by "get" the hash anchor, but I'm assuming you're trying to get it in either PHP, Twig, or JavaScript.

It depends on what you mean by "get" the hash anchor, but I'm assuming you're trying to get it in either PHP, Twig, or JavaScript.

It is physically impossible to get the #anchor part via PHP, since PHP never has access to that information. This SO thread describes the issue.

#anchor
load more v

Other "retrieve-undefined" queries related to "Retrieve the hash in the url with php?"