Jump to content
xisto Community
Sign in to follow this  
Jesse

Display 'from Ip Address' On Web Page PHP Code Help

Recommended Posts

I need some help with adding a PHP Code into my Website. I need to get a code to add in someones IP Address.Example: From IP: (IP Address Here)I also need to get a code to add how long it took to load the page at the bottom of each page.Any help with this and I would be extremly grateful, thanx heaps.

Share this post


Link to post
Share on other sites

I assume that you are running the page through the php parser already? Either by using a ".php" file extension or by modifications to the .htaccess file as required.

The method required to post the page generation time (not the load time) is to include a section of php code at the top of the page which creates a start time and another section of code at the bottom which creates a stop time. To find the page generation time is simply a matter of subtracting one from the other and echoing it to the html page. Here is some code for that:

<?php $time_start = microtime (true); ?> // place early on the page::<?php // place this code late in the page$time_end = microtime (true); $load_time = $time_end - $time_start;echo "Page Generated in $load_time seconds";?>
This code works as is for php5 only. If your server is running php 4, there is a function available on the php site that will work for the earlier versions. Search their site ( http://www.php.com/ ? ) for the microtime() function and it is explained in the first example box.

To display the User'$s IP address, you reference the Global values found in the "$_SERVER" array. The complete list of information that is available in the Array can be found by printing the contents of the array using the following code:

<?php print_r($_SERVER);

But of course, you only want one element of the array, so rather than displaying all the data, use the following bit of code on the page where you want the IP address displayed:
<?php echo "Your IP Address is $_SERVER[REMOTE_ADDR]";?>

Hope this helps.

Share this post


Link to post
Share on other sites

Yes it does help. The IP Address works fine.However the page generation time is way too long ... it is now coming out looking like this:Page Generated in 0.00013999999999997 secondsIs there anyway to shorten to this say:Page Generated in 0.00013 secondsThanx in advance

Edited by Jesse (see edit history)

Share this post


Link to post
Share on other sites

$load_time is the variable that stores the timing, which in your case is "0.00013999999999997". You can always treat that variable as a string and trim unwanted characters.

 

I recommend the following code, using substring.

$load_time = substr($load_time, 0, 6)
You can also look into this page for information and usage of the substr() function.

http://de1.php.net/substr

 

You code will eventually be:

<?php // place this code late in the page$time_end = microtime (true); $load_time = $time_end - $time_start;$load_time = substr($load_time, 0, 6)echo "Page Generated in $load_time seconds";?>

Share this post


Link to post
Share on other sites

Thanks for the assistance there Inspiron. I'm sure Jesse will be thankful.Another example of the level of assistance which is available around here. 24 / 7.

Share this post


Link to post
Share on other sites

My apologies...

I've forgotten to close that code with a ";" character.

$load_time = substr($load_time, 0, 6);
Add a ";" at the end of this line.
It should work now.. :(


Thanks for the assistance there Inspiron. I'm sure Jesse will be thankful.Another example of the level of assistance which is available around here. 24 / 7.

Haha thanks. Actually you answered her question. I'm just giving the support for shortening the length of the output timing text.
That's where my sig comes into it's use.. "Inspiron, At Your Service".. :(
Edited by Inspiron (see edit history)

Share this post


Link to post
Share on other sites

Well, I didn't catch that either when I looked at it. Glad it all worked out. That's a problem when trying to solve these sorts of difficulties without the code. Sometimes it just blows up in your face. If you would've had the code available, you would be able to test it beforehand.

Share this post


Link to post
Share on other sites

Jesse, (and others),

Here is the php code for php versions earlier than php5. I found it in a Tutorial written by Albus D, so I am quoting it here instead of using code tags.

<?php
$starttime = explode(' ', microtime());
$starttime = $starttime[1] + $starttime[0];

?>

<?php

$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;
printf('Page loaded in %.3f seconds.', $totaltime);

?>


And the reason it failed when you tried it is because the "printf()" function formats the output. The "%.3f" defines the format as floating point decimal using 3 digits and was not displaying the number correctly because your page generation times were too fast (small). Had it been formatted as "%.6f", it would have worked better for you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.