Monday, October 5, 2009

Regular Expression Cheatsheet

posted by adityachandra @ 10:13 PM, ,


PHP Cheatsheet

posted by adityachandra @ 10:12 PM, ,


Tuesday, September 29, 2009

Multiple Databases, Database Switching in CAKEPHP

This is a small tip but more powerfu l and useful tweak for the developers who will often change the database configuration details while moving to developement environment to live env and vice versa.

<?php
class DATABASE_CONFIG {
var $development = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'your_local_username',
'password' => 'your_local_password',
'database' => 'your_local_database',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
var $production = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_remote_server',
'port' => '',
'login' => 'your_remote_username',
'password' => 'your_remote_password',
'database' => 'your_remote_database',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
var $default = array();
function __construct(){
$this->default = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? $this->development : $this->production;
}
function DATABASE_CONFIG(){
$this-> __construct();
}
}
?>


By the above script you can automatically switch your datbases according to the server.
And one more useful tip
we often used to change the value of debug constant in core.php file Right.... ?
Below is the small tip for that. By this, debug will be set to zero automatically
in live environment and it will be set to '2' in the development environment.
In app/core.php replace with following code :

FIND:

Configure::write('debug',2);

REPLACE WITH :

if($_SERVER['SERVER_ADDR'] == '127.0.0.1'){
Configure::write('debug', 2);
}else{
Configure::write('debug', 0);
}

Labels: , , , ,

posted by adityachandra @ 11:18 PM, ,


Friday, September 18, 2009

Turnoff autocomplete or disable cache in HTML pages

In some pages(like creditcard payment page or any other authentication page) we have to avoid the drop down which was appeared below the input text elements. This is active by default in a web browser.

To avoid the autocomplete of input text elements add autocomplete="off" attribute to your input element.
For example..



<input id="creditcardnumber" name="creditcardnumber" autocomplete="off" type="text">


Labels: , , ,

posted by adityachandra @ 1:56 PM, ,


Friday, September 11, 2009

Excluding a directory while doing zip in linux

1) If you want to exclude a single directory.

tar -cvf /home/Virtualhosts/foostor.zip /home/Virtualhosts/foo --exclude "/home/Virtualhosts/foo/yourdirectory"

2) If you want to exclude multiple directories .

tar -cvf /home/Virtualhosts/foostor.zip /home/Virtualhosts/foo --exclude "/home/Virtualhosts/foo/yourdirectory1" --exclude "/home/Virtualhosts/foo/yourdirectory2" --exclude "/home/Virtualhosts/foo/yourdirectory3"

Labels: , , ,

posted by adityachandra @ 1:19 PM, ,


Monday, May 4, 2009

Encryption and Decryption in PHP

Hello Readers( if any ;) ) . Encryption and Decryption is one of the most useful and widely used technique when we want to pass values through URL (or) for encrypting the sensitive data. I googled over the Internet for these functions and finally i got them. Here are the two simple functions to encrypt and decrypt the the data with a key (or) password.

First, let us see the ENCRYPT function

/** Input Parameters are

** $string- The string you want to encrypt

** $key- string(it's like a password)

**/

function encrypt($string, $key) {
$result ='';
for($i=0; $i

//enode the result using base64 function for more security
return base64_encode($result);
}

?>

Now head on to DECRYPT function.

/** Input Parameters are

** $string- The string you want to encrypt

** $key- string(it's like a password)

**/

function decrypt($string, $key) {

$result ='';

//decode the result using base64 decode function

$string = base64_decode($string);
for($i=0; $i

?>

USAGE:

//Here we are encrypting "foobar" using the key "wordpress"

$encoded = encrypt("foobar","wordpress");

//Here we are decrypting "foobar" using the key "wordpress"

$decoded= decrypt($encoded,"wordpress");

Labels: , , ,

posted by adityachandra @ 12:55 PM, ,


Thursday, April 30, 2009

Calculating Date Difference in Javascript

Here is java script function to calculate the date difference in number of days.

function dateDiff(date1,date2) {
//Date Format YYYY-MM-DD
var startingdate = date1.split("-");
var startingdate=new Date(startingdate[0], startingdate[1], startingdate[2]) ;//Month is 0-11 in JavaScript
var endingdate = date2.split("-");
var endingdate=new Date(endingdate[0], endingdate[1], endingdate[2]) ;//Month is 0-11 in JavaScript
var one_day=1000*60*60*24;
alert("Difference is "+Math.ceil((endingdate.getTime()-startingdate.getTime())/(one_day))+" days ");
}

Points to Remember

  • Date Format is YYYY-MM-DD

Labels: , ,

posted by adityachandra @ 11:04 PM, ,