PHP: Get visitor’s location, city, country from IP address
Ip to Location Many applications need to get information about the geographical location of the user/visitor on their websites. This can be done in many ways. There are online services like ipinfodb and geoio that provide web based api to get the geolocation information about a given ip address. Services like Maxmind geoip provide free… Read More »
Parse and change date format in php
Parsing dates Most php applications need to parse dates and convert them to timestamps for example. Parsing is also necessary to convert the date into a more standard format like yyyy-mm-dd which can be stored in a database like mysql. Mysql database stores dates in the format yyyy-mm-dd like 2009-04-15. However humans are more used… Read More »
Restore Mysql Database from a sql or zip file using PHP
In a previous post we learnt how to inside php. In this example the same zip file would be used to restore the database. Code // Function to restore from a file function restore($path) { $f = fopen('restore/temp.sql' , 'w+'); if(!$f) { echo "Error While Restoring Database"; return; } $zip = new ZipArchive(); if ($zip->open($path)… Read More »
Backup mysql database with php and zip it
Many php applications prefer to backup the mysql database from within the application and save it as an archive. The mysqldump commandline utility can be used to perform this function of backing up a mysql database as sql file. The command would be like this : mysqldump –user=$username –password=$password –opt $db_name > $dir/backup.sql The above… Read More »
How to code a TCP Connect Port Scanner in C with Winsock
Tcp connect port scanning TCP connect() scanning is the most basic form of TCP scanning. The program performs a connect() command on those ports of the target machine which are to be checked. If the port is open then the connect() command will succeed and a connection will be established. If the port is closed… Read More »
How to Code a Port Scanner in C on Linux
Port scanning A port scanner is a program that checks for open network ports on a local or remote machine. For example, if a machine is running an http webserver then it has port 80 open. So by scanning for open ports on a machine, we can find what server applications are running on it…. Read More »
Php – parse text and convert urls into hyperlinks
The following function will parse a given text and convert all the urls into links. It does this using regular expressions. It converts email addresses to mailto links as well. Code function parse_links($str) { $str = str_replace('www.', 'http://www.', $str); $str = preg_replace('|http://([a-zA-Z0-9-./]+)|', '<a href="http://$1">$1</a>', $str); $str = preg_replace('/(([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str); return $str; }
How to code a Packet Sniffer in C with Libpcap on Linux
Libpcap Libpcap is a packet capture library for linux which can be used to sniff packets or network traffic over a network interface. Pcap Documentation gives a description of the methods and data structures available in the libpcap library. To install libpcap on your linux distro you can either download the source from the website… Read More »
How to code Packet Sniffer in C with Sockets on Linux
Packet sniffer Packet sniffers are programs that intercept the network traffic flowing in and out of a system through network interfaces. So if you are browsing the internet then traffic is flowing and a packet sniffer would be able to catch it in the form of packets and display them for whatever reasons required. Packet… Read More »
How to fix slow Internet on Ubuntu when using a switch
Switches, Routers, Wires and speed As usual internet was fast on ubuntu as it used to be till I needed to plug more PCs into the adsl route lying nearby. After connecting all computers and the adsl router to a switch I noticed that on one of the PC (Ubuntu 8.04) internet became really slow…. Read More »
Install Postgresql, phpPgAdmin and pgadmin on Ubuntu
Install Postgresql , phpPGAdmin and pgadmin 3 can be installed from synaptic. $ sudo apt-get install postgresql phppgadmin pgadmin3 After installation some configuration needs to be done. First of all setup the password of the user ‘postgres’ which is the default user of postgresql. Type the following in the terminal $ sudo -u postgres psql… Read More »
Run php scripts from cron
Automatic Running In a web application there are many cases, when a certain piece of code or script needs to run automatically in the background to perform certain tasks, without the need of any user to start it. Like scraping, extracting or crawling data from web or perform some regular tasks like sending emails, system… Read More »
Php set_time_limit wont take into account socket operations
The PHP function set_time_limit is used to set a time limit on the maximum execution time of a script. But if the script has socket operations using fsockopen , fread and fwrite or even CURL then the set_time_limit may not appear to have any effect on the scripts timelimit or timeout. Again if safe mode… Read More »
How to Install Apache Mod Rewrite on Ubuntu / Linux
Apache Mod Rewrite mod_rewrite is an apache module which enables rewriting of urls requested by client before the pages are fetched by apache. For example www.site.com/products.php?code=459 can be written as www.site.com/products/459 or www.site.com/products/459.html. The second url is re-written into the first one by mod_rewrite using rewrite rules specified in the .htaccess file. Enable Mod Rewrite… Read More »
How to Change Column / Field order in OpenOffice Base ( HSQL )
Altering the sequence of fields of a table was a feature I was looking for in OpenOffice Base. It is not directly possible to drag the columns or fields and alter their sequence. We need a sql workaround to change the order of the columns. Lets say the sequence or order of the columns is… Read More »
Setup apache for multiple users on ubuntu with userdir module
By default Apache puts all the web files into a single directory (commonly /var/www). If there are multiple users on a system then every user can have his own web directory to store the web files. userdir is the Apache module which enables each user to have a separate directory to store web files. This… Read More »
Get real IP address of user in php
Client IP address Php applications are mostly web based and often need to get the ip address of the user who is connecting to the application. Ip address is required for various logging purpose, geolocation service etc. The most common way to get the ip address of a remote user is by using the superglobal… Read More »
How to prevent Output buffering in PHP Apache with mod_gzip / mod_deflate
Till recently most of my php scripts were working fine on the servers with instantaneous outputs as and when a echo statement was encountered. Output in Chunks One issue that popped a lot on localhost was that of chunky outputs; that is the outputs of big scripts came out in chunks rather than smoothly with… Read More »
How to do Remote Login with Curl in PHP
Remote login with CURL The curl extension of php can be used to open remote webpages by both GET and POST methods. There are many situations when you need a php script to login into a website and open a certain page. For example when you are writing a bot or web scraper. To login… Read More »