0

I have url like this http://localhost/project/test/value#value2. when i hit this url i want to fetch #tag value on server. I tried like this on server side

    <?php 
   function test(param){ ?> 
       <script> 
          var className = window.location.hash;  
          alert(className);
       </script> 
   <?php 
    } ?>

this alert giving me class name but after page load if i assign this value to php variable its not working. I m using codeigniter so i tried this current_url() function and also i tried $_SERVER['REQUEST_URL'] but its not working in my case

Sanjuktha
  • 1,065
  • 3
  • 13
  • 26
Dexter
  • 1,804
  • 4
  • 24
  • 53
  • try defining the value inside `test` to a variable and then returning the variable. – mega6382 Nov 18 '15 at 06:54
  • This question already discussed here http://stackoverflow.com/questions/940905/can-i-read-the-hash-portion-of-the-url-on-my-server-side-application-php-ruby – Karan Nov 18 '15 at 07:03

4 Answers4

1

The problem here is that browser did not send #tag during request, so you don't have it at server side in any url get function, only option for you is process that #tag in js side and load necessary html part through ajax

Armen
  • 4,064
  • 2
  • 23
  • 40
0

Please refer this

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Community
  • 1
  • 1
M3ghana
  • 1,231
  • 1
  • 9
  • 19
0

Use the parse_url function.

$url = 'http://localhost/project/test/value#value2';

$tokens = parse_url($url);

print_r($tokens);

/*will output
Array
(
    [scheme] => http
    [host] => localhost
    [path] => /project/test/value
    [fragment] => value2
)
*/

The fragment bit is what you are after.

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
  • its not working becoz i m not getting that url . its server side so if i try current_url() function of CI its not giving me that url with hash tag – Dexter Nov 18 '15 at 06:45
  • what does `current_url()` return? – Alex Andrei Nov 18 '15 at 06:46
  • try this `print current_url() . $_SERVER['QUERY_STRING'];` – Alex Andrei Nov 18 '15 at 06:48
  • oh wait, you can't access the anchor (the hash and everything else) from php, it doesn't get passed over, see this http://stackoverflow.com/questions/967649/get-entire-url-including-query-string-and-anchor – Alex Andrei Nov 18 '15 at 06:52
0

You can't get hash tag from url in php. Because browser does not send #tag during processing request.

Arvind Jaiswal
  • 442
  • 5
  • 14