tags:

views:

55

answers:

5

I am try to add a javascript using php as follows

<?php
Header("content-type: application/x-javascript");
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")";
?>

Here i need to print my output as follows

Your IP address is: 127.0.0.1

but it is showing result like

document.write("Your IP address is: <b>127.0.0.1</b>").

i am using apache server in ubuntu.What is wrong with me. help me please...

A: 

You forgot the <script> tags.

echo "<script>document.write(\"Your IP address is: <b>" . $serverIP . "</b>\");</script>";
Pointy
i think when we are using following there is no need to add script tagHeader("content-type: application/x-javascript");but,when i try to add above it is also not working.showing same result.
Ajith
I don't believe that you can feed a server a Javascript source like that. Try setting the content type to "text/html" instead.
Pointy
A: 

As suggested by pointy, you need to wrap js code in script tags. Also you can get users' IP with PHP without resorting to JS:

echo 'Your IP address is: ' . $_SERVER['REMOTE_ADDR'];

Update:

Try this:

<?php
header("content-type: text/javascript");
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "<script>document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")</script>";
?>
Sarfraz
Thank you Sarfraz...But unfortunately actually I need to do as I mention above.
Ajith
@Ajith: see my updated answer plz.
Sarfraz
Thank you for your response
Ajith
A: 

Try header('Content-Type: application/javascript');

bogdanvursu
Thank you for your response.Extremely sorry to disturb you this is not working
Ajith
A: 

Try changing the mime content type to application/javascript

Foxtrot
Thank you for your response.I changed my mime content as you suggested.but unfortunately it is not working.
Ajith
A: 

Aside from fixing the header as suggestted above, how are you adding this to your html document? Presumably the html has a script tag, which then references this php script. And presumably that is somewhere within the body of your html....

<html>
<body>
<p>blah blah blah</p>
<script type="text/javascript" src="showipaddress.php"></script> 
<p>blah</p>
etc
rob