Sunday 29 July 2012

Cookie in CakePHP

Write Cookie:
$this->Cookie->write('countryName', 'India', false, 0); Argument Description sequence: cookie name, value, encrypted or not if set false means cookie will store as plain text.
Read Cookie:
echo $this->Cookie->read('countryName');
Output: India

Saturday 28 July 2012

strstr() Function

Finds first occurance of any string from a given string.
strstr() function is a case sensitive function to use case insensitive stristr() is used.
e.g
$emailId = 'test@mydomain.com';
$domainName = strstr($emailId, '@');
echo $domainName; // outputp will be: mydomain.com
$firstPart = strstr($email, '@', true); // PHP 5.3 and above
echo $firstPart; // outputp will be: test
?>

Friday 27 July 2012

sprintf() function

function sprintf() returns a formatted string, according to the formatting string format.
$numOfStudent = 50;
$class = 'Class';
$format = 'There are %d Students in the %s';
echo sprintf($format, $numOfStudent, $class);
// output will be: There are 50 Students in the Class
?>

Thursday 26 July 2012

strrpos() function

Finds position of the last occurance from a string.
e.g
$str = 'abcd';
$chkStr = 'b';
echo strpos($str,$chkStr); // output will be: 1
?>

Tuesday 24 July 2012

strstr() Function

Finds first occurance of any string from a given string.
strstr() function is a case sensitive function to use case insensitive stristr() is used.
e.g
$emailId = 'test@mydomain.com';
$domainName = strstr($emailId, '@');
echo $domainName; // outputp will be: mydomain.com
$firstPart = strstr($email, '@', true); // PHP 5.3 and above
echo $firstPart; // outputp will be: test
?>

Monday 23 July 2012

CSS Opacity property

Transparency property of CSS is called opacity.
img
{
opacity:0.5;
filter:alpha(opacity=50); /* Used in IE8 or below version of IE */
}
Opacity range is 0.0 - 1.0, more lower opacity values means more transparent element.
property opacity is used in Firefox, Opera, IE9, Chrome and Safari for transparency. But IE8 or lower version uses filter:alpha(opacity=x). Here value x ranges from 0-100. Lower x value means more transparent element.

Friday 20 July 2012


tabindex property

tabindex attribute is used to set tab order for element.

<input name="first" tabindex="2" type="text" />
<input name="second" tabindex="3" type="text" />
<input name="third" tabindex="1" type="text" />
Above input box will highlight in sequence : third,second,first when we press tab key.

Thursday 19 July 2012

Mysql concat() function

mysql concat() function
This function combines two string into a single string.
uses:
1) SELECT CONCAT('FIRST_NAME', 'LAST_NAME') as concatname;
o/p: FIRST_NAME LAST_NAME
2) concat with separator
SELECT CONCAT_WS('-','FIRST_NAME','LAST_NAME');
o/p: FIRST_NAME-LAST_NAME

Wednesday 18 July 2012

z-index property

z-index specifies stack order of an element. An element which has greater stack order means greater z-index value is always in front of an element which has low stack order.
z-index works only on positioned elements like: position:absolute, position:relative, or position:fixed.
z-index property value:
auto: Stack order equal to its parents stack order value. This is default value.
number: Sets stack order for any element this value can be negative.
inherit: This property specifies that the z-index should be inherited from the parent element.

Tuesday 17 July 2012

Mysql replace() function

REPLACE(str1,fromStr,toStr)
Returns str with all occurrences of the string from the fromStr and replaced by the string toStr. REPLACE() is a case sensitive match when it searches str for the fromStr.
SELECT REPLACE('www.test.com', 'w', 'Ww'); o/p: 'WwWwWw.mysql.com'

Mysql DAYOFMONTH() function

DAYOFMONTH(date)
Above function returns day of the month for the date in the range between 1 to 31, or 0 for the dates like '0000-00-00' or '2012-00-00', which have zero in day part.
SELECT DAYOFMONTH('2007-02-03'); o/p: 3

Mysql DAYNAME() function

DAYNAME(date)
Above function returns name of the weekday for date.
SELECT DAYNAME('2007-02-03'); o/p: 'Saturday'

Monday 16 July 2012

Mysql DATEDIFF function

Mysql DATEDIFF(arg1,arg2) function
DATEDIFF() function returns arg1 – arg2. Value is expressed in term of days. arg1 and arg2 are either date or date-and-time expressions. Only date part is used for calculation.
SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
o/p: 1
SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
o/p: -31

