0

I am making a basic page that displays your WLAN IP address, i have an index.php as landing page and a getip.php that is required by index.php the problem that i am having is that the page will display the last ip that saved, and not yours, until the page is refreshed. i already tried with no cache and expires but no good. my question is.. how can i make it so the php variable gets pulled on page load and not just on refresh.. Thank You.

this is my index.php:

 <?php
  require "getip.php";
  $ip = getip();
  ?>

and then just:

<p style="text-align: center; color:white;">
 Your IP address is 
<span style="color:red;"> 
<?php
  echo $ip;
?>
</span>
</p>

and my getip.php:

<?php

function getip() {
    $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';

        return $ipaddress;
         }?>
JoelContreras
  • 83
  • 1
  • 11
  • The most reliable way to get an IP address is simply $_SERVER['REMOTE_ADDR']. All those headers that you are testing for can be easily spoofed. See http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php?rq=1 – Andrei Savin Feb 19 '17 at 06:41
  • will keep in mind, thank you.. but still need an answer to my question. – JoelContreras Feb 19 '17 at 06:56
  • @AndreiSavin the way he's doing is actually is better as it deals with proxy servers and what not. For instance if you get a proxy request, you'll get the address of the proxy server, not the client actually requesting the information. – Jonathan Feb 19 '17 at 09:05

1 Answers1

3

The problem you are stating above is the normal behavior of the php web server. How do you expect the php function to give you new ip unless there is trigger to get the new one ??

You are not getting the new ip address instantly after you change the ip address is due to following fact::

  1. PHP is server side scripting which somewhat means that the actions will be performed only after the page reload.
  2. Since you do not have any methods to reload the page, you are not getting the new ip address instantly.
  3. You are getting the new ip after page refresh because the function getip() gets called when you reload your page that generates the current ip. i.e. you are manually triggering the function by refreshing your page.

There are few things you might want to try::

  1. Use html tag to refresh you page on certain interval.
  2. Use the php connection handling, it is quite interesting topic.
  3. Use manual trigger as button click to call getip() and show the new ip.
archvayu
  • 438
  • 4
  • 13