Wednesday, April 28, 2010

Funny successful page!

One day I install some web based software (I don't like to mension the name). Once I successfully install the program I saw,

"If you see this page, there was no error while installation" 


How to get HTTP response code of HTTP request in PHP

Here the function to get the HTTP response code of a HTTP request. This function can easily be used to detect what is the status of a page that you are requesting (such as 404 - Not Found, 301 - Permenently Moved). Here is a complete reference for HTTP response codes.

  1. function getResponseCode($url) {
  2.     $ch= curl_init();
  4.     curl_setopt($ch, CURLOPT_URL, $url);
  5.     curl_setopt($ch, CURLOPT_HEADER, 1);
  6.     curl_setopt($ch, CURLOPT_NOBODY, 1);
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8.     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  10.     curl_exec($ch);
  12.     $details = curl_getinfo($ch);
  14.     curl_close($ch);
  15.     return $details['http_code'];
  16. }

How to Validate RSS or ATOM feed using simple PHP Script

Here a simple function to Validate Feed URL,

  1. function getFeedStatus ($url) {
  2.     $ch = curl_init();
  3.     curl_setopt ($ch, CURLOPT_URL, $url);
  4.     curl_setopt ($ch, CURLOPT_HEADER, 0);
  5.     curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
  6.     curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  9.     $string = curl_exec($ch);
  10.     curl_close($ch);
  12.     if (strpos($string, '<feed') > 0 && strpos($string, '</feed')) {
  13.             return "ATOM";
  14.     } else if (strpos($string, '<channel') > 0 && strpos($string, '</channel')) {
  15.             return "RSS";
  16.     } else {
  17.             return "INV";
  18.     }
  19. }

Monday, April 12, 2010

Floating Layouts for Web

Tables were the most popular way used to build the basic layouts of web pages. Nowadays, the floating layouts are gaining lot of interest.  Floating layouts are dynamic and highly depend on CSS (cascade style sheets). Because, it is the new trend, let's take look at the basics of a floating layout.

Basically the floating layouts are created by using floating Divs. Floating divs are controlled using CSS and they are more like the text in a fixed width div. Similar to the way words are arranged in a div, the basic building blocks of the web site are arranged within a div. What happens when there are more words in a line that exceeds the width of the container div. A new line is used to arrange the excess of words. Same applies for the divs in a floating layouts as well.
My blog have been built entirely with a floating layout without even using a single Table. Here is a good tutorial on how to do this.

Thursday, April 8, 2010

How to track a page or send data to another domain using the browser?

Say, you want to track a page. To track a page you need to send request to a tracking page when the user viewed the page in the browser. For example, say you need to send a request to a page (track.php) from another page (index.php) is loaded. If the track.php is in the same domain with the index.php, then you can easily send an AJAX request to track.php from index.php when the page is loaded.
That works fine only if the files are not in separate domains.

In separate domains means, location of track.php is and location of index.php is Now you may ask, "why?, what is the problem, if the files are is in a separate domains? Can't we still send an AJAX request?".  The answer is, if you send an AJAX request in this situation from index.php to track.php, the browser will raise a Security Exception and the AJAX request will fail, since you are making a cros domain request.
So what is the easiest way to get this done? The simplest solution is say the browser "you don't worry, is an image, just load it". Confuced?? What we do is adding following HTML code to the index.php.

<img src="" height=1 width=1/>

The poor browser doesn't know the source of image is a tracking page and it will load the image even if it is in a separate domain.So, you can track the page in example_2 domain, from example_1 domain. In addition, if you want to send additional data to the example_1 domain, just pass URL variables as follows.

<img src="" height=1 width=1/> 

In both above cases it is better if you response with an actual image with white pixel. To do that you can use following PHP code.
  1. <?php
  2. // Create a blank image and add some text
  4. // Sets color to white
  5. $white = imagecolorallocate($im, 255, 255, 255);
  6. imagefill($im, 0, 0, $white);
  8. // Set the content type header - in this case image/jpeg
  9. header('Content-type: image/jpeg');
  11. // Skip the filename parameter using NULL, then set the quality to 75%
  12. imagejpeg($im, NULL, 75);
  14. // Free up memory
  15. ?>
Alternative, you may use an iFrame to send the request. Other than that you can request an JavaScript file or CSS file or whatever that makes request to a server as you wish. But may not be the best for that.
All rights reserved for all the posts. Please inform us on if you are publishing post in this without modifying. Code snippets are free to use, if not explicitly mentioned.