Search code examples
phpipbackenddom-manipulation

Display Return Value in HTML


I have to create a tool like (whatsmyip.com) but with special filters or so, I have to implement this code into a html site but don't know how to echo the returned value in html.. Thanks for your help

    public function getClientIp() {
        if ($this->clientIp) {
            return $this->clientIp;
        }
        $ip_keys = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'];
        foreach ($ip_keys as $key) {
            if (isset($_SERVER[$key])) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    // trim for safety measures
                    $ip = trim($ip);
                    // attempt to validate IP
                    $filterOptions = FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
                    if (filter_var($ip, FILTER_VALIDATE_IP, $filterOptions) !== false) {
                        return $ip;
                    }
                }
            }
        }
        $this->clientIp = $_SERVER['REMOTE_ADDR'] ?: false;
        return $this->clientIp;
    }

i need somehing like this do display the ip

<h1>Was ist meine IP-Adresse?</h1>
<p>Hier sehen Sie, welche IP-Adresse Sie derzeit für Ihre Internetverbindung nutzen.</p>
<br /><br /><div style="background-color:lightgray; padding:10px;">{source}<span style="font-family: courier new, courier, monospace;">&lt;?php echo "Ihre IP-Adresse lautet: $clientIp" ?&gt;</span>{/source}
</div>

Solution

  • You simply need to echo the result of the function which returns the IP address:

    echo getClientIp();
    

    You also need to amend the function - I guess you took this code from somewhere else without really understanding it (which is rarely a good idea). It's clearly meant to be part of a class, hence the public and all the references to $this. I've changed it below so it will work as a standalone function.

    Full example:

    <?php
    
    function getClientIp() {
        $ip_keys = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'];
    
        foreach ($ip_keys as $key) {
            if (isset($_SERVER[$key])) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    // trim for safety measures
                    $ip = trim($ip);
                    // attempt to validate IP
                    $filterOptions = FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
                     if (filter_var($ip, FILTER_VALIDATE_IP, $filterOptions) !== false) {
                        return $ip;
                     }
                 }
            }
        }
    
        $clientIp = $_SERVER['REMOTE_ADDR'] ?: false;
        return $clientIp;
    }
    ?>
    
    <h1>Was ist meine IP-Adresse?</h1>
    <p>Hier sehen Sie, welche IP-Adresse Sie derzeit für Ihre Internetverbindung nutzen.</p>
    <br /><br />
    <div style="background-color:lightgray; padding:10px;">
      {source}
        <span style="font-family: courier new, courier, monospace;">
          Ihre IP-Adresse lautet: <?php echo getClientIp();?>
        </span>
      {source}
    </div>