Category : "PHP"

Php tutorials

Get http request headers in php

When a browser makes a request to a php script, the browser sends some http headers which can look like this : Host: localhost Connection: keep-alive User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: UTF-8,*;q=0.5 Cookie: username=admin; password=21232ffc3; PHPSESSID=o5m4e2td1m66c4pkjdag9vs0u2 The php script under request , may want to access these http headers. PHP has a method getallheaders() which provides ...

PHP get adsense earnings and reports

In this article we are going to login into Google Adsense and retrieve earnings. Google Adsense now has a new interface and so needs a different kind of coding. In this example we shall fetch "yesterday's" earnings sitewise. Create a Report to be fetched : First open the sitewise report in the Performance Tab in the new interface of Google Adsense at ...

PHP get DNS Nameserver , Cname , MX , A records of a domain

PHP has a useful function called dns_get_record that can be used to get different types of records for a give domain , very easily. Nameserver - NS Records : $ php -a Interactive shell php > print_r(dns_get_record('www.binarytides.com' , DNS_NS)); Array ( => Array ( ...

Parse MVC style urls in PHP

If you have used codeigniter or some mvc php framework then you might be aware of urls like www.yoursite.com/index.php/class/method/param1/param2?a=b&c=d or www.yoursite.com/class/method/param1/param2?a=b&c=d if you use mod rewrite to add the index.php The htaccess code could be like this : <IfModule mod_rewrite.c> #Start the rewrite engine RewriteEngine on #If requested thing is not a file or directory , then rewrite RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d ...

Suppress Errors & Warnings With @ operator in PHP

The at sign (@) is an error control operator in PHP. You can suppress errors, warnings and notices using @ operator in PHP. It is quite common to prefix function calls with @ to suppress errors like this - <?php @mysqli_connect($host, $user, $pass, $db_name); # If the connection is not established for some reason, then the error will be suppressed @getimagesize('lol.png'); # If ...

Write a simple PHP Hit Counter Script

In this tutorial, I am going to show you a simple way to write a Hit Counter script in PHP to save and display the number of visits to your website. We will basically store and increment the hits in a file, that can be used to display the total hit count as well. The code to store the hits follows - <?php class ...

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 ...



Connect with us