MYSQL DAYOFWEEK function

MYSQL DAYOFWEEK(date) function
Returns the weekday index for date (1 = Sun, 2 = Mon,3=Tue,4= Wed, 5=Thu, 6= Fri, 7 = Saturday). index values are ODBC standard.
SELECT DAYOFWEEK('2007-02-03');
o/p: 7

Mysql extract (date) Function

extract() function can be used to extracting date value from any date.
SELECT EXTRACT(YEAR FROM '2009-07-02');
o/p: 2009
SELECT EXTRACT(MONTH FROM '2009-07-02');
o/p: 7
SELECT EXTRACT(DAY FROM '2009-07-02');
o/p: 2
Point to be Noted:
SELECT EXTRACT(MONTH FROM '2009-13-02');
Above Code will o/p NULL, because month exceeded to 12.

Sunday 15 July 2012

Concept of Smarty

A template engine for PHP is Smarty which separates presentation from application logic.
Features of Smarty:
1) It is open source and free available.
2) Smarty used in front end and PHP backend.
3) Compliment PHP, not replace it
4) fast development/deployment for programmers and designers
5) quick and easy to maintain
6) syntax easy to understand, no PHP knowledge required
7) flexibility for custom development
8) security: insulation from PHP
9) clean separation of presentation from application code

User Group Permission set in UBUNTU

sudo adduser www-data (group)
sudo chgrp (group) /var/www
sudo chmod –R 775 /var/www
sudo chmod g+s /var/www

Friday 13 July 2012

File Synchronization with rsync

The magic of synchronization is very helpful for backups, keeping web servers synchronization. It is very fast than copying of files and takes low bandwidth, using one command only.

Installation of rsync


Mostly linux flavours contains pre installed rsync. If this service is not installed we can install with package manager. In the case of ubuntu below command is used:
aptitude -y install rsync
Below command is used to copy local to server:
local /home/kevin/source to /home/kevin/destination which resides on the server: server.mysite.com:
rsync -az --progress --size-only /home/kevin/source/* server.mysite.com:/home/kevin/destination/
prefix with sync explained:
1) -r
recursive
2) a
archive, preserves all attributes like ownership, timestamps, etc
3) z
compress, saves bandwidth but is harder on your CPU so use it for slow/expensive connections only
4) --progress
shows you the progress of all the files that are being synced
5) --size-only
compare files based on their size instead of hashes (less CPU, so faster)

Thursday 12 July 2012

to_days() function in mysql

SELECT TO_DAYS(950501); output: 728779
SELECT TO_DAYS('2007-10-07'); output: 733321
to_days(date) function will give output to number of days since year 0 of the passing argument date.

Wednesday 11 July 2012

Delete duplicate rows from Database

DELETE ta FROM tablename t1, tablename t2 WHERE t1.id > t2.id AND t1.fieldname = t2.fieldname
Above query will keep rows with lowest id of the table tablename.
DELETE ta FROM tablename t1, tablename t2 WHERE t1.id < t2.id AND t1.fieldname = t2.fieldname
And above query will keep rows with highest id of the table tablename.
Note: Above id is a field of integer type.

Tuesday 10 July 2012

set chmod for directory/files

Recursively chmod only for directory
find . -type d -exec chmod 755 {} \;
Recursively setting chmod on every directory for execting
chmod -R a+X *
Above +X flag is used to set execute bit for directories only.
Recursively chmod setting only for files.
find . -type f -exec chmod 644 {} \;
Recursively set chmod only for PHP files which have extensions (.php)
find . -type f -name '*.php' -exec chmod 644 {} \;

Monday 9 July 2012

Concept of REST

REpresentational State Transfer or REST is asoftware architecture which is used for distributed system like world wide web (www). REST is used as a web services which displaced other design models like SOAP and WSDL due to its simpler style

Sunday 8 July 2012

HTTP Methods

Below are the set of methods defined in HTTP/1.0:
1) GET -: This method retrieve information in the form of entity which are identified by the request URI.
2) POST -: Post method is used to send data to the server to process it by some way like by the CGI Script.
3) HEAD -: Like GET method HEAD request do same except this message asks to server only to return response headers no body of the message. This method is useful to saving bandwidth because it check characteristics of resource without downloading it.

Saturday 7 July 2012

SOAP or Simple Object Access Protocol is a protocal which is used for exchanging structured information in the web service implementation in the computer networs. For message formats it relies on XML (Extensible Markup Language) and for message negotiation and transmission it relies on Application Layer Protocols (like HTTP) and Simple Mial Transfer Protocol (SMTP).
Features of SOAP:
  1. It is a communication protocol
  2. It is used for communication between applications
  3. It is a format which is used for sending messages
  4. It communicates via Internet
  5. It is platform independent
  6. It is language independent
  7. It is based on XML
  8. It is simple and extensible
  9. It allows to get around firewalls
  10. It is a W3C recommendation

Builiding Blocks of SOAP:
SOAP message is a a simple XML document which includes below elements:
  1. An Envelope element that identifies the XML document as a SOAP message
  2. A Header element that contains header information
  3. A Body element that contains call and response information
  4. A Fault element containing errors and status information
SOAP Syntax Rule: Below are some important syntax rule:
  1. A SOAP message MUST be encoded using XML
  2. A SOAP message MUST use the SOAP Envelope namespace
  3. A SOAP message MUST use the SOAP Encoding namespace
  4. A SOAP message must NOT contain a DTD reference
  5. A SOAP message must NOT contain XML Processing Instructions
SOAP Document example ... ... ...

Friday 6 July 2012

Create CakePHP Component

When we require any code for many controllers or different parts of application we can create component in CakePHP Framework. Suppose we need a complex mathematics calculation in every controller or different parts of application we create a math component to share in different controllers. Let we create a Sum component to return sum of two numbers, we can create this component in the path /app/controllers/components/math.php with below code:
class SumComponent extends Object {
function sumTwoNums($amount1, $amount2) {
return $amount1 + $amount2;
}
}
?>

Above Sum component is extending Object class not Component, because if we extend Component which can create infinite redirect issues when component is combined with another Components.
Include Component in Controller where we need it.
var $components = array('Sum');
Calling Component functions inside Controller by below code:
$this->Sum->sumTwoNums(4,8);

Thursday 5 July 2012

Concept about JSON

JSON or Javascript Object Notation is a way to storing data or information in organized manner. These information can access easily. These stored information is human readable so we can access it logical manner.
Example of JSON:
var jsonobj = { "name":"John", "age":"25 years", "sex":"Male" };
Above jsonobj variable is used to access the values. Property and values are kept inside curly braces like "property name":"property value" pair. Multiple vales can be kept using comma separation. We can access values stored in jsonobj by refering the name ofproperty like:
document.write(jsonobj.name+' is '+jsonobj.age+' old.');
Above code will output: John is 25 years old.

Tuesday 3 July 2012

Redirecting sub domains according to Country

To redirecting any sub domain according to country, install mod_geoip module (GeoIp Extentsion) inside server if it not installed. Write below code in .htaccess file:
GeoIPEnable On
GeoIPDBFile /GeoIp path/GeoIP.dat
#Redirection according to country
# For USA
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^USA$
RewriteRule ^(.*)$ http://us.mysite.com$1 [L]
# For India
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^IN$
RewriteRule ^(.*)$ http://in.mysite.com$1 [L]
# For Canada
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://ca.mysite.com$1 [L]

Types of Software Testing

Black Box Testing – This tests are based on requirements and functionality. Internal system design not considered in this testing.
White Box Testing – This testing is also called Glass Box Testing. This test is based on knowledge of the internal logic of any application. In this testing knowledge of code statements, branches, paths conditions should known.
Unit Testing – This type of testing is based on individual components or modules of any software. Because this testing requires detailed knowledge of the flow (design and code of software) of software test is done by programmer not by tester..
Incremental integration testing – In this type of testing functionality of application or modules should be independent to test it separately done by programmers or testers. So it is called bottom up approach for testing.
Integration testing – This testing is used when all modules are tested in integrated form. Modules may be application, code, client server functionality, distributed systems.
Functional testing – This test checks funtional requirements of any application like black box testing. It ignores internal parts of application.
System testing – This test is based on entire system testing as per the requirements. It checks overall requiremetns of any application in combined form.
End-to-end testing – This type of testing is used for complete application environment. In this test real-world use of software is tested like database interaction, network communications, interaction with hardware or applications or any other systems.

Sunday 1 July 2012

Enable Disable root account in ubuntu

In Ubuntu Linux distribution root account not enable. But if user want to do some command with root permission on the terminal sudo command should be written before command. sudo stands for super user do. sudo command require password. Below command can be used to enable root account:
$sudo yourpassword root
Above command will prompt for new root password after confirming root account can be logged in.
To disable root account in ubuntu have to lock root account by using below command:
$sudo password -l root
Better way to work on a root console below command is used:
$sudo -i