PHP Classes

Overkill

Recommend this page to a friend!

      PHP IP Address of User  >  All threads  >  Overkill  >  (Un) Subscribe thread alerts  
Subject:Overkill
Summary:It can be done in 6 lines
Messages:4
Author:Michael Richey
Date:2014-03-03 22:13:44
Update:2014-03-04 15:27:22
 

  1. Overkill   Reply   Report abuse  
Picture of Michael Richey Michael Richey - 2014-03-03 22:13:44
This is OOP for the sake of OOP. There is no reason to make something so simple into an interface. And why use isset() and empty() when you can kill 2 birds with one getenv()?

In PHP 5.3 or greater, it is this easy:

$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');

Prior to PHP 5.3, it is this easy:

$ip = getenv('HTTP_CLIENT_IP')?getenv('HTTP_CLIENT_IP'):
getenv('HTTP_X_FORWARDED_FOR')?getenv('HTTP_X_FORWARDED_FOR'):
getenv('HTTP_X_FORWARDED')?getenv('HTTP_X_FORWARDED'):
getenv('HTTP_FORWARDED_FOR')?getenv('HTTP_FORWARDED_FOR'):
getenv('HTTP_FORWARDED')?getenv('HTTP_FORWARDED'):
getenv('REMOTE_ADDR');

Pass the result to define() and you've provided user IP everywhere.

  2. Re: Overkill   Reply   Report abuse  
Picture of Archzilon Eshun-Davies Archzilon Eshun-Davies - 2014-03-03 23:47:42 - In reply to message 1 from Michael Richey
Well nice to know but these are components from a large project am just
sharing the pieces whenever i get time. interfaces are important on large
projects and are *much* better than any documentation because at a glance
I can tell every function in the class no need to search for example
code and what not. OOP is designed for one purpose if everyone hates C so
much why is it that you are all resisting the so called OOP methodology
that you used in killing C. I do cgi in C and php if *new* school developers
would be taking over from me. I find that if projects like
joomla and wordpress used proper OOP methodologies then they've had less trouble managing bugs. with proper OOP design adding features is no longer
original dev time x 2

Am writing a book on this I'll share when am done.

PS: all the environment variables are set whether empty or not so your approach might not work all the time.

Regards
laudarch

  3. Re: Overkill   Reply   Report abuse  
Picture of Michael Richey Michael Richey - 2014-03-04 08:24:18 - In reply to message 2 from Archzilon Eshun-Davies
All of the environment variables in this test are NOT set if they aren't part of the original request. The only one that is guaranteed to be set is REMOTE_ADDR, because it's set by the server hosting the PHP script.

By the way - why do you claim that they're always set, and still use isset() in your script? If that's the case, you could skip directly to your empty() test and cut your number of tests in half.

getenv() returns false if a value isn't set, allowing for the kind of ternary expression I posted.

You should read about it:
php.net/getenv

  4. Re: Overkill   Reply   Report abuse  
Picture of Archzilon Eshun-Davies Archzilon Eshun-Davies - 2014-03-04 15:27:22 - In reply to message 3 from Michael Richey
Yeah that's true i could do that, am adding that now. i have used getenv before but only when i used php as cgi.

thanks and regards.