Kiran's blog

Using Perl Code in PHP

PHP perl extension is available to allow the usage of perl code in PHP.

The perl extension is used to load and execute perl files, evaluate perl code, access perl variables, call perl functions, instantiate perl objects, access properties of perl objects, call methods of perl objects.

PHP's perl extension is available from the PECL web site at http://pecl.php.net/package/perl. The latest development version can be obtained with the following CVS command:

$ cvs -d :pserver:cvs.php.net:/repository co pecl/perl

If you have a full perl installation, the extension will work with it. If you don't have perl on board, you can still communicate with the perl interpreter through PHP by putting a copy of perl58.so or perl58.dll somewhere PHP can find it (in the PHP directory or in your system path).

To access the perl interpreter from PHP, you must first create an instance of the Perl class.

$perl = new Perl();

Introduction to CouchDB

Apache CouchDB being a schema-free, distributed, fault-tolerant and document-oriented database accessible via a RESTful HTTP/JSON API.

It is highly robust, incremental replication with bi-directional conflict detection and resolution, and is queryable and indexable using a table-oriented view engine with JavaScript acting as the default view definition language.

CouchDB is written in Erlang, but can be easily accessed from any environment that provides means to make HTTP requests. There are a multitude of third-party client libraries that make this even easier for a variety of programming languages and environments.

CouchDB is not at all a replacement for relational database management system. Instead of storing data in rows and columns, the database manages a collection of JSON documents (early versions of CouchDB used XML).

Checking File Permissions In PHP

Since script can only read and write files when the PHP interpreter has permission to do so. You don't have to cast about blindly and rely on error messages to figure out what those permissions are, however. PHP gives you functions with which you can determine what your program is allowed to do.

To check whether a file or directory exists, use file_exists( ). Here is an example which uses this function to report whether a directory's index file has been created.

Checking the existence of a file

<?php
if (file_exists('/usr/local/htdocs/index.html')) {

    print "Index file is there.";

} else {

    print "No index file in /usr/local/htdocs.";

}
?>

To determine whether your program has permission to read or write a particular file, use is_readable( ) or is_writeable( ). Here is an example which checks that a file is readable before retrieving its contents with file_get_contents( ).

Testing for read permission

<?php

Auto Page Refresh using PHP

For refreshing a PHP page after a pre-defined amount of time for what ever reason like automatically refreshing the home page (or subpages) for updated news, just add the following script at the very beginning of your page. Any other header sent before this header function will make it cause an error.

<?php
header("Refresh: 60;");
?>

NOTE: The above code will make the current page automatically reloaded after 1 minutes, here the time is expressed in seconds.

You can also redirect to another page of your site or an external site after pre-determined time.

The following code will send the visitor to the blog (assuming you have the blog running under the folder "blog") after 10 seconds.

<?php
header("Refresh: 10; url=/blog/");
?>

This will send the visitor to www.php.net after 5 seconds.

<?php
header("Refresh: 5; url=http://www.php.net");
?>

Cache API in DRUPAL

There are two major function in DRUPAL for Cache API.

1. cache_set().
2. cache_get().

Function signature for cache_set() is:

cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL)

The function parameters are:

• $cid: A unique cache ID string that acts as a key to the data.
• $table: The name of the table to store the data in. You can create your own table or use cache, cache_filter, cache_menu, or cache_page. The cache table is used by default.
• $data: The data to store in the cache. Remember that complex PHP data types must be serialized first.
• $expire: The length of time for which the cached data is valid. Possible values are CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.
• $headers: For cached pages, a string of HTTP headers to pass along to the browser.

Here is an example for cache_set() which can be seen in filter.module.

// Store in cache with a minimum expiration time of 1 day.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks