0

I need to code something like that to print value of javascript in php

<?php

$url = ( Value of <script>document.write(top.location.href);</script> );

echo ''.$url.'';

?>

Update: I need this to break frames, block proxy websites and bots so $_SERVER not useful

Please can anyone help me ?

Thank you

5
  • Why don't use php direct? echo "http://" .$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; Commented May 7, 2012 at 17:01
  • @semsem I need this to break frames, block proxy websites and bots so $_SERVER not useful Commented May 7, 2012 at 17:08
  • @COLDTOLD I don't know how to use ajax Commented May 7, 2012 at 17:08
  • You can't access one frame from another from within PHP. You also can't do it from Javascript if the frames are from different domains. Commented May 7, 2012 at 17:10
  • Why does your server not already know the URL of your client? How is your client talking to your server to make the request if it is not your URL? Are you trying to farm for the URL of another iframe? it will help to know more about what exactly you are trying to do. Commented May 7, 2012 at 17:40

3 Answers 3

3

No, that's not possible because PHP (server-side) is run before javascript (client side) code. You can do the opposite though. You will have to resort to one of these:

  • AJAX
  • Hidden Field
  • Query String

Though, if you want to read the current page URL, you could use:

$_SERVER['REQUEST_URI']
Sign up to request clarification or add additional context in comments.

7 Comments

JS server-side? did you mean client-side?
It is possible with AJAX, but +1 for your explaining
@Sarfraz thank you very much..but i don't know how to use ajax.
@masterio: Read the tutorial here: tizag.com/ajaxTutorial on how to implement it. BTW, jQuery's ajax() method can make it easy for you.
please what did you mean about "Hidden Field" ?
|
2

To do what you're doing in PHP you could do $url = $_SERVER['PHP_SELF'];

3 Comments

I need this to break frames, block proxy websites and bots so $_SERVER not useful
Then it would help if you actually told us what you were trying to do, as most of the other responses are correct pointing out the difference between client and server side code.
What do you mean "break frames" and proxies?
1

you might not be looking at this the right way. you can see the current path in the url by looking at this variable

echo $_SERVER['REQUEST_URI'];

Read More

Update:

Since you want to prevent your site for being under an iframe, you can google frame busting script, it is done entirely in javascript.

3 Comments

I need this to break frames, block proxy websites and bots so $_SERVER not useful
The you should search for a frame busting script
almost of proxy sites can break it easy..i have tried it before..thank you for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.