| 0 comments ]

How to add or remove WWW on URLs using htaccess. Adding www to your site url using .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursite.in$ 
RewriteRule (.*) http\:\/\/www\.yoursite\.in\/$1 [R=301]

Removing www from your site url using .htaccess

RewriteCond %{HTTP_HOST} ^yoursite\.in$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yoursite\.in$ [NC]
RewriteRule ^/?$ "http\:\/\/yoursite\.in\/" [R=301,L]

| 0 comments ]

Compressing HTML, CSS, Javascript using Apache There are two methods to available in apache to handle compression of webpage contents. They are mod_gzip and mod_deflate . Usually deflate comes pre-installed on servers. We can install both in our apache server. before we install, lets check is it availble in your server,

In order to check whether these apache directives are installed or not, we can create a test php file with calling function the phpinfo() function. From that we can see "Loaded Modules" setting in the "apache2handler" header.

From that we can see whether its installed or not. in the case of deflate, we can see "mod_deflate" under that section.

Install mod_deflate in your server

a2enmod deflate

the restart your apache with the following command

/etc/init.d/apache2 restart

We can enable this feature using .httacess file in the root directory

## Apache2 deflate support if available
##
## Important note: mod_headers is required for correct functioning across proxies.
##

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.[0678] no-gzip
BrowserMatch \bMSIE !no-gzip

 
Header append Vary User-Agent env=!dont-vary
 
 
# The following is to disable compression for actions. The reason being is that these
# may offer direct downloads which (since the initial request comes in as text/html and headers
# get changed in the script) get double compressed and become unusable when downloaded by IE.
SetEnvIfNoCase Request_URI action\/* no-gzip dont-vary
SetEnvIfNoCase Request_URI actions\/* no-gzip dont-vary
 


This is a simple version for the usage of mod_deflate.c in .httacces


 AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml


The another option is mod_gzip, it is usually 4 -6 times faster than deflate. But its needs too much server load compared to deflate. So its advised to use mod_gzip on low-traffic sites and in large traffic sites, its better to use deflate. The settings for mod_zip is following. its can be given in a .htaccess file.

# Turn on mod_gzip if available

    mod_gzip_on yes
    mod_gzip_dechunk yes
    mod_gzip_keep_workfiles No
    mod_gzip_minimum_file_size 1000
    mod_gzip_maximum_file_size 1000000
    mod_gzip_maximum_inmem_size 1000000
    mod_gzip_item_include mime ^text/.* 
    mod_gzip_item_include mime ^application/javascript$
    mod_gzip_item_include mime ^application/x-javascript$
    # Exclude old browsers and images since IE has trouble with this
    mod_gzip_item_exclude reqheader "User-Agent: .*Mozilla/4\..*\["
    mod_gzip_item_exclude mime ^image/.*


| 0 comments ]

Mysql optimization by configuring the query cache
In Ubuntu (debian) the query cache settings can be changed in the following file /etc/mysql/my.cnf
 Use the following command to edit the file
 nano /etc/mysql/my.cnf or vi /etc/mysql/my.cnf 

In this file you can find following settings.
#  Query Cache Configuration
#   
query_cache_limit       = 1M
query_cache_size        = 16M
#

Query Cache options SQL_CACHE
SELECT SQL_CACHE id, name FROM students;

SQL_NO_CACHE
 SELECT SQL_NO_CACHE id, name FROM students; 

We can check whether cache enabled in mysql database
 mysql> SHOW VARIABLES LIKE 'have_query_cache'; 

We can set Query_cache by queries
mysql>SET GLOBAL query_cache_size = 41984; 

If "query_cache_size " has value '0', then query cache is disabled, in the mysql environment. Also need to take care about the setting the query_cache_size with huge value. that will affect the system. For more information about the query caching you can visit http://dev.mysql.com/doc/refman/5.1/en/query-cache.html

| 0 comments ]

Lets have a look on How to use the Stored Procedures in Mysql. We can use these stored procedures in PHP. The following example shows how to use mysql stored functions in PHP scripts. Defining a stored function

DELIMITER $$

DROP PROCEDURE IF EXISTS `tester`.`GetAllProducts`$$
CREATE PROCEDURE `tester`.GetAllProducts(IN t INT,IN age INT)
 BEGIN
 UPDATE students SET age = age WHERE id = t;
 INSERT INTO students (name,age,sex) VALUES ('Jayan',35,'M');
 END $$
DELIMITER ;

Calling a stored function from PHP

$res = mysql_query('call GetAllProducts(2,30)');

if ($res === FALSE) {
    die(mysql_error());
} else {
    echo "@";
    print_r($res);
}

| 0 comments ]

Let us have a look on How to use the Stored Functions in Mysql. Here I am trying to show you how to use mysql stored functions in PHP scrips. Hope you all know, what is Mysql Stored Functions. Its like normal mysql functions, its written by the user. It will return results as a normal mysql functions. It can be called in normal sql statements. Defining a stored function

DELIMITER $$

DROP FUNCTION IF EXISTS `tester`.`sf_test`$$
CREATE FUNCTION `tester`.`sf_test` ()
RETURNS INT
READS SQL DATA
BEGIN
    DECLARE tot_count INT;
    select count(*) INTO tot_count from students;
    RETURN tot_count;
END$$

DELIMITER ;

Calling a stored function from PHP

$res = mysql_query('select sf_test()');

if ($res === FALSE) {
    die(mysql_error());
} else {
    echo "@";
    print_r($res);
}

| 1 comments ]

How to find out the CREATE TABLE statement of a selected table.

The 'SHOW CREATE TABLE' command will show the CREATE TABLE statement of a selected table. It will be very helpful for the database backup actions.

SHOW CREATE TABLE `tablename`

It will return two colomns one is its table name and other is the 'CREATE TABLE' statement. 

CREATE TABLE `tablename` (
 `id` int(10) NOT NULL,
 `title` varchar(256) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Hope this will helpful for someone ! 

| 0 comments ]

How to find out the Mysql storage engine type of a table.

The following queries will show the engine type of a table

SHOW
TABLE STATUS
WHERE
name= 'tablename';

The Following query will also help you to find out the storage engine.

Each Mysql table's informations are stored in the 'INFORMATION_SCHEMA' table. 

The following query will show the storage engine's of the given databse table.

SELECT 
TABLE_NAME,ENGINE 
FROM information_schema.TABLES
WHERE 
TABLE_SCHEMA = 'dbname'

To show particular table's mysql engine type.

SELECT TABLE_NAME,
ENGINE FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tablename'

Hope this will helpful for you all.

| 0 comments ]

Its a callback function for codeigniter Form validation class. It will check, whether the input date is a valid date, and it will also check whether the user is above a particular age limit. Here its 13 years.

You can use this function in codeigniter as given below

load form validation class for codeigniter into your controller functions

$this->load->library('form_validation');

Set the rules for date of birth field

$config = array(
                  array(
                     'field'   => 'dob',
                     'label'   => 'date of birth',
                     'rules'   => 'trim|required|callback_dob_valid'
                  ),
                 );

if you need you can add additional rules
Set the Rule for your form

$this->form_validation->set_rules($config);

then run the form against these rules

if ($this->form_validation->run() == FALSE)
{
     // reload the view
}
else
{
      // success action
}

/**
 * Date of birth
 * Checking for a valid date for date of birth and age verification
 *
 * @author http://phpqa.in
 * @since
 * @copyright GPL 
 */
