Tuesday, January 13, 2009

PHP: Getting Secondary Internet Protocol (IP) Address

You'll probably get the Remote IP Address of your users by using the server variable $_SERVER['REMOTE_ADDR']. However, if your users are being proxy servers or using dynamic IP address, you won't get the actual IP Address of your users using $_SERVER['REMOTE_ADDR'].

Probably you need the following code to help you to get the actual IP address of your users.

function getIP() {
$IP = '';
if (getenv('HTTP_CLIENT_IP')) {$IP =getenv('HTTP_CLIENT_IP');}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {$IP =getenv('HTTP_X_FORWARDED_FOR');}
elseif (getenv('HTTP_X_FORWARDED')) {$IP =getenv('HTTP_X_FORWARDED');}
elseif (getenv('HTTP_FORWARDED_FOR')) {$IP =getenv('HTTP_FORWARDED_FOR');}
elseif (getenv('HTTP_FORWARDED')) {$IP = getenv('HTTP_FORWARDED');}
else {
return $IP;

How does this get the actual IP?
When servers send data to a client through a proxy, the protocol will still send the actual IP address through the environment variables in order for the proxy to be able to identify which client to send to.

You should use this abreast (side by side) with $_SERVER['REMOTE_ADDR']. This function returns $_SERVER['REMOTE_ADDR'] if there's no secondary IP address.

Pastebin: http://thephpcode.pastebin.com/f24c2ec13

Sunday, January 11, 2009

MySQL: Grouping by most occurence in Column

I was developing a Content Management System (CMS) software when I encountered this issue of displaying most occurrence. So I managed to do this:

mysql> SELECT `DesireColumn`, COUNT(`DesireColumn`) AS `occurances` FROM `table` GROUP BY `DesireColumn` ORDER BY `occurances` DESC

This query groups rows with same value in `DesireColumn`, then sorts the result by the number of occurances. It's useful to me, and I hope that it's useful to you too.

Saturday, January 10, 2009

Code Monkey - Jonathan Coulton

something out of programming for all programmers - Code Monkey by Jonathan Coulton.


I think you can download the song straight from his blog.
More about what is a code monkey: http://en.wikipedia.org/wiki/Code_Monkey

Cleaning/Stripping/Removing empty lines using PHP Regex (Regular Expressions)

Removing empty lines from whole chunk of HTML/CSS/Javascript output would do great effects. With the function below which uses preg_replace (Perl-Compatible Regular Expressions - PCRE Regex), you can easily strip off empty lines, which can improve your application's workload and speed.

function removeEmptyLines($s)
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $s);

Use the function to clean buffers of HTML, CSS or Javascript before outputting to browsers/clients. It will do a great deal of magic.

Sunday, January 4, 2009

Why exit(); after Header redirect

You'll probably need a few HTTP 302 or 301 redirect in your PHP application when you develop it. To do a redirect, you can easily call:

  header($_SERVER['SERVER_PROTOCOL'].' 302 Found');
  header('Location: nextpage.php');

However, many people do not know that even after this header is called, the code after this line still executes! In the above code, the temporary file "2A4E22F1.dat" is still deleted. Therefore, you must add a exit(); after this redirect.

  header($_SERVER['SERVER_PROTOCOL'].' 302 Found');
  header('Location: nextpage.php');

Since it is not valid and you did a redirect, the temporary file is not deleted. This is sometimes what developers miss out when doing redirects. hope it helps!