0

I am using the following piece of Javascript code to get the link from the browser:

<script type="text/javascript">
document.write(document.URL);
</script>

How can I pass the value to PHP or get the link to use it on PHP code?

Please, that's just what I want to know, I do not want other answers about how I can get the link directly through php. I know already that there are ways to get the link through PHP directly by useing $_GET, $_SERVER but they do not get the link with the hash and I want get the value after the hash.

I.e.: http://sitename.com/t10.html#post22

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
o6 qr
  • 95
  • 2
  • 7
  • The hash is irrelevant to the HTTP Request and is actually only relevant to the browser. So you simply cannot get it using PHP. – Ali Mar 15 '14 at 15:12
  • thnx , that is not what i asked about , i ask about how i pass the value from java to php , what you say i already know it . – o6 qr Mar 15 '14 at 15:21
  • do you want a pure javascript method or are you ok with using a library like jquery? – anurupr Mar 15 '14 at 15:24
  • You can't pass values from javascript to PHP – Ali Mar 15 '14 at 15:26
  • @anurupr i think pure javascript is enough , Because i want from it get only the full link from browser , and i will use the value on PHP . THNX – o6 qr Mar 15 '14 at 15:29
  • @Chosen Wann , OH really ? i don't know that .. so how i can get all link from broswer with hash , sure there is a way to do that – o6 qr Mar 15 '14 at 15:31
  • @o6qr as far as I know, there isn't. – Ali Mar 15 '14 at 15:32
  • AJAX or just post it via a form. @ChosenWann, there is a way. – putvande Mar 15 '14 at 15:37
  • @putvande I might have found something. check my answer in a few minutes. – Ali Mar 15 '14 at 15:45
  • @putvande , okay how i get full link by ajax , i don't know much about it – o6 qr Mar 15 '14 at 15:46
  • @Chosen Wann , okay brother waiting you .. but u have to know i check this link already http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url – o6 qr Mar 15 '14 at 15:47
  • @o6qr also, do you mind me asking what are you planning to do with whatever is after the hash after you get it? how will you process it? – Ali Mar 15 '14 at 15:49
  • @Chosen Wann , okay i want to get the number of post from broswer to know if user login or see this post or not , there is a field at the database i will insert on it value 1 or 0 , 1 means user see the post , 0 means user don't see the post – o6 qr Mar 15 '14 at 16:05
  • @o6qr then why not simply set it as a parameter rather than a hashtag? `example.com/index.php?post_id=1` – Ali Mar 15 '14 at 16:07
  • @Chosen Wann , yeah that's will be very easy to use $_GET but the links like this #post99 already with hash , i cannot change it to post=99 – o6 qr Mar 15 '14 at 16:12

4 Answers4

0

Use ajax (jquery or any similar library) to post the data to your php script.

  • thnx , all answer about use ajax , but i don't know about it , how i use ajax to get full link or get what is after hash ?? – o6 qr Mar 15 '14 at 15:49
  • @sh0ber the answer does point the questioner to the right direction. –  Mar 15 '14 at 16:22
  • 1
    @Howard Pointers are nice. Answers are better. – Dan Mar 15 '14 at 18:54
0

For a solution, we'll be using a cookie to store the url in javascript then retrieve it in PHP

so here's the javascript part

<script>
    document.cookie = 'url='+document.url;
</script>

Then in PHP, simply

<?php
    echo $_COOKIE['url'];
?>
Ali
  • 3,479
  • 4
  • 16
  • 31
0
var myHash = window.location.hash.substr(1);

You can the pass it to a php page using post()

$.post("mypage.php", {hash: myHash}, function (data) {
    alert(data);
});
mathius1
  • 1,381
  • 11
  • 17
  • thnx brother , i don't understand the code so good , and i'm want pass the value var from java to php on same page , not pass to another page . – o6 qr Mar 15 '14 at 21:05
0

thnx brothers .. i found The solution to my problem After hundreds of attempts ,the closest solution to my problem by @Chosen Wann

first this code of java to get waht's after hash from the link

<script>
document.cookie = 'url='+window.location.hash;
</script>

and you can passing the value to PHP BY

$_COOKIE['url'];

if your links like

http://sitename.com/t10.html#post22

you can get the number of post

<?php
$inurl = explode("#post",$_COOKIE['url']);  
echo $inurl[1];
?>
// Value Output:  22
o6 qr
  • 95
  • 2
  • 7