function dob_valid($str)
{
if($str)
{
//match the format of the date dd-mm-yyyy
 // for yyyy-mm-dd  use "/^([0-9]{4})-([0-9]{2})-([0-9]{4})$/
 if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts))
 {
   //check weather the date is valid of not   
 
   $day        = $parts[1];
   $month = $parts[2];
   $year        = $parts[3];
  
   /*
   // for yyyy-mm-dd 
   $day        = $parts[3];
   $month = $parts[2];
   $year        = $parts[1];
   */
  
   // checking 4 valid date
  
   if(!checkdate($month,$day,$year))
   {
$this->form_validation->set_message('dob_valid', '%s is not a valid date, use dd-mm-yyyy format');
return false;
}
else
{
//check for future date
$dob = $year.'-'.$month.'-'.$day;
if($dob < date("Y-m-d"))
{
// age verification using DOB
$dob_time = mktime(0, 0, 0, $month,$day,$year);
$age_req = strtotime('+13 years', $dob_time);
if(time() < $age_req)
{
   $this->form_validation->set_message('dob_valid', 'You must be atleast 13 years old');
   return false;
}
else
{
  return true;
}
return true;
}
else
{
$this->form_validation->set_message('dob_valid', '%s should be a past date');
return false;
}
}
 }
 else
 {
  $this->form_validation->set_message('dob_valid', '%s is not a valid date, use dd-mm-yyyy format');
  return false;
 }
}
return false;

By removing the "$this->form_validation->set_message()" function from the above function we can use the above function in any php scripts
  

| 0 comments ]


function to validate the a given credit card based on the card type and based on luhns algorithm



/**
* function to validate a given credit card based on the card type and based                    on luhns algorithm
*
* @param $c_number credit card number
* @param $c_type credit card type
* @return boolean. true or false.
*
*/


function validateCreditCard($c_number,$c_type='')
{
$cc_num = $number;
if($c_type == "American") 
    {
$denum = "American Express";
elseif($c_type == "Dinners") 
{
$denum = "Diner's Club";
elseif($c_type == "Discover") 
{
$denum = "Discover";
elseif($c_type == "Master") 
{
$denum = "Master Card";
elseif($c_type == "Visa") 
{
$denum = "Visa";
else
{
}
// checking card types and its length based on card type
if($type == "American") 
{
$pattern = "/^([34|37]{2})([0-9]{13})$/"; //American Express
if (preg_match($pattern,$cc_num)) 
{
$verified = true;
} else 
{
$verified = false;
}
elseif($type == "Dinners") 
{
$pattern = "/^([30|36|38]{2})([0-9]{12})$/"; //Diner's Club
if (preg_match($pattern,$cc_num)) 
{
$verified = true;
} else {
$verified = false;
}
elseif($type == "Discover") 
{
$pattern = "/^([6011]{4})([0-9]{12})$/"; //Discover Card
if (preg_match($pattern,$cc_num)) 
{
$verified = true;
} else {
$verified = false;
}
elseif($type == "Master") 
{
$pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; //Mastercard
if (preg_match($pattern,$cc_num)) 
{
$verified = true;
} else 
{
$verified = false;
}
elseif($type == "Visa") 
{
$pattern = "/^([4]{1})([0-9]{12,15})$/"; //Visa
if (preg_match($pattern,$cc_num)) 
{
$verified = true;
else 
{
$verified = false;
}
}
if($verified == false) 
{
return false;
else 
// checking the card number with luhns algorithm
$stack = 0;
$number = str_split(strrev($number), 1);
if(array_sum($number) == 0)
{
return false;
}
foreach ($number as $key => $value)
{
if ($key % 2 != 0)
{
$value = array_sum(str_split($value * 2, 1));
}
$stack += $value;
}
if($stack%10 == 0)
{
return true;
}
return false;
}
}

Sample code to use the function.

if(validateCreditCard("340000000000009","American"))
{
echo "Yeah Its a valid credit card number";
}
else
{
echo "Oops, Its not a valid credit card number";
}