Call External Javascript using php
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.writ开发者_开发百科e("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...
Try with the following code
<?php
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "<script>document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")</script>";
?>
I tried it at my end and it worked
Try header('Content-Type: application/javascript');
Try changing the mime content type to application/javascript
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>";
?>
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
You forgot the <script>
tags.
echo "<script>document.write(\"Your IP address is: <b>" . $serverIP . "</b>\");</script>";
精彩评论