echo the output from document.write

cunoc
cunoc used Ask the Experts™
on
Hi all Experts,
I can get the result from PHP Script with $_SERVER['HTTP_USER_AGENT'] but I want to know if I can  (echo) the output (navigator.appName) and (navigator.appVersion) from the (document.write...). Thanks in advance for any helps from all of you.

----

<?php

echo '<script language="Javascript">
<!--
   // document.write("Here is the result: "+navigator.appName+" "+navigator.appVersion+"");

$Result = 'navigator.appName'+'navigator.appVersion';
//-->
</script>';


$file = fopen("log.txt", "a");

fwrite($file, "$Result\n");

echo $Result;

fclose($file);

?>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
The order of processing is that the PHP code completely generates the document, then sends it down to the browser.

At the browser, the client end renders/draws/displays the data, and fires any javascript code.

By the time the browser sees the page, the php code is done and finished, so it cannot "echo" anything from the client side, such as the Javascript code output/variable.

Since you can parse it from the user agent, just stick with that.
Shinesh PremrajanEngineering Manager
Commented:
you can try placing that in a hidden variable and when the form submits then you have both the variable available.

Sine the PHP is executed prior to the javascript, its not possible at all to pass that value directly to the PHP variable, as you have mentioned.

hope this helps

Author

Commented:
Hi Experts,

I tested the below PHP Script and here is the output from the (log.txt)
---

<script type="text/javascript">
document.write("Here is the result: "+navigator.appName+" "+navigator.appVersion+"");
</script>  | July 1, 2010, 1:05 pm

---

And here is the out put of the (echo $str;)

Here is the result: Microsoft Internet Explorer 4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)

---

<?php

$str = <<<Output
<script type="text/javascript">
document.write("Here is the result: "+navigator.appName+" "+navigator.appVersion+"");
</script>
Output;

echo $str;

$file = fopen("log.txt", "a");
$time = date("F j, Y, g:i a");

fwrite($file, "$str  | $time\n");

fclose($file);

?>
----
The above PHP Script seems to work with the ( echo $str; ) and not with ( fwrite($file, "$str  | $time\n"); ), so how I can fix that problem... Thanks
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Shinesh PremrajanEngineering Manager
Commented:
actually what you are echoing is not the  result, just view the source code you will convince on that.

Actually your echo is printing the javascript code itself and not the the browser name

hope this helps
Currently you seem to confuse the serverside with the client side code.

PHP is a serverside code, all execution is done on the server.
JS is a clientside code, all execution is done on the client.
The client can only begin to process Javascript when php is finished and has sent the result to the browser.

Using a result from a Javascript in the same request is therefore not possible with php.

You could use a seperate request (like with ajax) to store this data on server.

The reason your current script works:

$str will not be filled with the output of the javascript, but with the literal string containing the javascript. It echoes the javascript itself, wich in turn is sent to the client. The client sees the javascript, exectes is and writes the information to the screen.

during the time javascript starts to run, php has already finished it's request.

Author

Commented:
Hi all experts,
I agreed with all your advices and I have learned something from you guys. Thank you very much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial