-1

Am trying to get the current URL that contains a hash e.g (http://www.example.com/index.php#folder23242%FNew). Anything after the hash the variable must store it in PHP variable. I have added the script but the PHP does not recognize the Javascript variable value and prints no value. I need it to take the Javascript variable so it can execute my php script.

echo '<script>
var url = ""; 
url = location.hash;
url = decodeURI(url);
document.cookie="currenturl=url";
</script>';
$url = "";
$url = $_COOKIE["currenturl"];
$arr = explode('/', $url);
$url = end($arr);

echo "<script>alert('".$url."')</script>";
saiyan101
  • 610
  • 4
  • 12
  • 28
  • PHP is Server Side and JS is client side. JS will always execute (on client side) after PHP(on server side). How are you expecting this to work. – Umair Khan Nov 20 '19 at 06:08
  • PHP does not get URL that values that contain a # even if you USE "REQUEST URI" etc hence the use of Javascript. Unless there is a to? – saiyan101 Nov 20 '19 at 06:14
  • Does this answer your question? [Get fragment (value after hash '#') from a URL in php](https://stackoverflow.com/questions/2317508/get-fragment-value-after-hash-from-a-url-in-php) – Aaron Yordanyan Nov 20 '19 at 06:25

2 Answers2

1

Impossible to get hash from current url.

$_SERVER['REQUET_URI'] only contains path and query string.

The browser does not send the fragment to the server.

Answer is 'Standard methods will not help you."

Aaron Yordanyan
  • 738
  • 7
  • 19
0

You can set it in your cookie with document.cookie="currenturl=location.toString()";

Then access it from your php code,

$url = $_COOKIE["currenturl"];
echo parse_url($url)["fragment"];

Example:

php > print_r(parse_url("https://example.com/#folder"));
Array
(
    [scheme] => https
    [host] => example.com
    [path] => /
    [fragment] => folder
)

LF00
  • 27,015
  • 29
  • 156
  • 295