Jump to content

function question


elmas156
 Share

Recommended Posts

I have a question about functions and what is the benefit of using a function over multiple if/else statements.  For example:

 

What would be the difference in using this function:

 

<?php

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

?>

 

or just using this code:

 

<?php

    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }

?>

 

With the first example, I try to echo the ip address ($ip) to the page and nothing happens.  With the second, I echo $ip and the ip address is shown on the page.  Why?  Am I doing something wrong or taking a shortcut that shouldn't be taken?

 

This may seem like a silly question but I'm trying to figure this out and sometimes it's easier to just use multiple if/else statements to accomplish my task rather than an actual function.

 

Thanks for your help.

Link to comment
Share on other sites

If you're only using the chunk of code once, then making it into a function is not really worth it. If you're using the same chunk of code more than once and function is preferable.

 

How are you using the function when the echo doesn't work.

 

Ken

Link to comment
Share on other sites

Basically, I am recording a user's ip address when they use my site and take an action that inserts information into my database.  When I use the function as shown and use $ip where I want the ip address displayed on the screen or inserted into the database, nothing happens.  When I use the second example above and I use $ip to display or insert the ip address, everything works fine... not sure what I'm doing wrong.

Link to comment
Share on other sites

To use the function, you need to do something like this:

<?php
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ip = getRealIpAddr();
echo $ip;
?>

 

How are you using it?

 

Ken

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

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.