Archive

Posts Tagged ‘ip address’

Common Regular Expressions

March 24th, 2008 No comments

Here are some of the regular expressions that I frequently use.

Find a blank line
^$

Spaces
[ \t]+
You can use this to break a text string apart into words.

Date
\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}
This will match anything in the format mm/dd/yyyy, or even dd/mm/yyyy.
[A-Z][a-z][a-z] [0-9][0-9]*, [0-9]{4}
Will match a formatted date, such as Mar 24, 2007.

Time
([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?
This will match HH:MM or HH:MM:SS or HH:MM:SS.mmm.

IP Address
(((\d{1,2}|(1\d{2})|(2[0-4]\d)|25[0-5]))\.){3}((\d{1,2}|(1\d{2})|(2[0-4]\d)|25[0-5]))
This also checks to see that the IP address is within the range 0.0.0.0 to 255.255.255.255.

Email Address
([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))
This is using a simple mechanism, the following expression uses the RFC standard for an email address format and so should match %99.99 of all email addresses.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Complete URLs
https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?
This will match virtually any URL.

HTML Comments
<!-{2,}.*?-{2,}>

Inline Comments
//.*
This will match inline comments in C, PHP, Java, JavaScript etc.

How To Read A Remote IP Address In PHP

January 16th, 2008 No comments

PHP keeps certain variables to do with server and networking in an associative array called SERVER. To find out the remote address of a user you can use the array identifier REMOTE_ADDR. This is used in the following manner.
$ipaddress = $_SERVER['REMOTE_ADDR'];
This IP address can be passed into the gethostbyaddr() function to find out host name associated with the specified IP address.
$hostname = gethostbyaddr($ip);
You can then pass this into a database on your site to record not only who is visiting your site but where they are from. Gaining this information from PHP is beneficial if you are using Google Analytics as Google doesn’t tell you specific visitors just an overall picture. Additionally, Google Analytics will only record information from users who have JavaScript turned on. You will find that there might be a lot of traffic to your site that isn’t recorded like search engine spiders and page scrapers.

Categories: PHP Tags: , , ,