Thursday, 24 October 2013

php include all files from a directory

Include all files from a directory:-

Here is small code snippet to show that how we can include all files from a particular directory .

$all_files =  glob( "path to the directory/*") ;
foreach ( $all_files  as $filename)
include $filename;

If we try to explain it , then please understand glob() will return an array of all files on the path given as parameter .Rest everything is familiar with you all . foreach is the loop to iterate every element of any array . So by this we are including all files .

We can go in some tricky way also . example :-

Include all php files only :-

We can use regex syntax to achieve our goal . We will be able to include all php files only by following method

$all_files =  glob( "path to the directory/*.php") ;
foreach ( $all_files  as $filename)
include $filename;

Here only one parameter in the glob() function is changed and it will pic up all files which ends with .php extension .

Please ask question if you feel need of any clarification . 

Friday, 18 October 2013

php cache using APC cache in php and simple program to count pages views using apc cache

First of all we need to understand what is cache ?Cache is collection of items (data) stored in RAM . It is easily accessible . it does not require any I/O operation so its fast also .We use cache to optimize our application response time .

Cache in PHP :-

There are many ways we can use cache in php :-

      1)     Memcahce
       2)    APC

Here I will explain how to use APC .

APC is alternate php cache 

It is an extension to php . we can install it by following command .

             yum install pcre-devel
             yum install apc

Here are some predefined functions we need to use for APC .We can understand many functionalities just by name of the method .

apc_store () -- to store data in cache
apc_fetch() -- to fetch data from cache
apc_delete() -- to delete entries from cache
apc_inc() -- to increase integer value of any cache entry
apc_dec() -- to decrease integer value of any cache entry

Uses of functions :-

     apc_store('name','parveen');          //this will store name=’parveen ‘ in cache .
     apc_store('name','parveen',500);    // store in cache for 500 seconds.after that will be auto deleted 
     echo apc_fetch('name');                 // it will return ’parveen’
     apc_delete('name') ;                       // will delete from cache . if we try to fetch now then it will give error

example :-

apc_inc('num'); // now num is 2
apc_inc('num',5); //now num is 7
apc_dec('num'); // now num is 6
apc_dec('num',3); //now num is 3

Some points that we need to take care of :-

  1)  If memory of cache is full then it will delete all cache . so increase max limit size acording to your use . this can be achieved by making some entries in php.ini (php conf file)

   2) For better optimization we should set maximum time limit of cache . so that old entries get deleted automatically and free acquired space .

we can write following lines in php.ini file 

apc.shm_size="128M"       // this will set maximum size of cache to 128MB . default is 32 mb
apc.ttl="1800"                   // this will set maximum time to live for cache entry . default is unlimited

Simple page visits counter programs using cache :-

$key = 'visit_num';
apc_inc($key, 1);

apc_store($key, $count);


and on hourly basis we can get that count from cache and store it in db . we can do it on daily basis or according to our convenience .

Wednesday, 16 October 2013

php using date and time for various purposes

We always need to deal with date and time in our programming specially when we are using php .
time() and date() these two functions are very important regarding our concern .

see following line of php code :-
$a = time();
time() function returns total number of seconds passed till date from 1st jan 1970 .
date() function is used to get date formatted according to our wish .
function date() can have two parametes:-
1) format of time stamp that we want to show
2) optional:- timestamp which we need to use to get date

first parameter is string that shows that exact output format  . some characters are usefull like
Y -- to show current year
m -- to show current month number
d -- to show today's dat
H -- current hour   24 hour format
i -- to show current minute
s -- to get current seconds
t -- shows number of days in current month  or last date of current month

following are some uses date() function :-

echo  date(""); // this will print nothing
echo date("Y-m-d H:i:s"); // this will show complete time stamp i.e.  2013-10-16 12:00:00 as your system time

echo date("Y-9-d 12:00:s"); // this will show  2013-9-16 12:00:45

these were examples of formatting of time stamp that we want to show ;

second parameter is time stamp itself .
for example :-

 echo date("Y-m-d H:i:s" , time());  // this will show  current time
  echo date("Y-m-d H:i:s" , time() + 500);  // this will show  time after 500 seconds

  echo date("Y-m-d H:i:s" , time() + 86400 );  // this will show tomorrow's time because 86400 is number of seconds in one day .

if we want to see time 5 days ahead  then we need to do is
  echo date("Y-m-d H:i:s" , time()  + 5*86400);

similarly if we want to see time 5 days back then we need to do is
  echo date("Y-m-d H:i:s" , time()  - 5*86400);

if we need to find how many days are in this month or any month then we use "t" in format string (first parameter) .

$number_of days_this month = date("t"); // it will give only last date of the month

Thanks and please ask in comment if you need any clearification .

Tuesday, 15 October 2013

PHP file to return image or return 1x1 pixel image

Using php file we can return image .  there are many benefits of this thing . we can keep count that how many times our image is fetched or from which us/ip that image is fetched.also we can change image from php code without changing in the real html file .

see following code

<img src="http://localhost/test.php"/>

if we want that test.php should return any image
following is the code snippet to return that image

test.php :-

$file = file_get_contents("full path to ur image fle");
$contenttype = "gif" ; // it is type or extension of image like jpeg for jpeg images and gif for .gif images
header("Content-type: image/$contenttype");
echo $file;

we need to  take care of some thing in this .
we should not print or echo any other variable or string from the same php file .then it will be showing some error and image will not be returned .

IF we want to return 1x1 image pixel then following is the code snippet .

header('Content-Type: image/gif');
header('Content-Length: 43');

this code will provide us 1x1 image