/    Sign up×
Community /Pin to ProfileBookmark

ipAddress script doesn’t work

Doesn’t work
The script at: [url]http://javascript.internet.com/user-details/ip-address.html[/url] works THERE,
but the script offered doesn’t work when copied.
The pop-up displays: /! Your IP address is <!–#echo var=”REMOTE_ADDR”–>
The source code has my IP address INSTEAD of the generating echo code! not HOW it was fetched. sounds like php…
var ipAddress = <!–#echo var=”REMOTE_ADDR” –>;
returns: /! Your IP address is undefined …Getting closer…
I have tried this script mounted on a web page, not just on my computer.
PHP: if mounted on a web site as *.php it returns: “syntax error”
How do I ‘define’ the IP address or write this into a web page as php script?
Can someone correct this? what is the complete correct code?
using Firefox 2.0 or Seamonkey1.1, SUSE Linux.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@felgallJan 09.2008 — That code uses a server side include to retrieve the IP address.

To do it with PHP instead of SSI replace the following in the original code

var ip = '<!--#echo var="REMOTE_ADDR"-->';

with

var ip = '<?php echo $_SERVER["REMOTE_ADDR"]; ?>';

and place it in a file with a .php extension instead of one with a .shtml extension.
Copy linkTweet thisAlerts:
@MnighthawkauthorJan 12.2008 — Thanks a lot, Stephen, for pointing me in the right direction!

Place the following code anywhere in a *.php web page and the public IP number appears like magic

<?php echo $_SERVER["REMOTE_ADDR"] ?>

- - -
Using JavaScript on a *.html web page, place [I]something like[/I] the following in the <head></head>

between <script language="text/JavaScript"> and // End --></script>

var ip = '<!--#echo $_SERVER["REMOTE_ADDR"]-->';
-


and this where you want the IP number to appear in the body of the web page:

<script type="text/javascript">

// This part is for the web page

document.write("Your IP address is "+ip);

// End -->

</script>

the "var ip = " part isn't quite right, it doesn't return the IP number. Isn't this possible with only html & JavaScript?

using phpinfo.php I see that there is a REMOTE_ADDR for both Apache and php. *.shtml page is returned as plain text.
Copy linkTweet thisAlerts:
@felgallJan 13.2008 — You can get the IP address using Java or any server side language such as PHP or SSI. Neither HTML nor JavaScript has any access to the IP address, you can only make the IP address available to JavaScript if some other language runs first to insert the IP address into the code.

To do it with a server side include (in a .shtml page) use:

var ip = '<!--#echo var="REMOTE_ADDR"-->';

To do it with PHP (in a .php page) use:

var ip = '<?php echo $_SERVER["REMOTE_ADDR"]; ?>';

Of course with the simple JavaScript you have it is easier to just get rid of all the JavaScript and embed the IP address from the SSI or PHP straight into the HTML. For example:

<p>Your IP address is <?php echo $_SERVER["REMOTE_ADDR"]; ?>.</p>
Copy linkTweet thisAlerts:
@MnighthawkauthorJan 17.2008 — thank you for your answer, I now understand why just HTML & JavaScript will not work.

The web server I am on runs FreeBSD with php (v. 4.4.6) and Apache (v. 1.3.37), Virtual Directory Support - disabled. If I compose a *.shtml page the viewable results are text only. Any explanation why? - Mnighthawk
Copy linkTweet thisAlerts:
@felgallJan 17.2008 — Does the server have Server Side Includes enabled? If SSI is disabled then that option will not work either.

Another alternative is to use Java but then you are reliant on your visitor having java installed and enabled.
Copy linkTweet thisAlerts:
@Pth1515Sep 06.2014 — [U][B]You have to do this online for it to work.[/B][/U]
Copy linkTweet thisAlerts:
@Pth1515Sep 06.2014 — Edit:
You can get the IP address using Java or any server side language such as PHP or SSI. Neither HTML nor JavaScript has any access to the IP address, you can only make the IP address available to JavaScript if some other language runs first to insert the IP address into the code.

To do it with a server side include (in a .shtml page) use:

var ip = '<!--#echo var="REMOTE_ADDR"-->';

To do it with PHP (in a .php page) use:

var ip = '<?php echo $_SERVER["REMOTE_ADDR"]; ?>';

Of course with the simple JavaScript you have it is easier to just get rid of all the JavaScript and embed the IP address from the SSI or PHP straight into the HTML. For example:

<p>Your IP address is <?php echo $_SERVER["REMOTE_ADDR"]; ?>.</p>[/QUOTE]


Does the server have Server Side Includes enabled? If SSI is disabled then that option will not work either.

Another alternative is to use Java but then you are reliant on your visitor having java installed and enabled.[/QUOTE]
Copy linkTweet thisAlerts:
@Pth1515Sep 06.2014 — Also I made a website to show the code: *** link removed by moderator as we cannot verify it ***
×

Success!

Help @Mnighthawk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...