Category : "Coding"

All kinds of Programming.

Code a TCP Connect port scanner in PHP

A port scanner is a software application designed to probe a server or host for open ports. We earlier made a TCP Connect port scanner in C here - http://www.binarytides.com/blog/tcp-connect-port-scanner-code-in-c-with-winsock/ and here - http://www.binarytides.com/blog/tcp-connect-port-scanner-with-linux-sockets-bsd/ Now we shall try making the same in PHP. The code is very simple : Using fsockopen <?php /* Simple TCP connect port scanner in php using fsockopen */ //avoid warnings like ...

PHP best way to check if file is an image

The getimagesize function of php provides lot of information about an image file , including its type. The type can be used to check if the file is a valid image file or not. To check if a file is an image or not, use the function function is_image($path) { $a = getimagesize($path); $image_type = $a; if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP))) { return true; } return false; } $a ...

PHP manage multiple cronjobs with a single crontab entry

In many php applications there are multiple tasks that need to be run via cron at different times. In a typical application you may be doing the following tasks via cronjobs : 1. Backup database. 2. Send out email to subscribers. 3. Clear temporary files. 4. Fetch xml feeds from some source and store them in database. So if you had separate php files doing ...

PHP check if a timestamp matches a given cron schedule

desktop:~$ php -a Interactive shell php > echo time(); 1319362432 php > Above is an example of a given timestamp. And a cron schedule can look like this 0 5 * * * - which means run everyday at 5 hours and 0 minutes. Now in a php application you may need to test if a given timestamp , say 1319362432 matches a given cron schedule ...

PHP strtotime in 64 bit environment

The strtotime function is used to convert a date in various formats to a timestamp. However its behaviour is different based on architecture it is running on, whether 32bit or 64bit. Lets take a few examples : In 64 bit environment desktop:~$ php -a Interactive shell php > echo strtotime("0000-00-00 00:00:00"); -62170005200 php > echo strtotime('1000-01-30'); -30607739600 php > echo strtotime('2100-01-30'); 4104930600 php > Over here we can see that strtotime ...

Mysql autoincrement column that resets every month

There are situations when we need an autoincrement field to reset every month or so. Example : Invoice Numbers : Month of october 2011-10-01 2011-10-02 ... ... ... 2011-10-800 Month of november 2011-11-01 2011-11-02 ... ... and so on. Groupwise autoincrement P1 , P2 , Q1 , Q2 , Q3 , Q4 , P3 , R1 , P4 ... Myisam table has a feature where , if a primary key contains 2 columns where one is an autoincrement ...

PHP create nested directories for a given path

If a file is to be saved in at path /var/www/a/b/c/d/abc.zip where the directory c and d dont exist then the directories have to created. Here is a function that uses recursion to check for directories in a path and create them if they do not exist : /** Make a nested path , creating directories down the path Recursion !! */ function make_path($path) { $dir = pathinfo($path ...

Programming box2d in javascript – tutorial on basics

Box2d Box2d is a 2d physics simulation library written for C++ and ported to many languages including Javascript. From Box2d's Website Box2D is a feature rich 2D rigid body physics engine, written in C++ by Erin Catto. It has been used in many games, including Crayon Physics Deluxe, winner of the 2008 Independant Game Festival Grand Prize. Box2d can is used to develop 2d ...

DNS Query Code in C with linux sockets

This article is an extension of the article : http://www.binarytides.com/blog/dns-query-code-in-c-with-winsock/ The Linux version has some changes. On Linux the dns server ips are stored in a file called /etc/resolv.conf. So the get_dns_servers function will open this file and pickup the dns server ip addresses. A typical /etc/resolv.conf file can look like this : # Generated by NetworkManager nameserver 208.67.222.222 nameserver 208.67.220.220 So the lines starting with nameserver can ...

PHP benefits of storing session data in database

PHP's session_start method stores the session data in files by default. These files are stored in the tmp directory of the OS and are cleared at intervals by the garbage collector. Alternatively the session data can be stored in database as well. Storing session data in database has certain advantages : 1. Limit the number of logins For instance if your application should ...



Connect with us