Wednesday, 31 July 2013

What is SQL Injection and How to avoid it?

SQL injection happens if the user input is not properly validated

This is just to play around with the database queries

To Avoid SQL Injection

  • Encrypt sensitive data.
  • Access the database using an account with the least privileges necessary.
  • Install the database using an account with the least privileges necessary.
  • Ensure that data is valid.
  • Do a code review to check for the possibility of second-order attacks.
  • Use parameterised queries.
  • Use stored procedures.
  • Re-validate data in stored procedures.
  • Ensure that error messages give nothing away about the internal architecture of the application or the database.

Difference between PHP4 and PHP5

Advantages of PHP5

Static Methods and Variables

Public , Private and Protected

Abstract Classes


Magic Methods

Final Keyword

__autoload Function

Type Hinting



Friday, 14 June 2013

How to check which wordpress theme or plugin is installed

Many a times you want to check which theme is installed on a wordpress site then you can surely use this tool to which theme is installed on that
It also show installed plugins as well

Tuesday, 4 June 2013

How to get date in javascript in required format

function tgDate(date) {
    var d = new Date(date ||,
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [day, month, year].join('-');


Friday, 17 May 2013

Where is cookie located in windows

In Windows XP

In Window 7

Cookies are located at:

Wednesday, 8 May 2013

Maximum length validation in MySQL

The following table describes the maximum length for each type of identifier.

Maximum Length (characters)
Stored Procedure or Function
256 (see exception following table)
Compound Statement Label

Difference between ENUM and SET

ENUM - Accepts only one of the value from the options.
SET  - Accepts one to many values from the options.

Tuesday, 30 April 2013

How to check button pressed in ExtJs3.4 and make it pressed programmatically

To fetch the button status

var btnPressed = Ext.getCmp("#elementId").pressed;

The btnPressed contains boolean value

To make it pressed programmatically you can use toggle function


This function changes the pressed state of the button

Suppose we want that if its pressed we want to un-press  it


Ref 1
Ref 2

Monday, 11 March 2013

Read PHP Interview Question

Sunday, 13 January 2013

How to delete all .svn folders using Windows Command Prompt?

I was working in a application which contained many “.svn” folder and so i wanted to delete all the folders at once so i fired this command

It will delete all the folders recursively

FOR /R . %f IN (.svn) DO RD /s /q %f


How to replace a URL to an Hyperlink in a String?

At times you come across a scenario where the string contains an http link and you need to create the hyperlink to the same.

This is can very well achieved using the single line of code.

$text = "This is site";
echo $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $text);



How to fetch Twitter latest updates using PHP?

many a times we need to fetch twitter updates on to our business website or elsewhere.

This is can very well achieved using the following lines of code.



echo $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $tweet->status[0]->text);


Also if when we need to with our wordpress site

//Set parameter for query string
$username = 'techoguru';
$num = '10';
// Get the data from Twitter JSON API
$json = wp_remote_get("$username&count=$num");
// Decode JSON into array
$data = json_decode($json['body'], true);
//Create unordered list
echo '';


Redirect Naked URL with www using htaccess file

There is some times requirement to redirect naked URL to URL having “www”.

We have the solutions with our .htaccess file.

The following line of code will help us to achieve the purpose.

### re-direct to www
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

How to add favicon icon file to wordpress blog or site?

Today I was thinking of adding a favicon icon for my blog PHP Blog

For making favicon.ico file

  1. Go to

  2. Upload image for which you need to create the favicon icon

  3. or you can create your own using the tool

  4. After Uploading the image, the created ico file can be downloaded

  5. Upload to the theme folder

How to Check MySQL Version from Command Prompt

Today I was asked to provide MYSQL version which we have used within our integrated system.

We as a developers are not bothered about the version, I was not aware of it and on my local system I have WAMP installed, so it directly gives the MYSQL version.

So to get MYSQL version from “mysql” command line, I got help from one of the site which asked to use the following line of code

select version ();

This will display the current MYSQL version used within system

How to find no of checkboxes using javascript?

To find no of checkboxes in html using jQuery or javascript

var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;
var cbs = []; //will contain all checkboxes
var checked = []; //will contain all checked checkboxes
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
if (inputs[i].checked) {
var nbCbs = cbs.length; //number of checkboxes
var nbChecked = checked.length; //number of checked checkboxes

The following code gives no of check boxes present and no of checked.

Using the above code I have written following code


How to repeat a string n times in PHP?

How to repeat a string n times in PHP?


echo str_repeat(" ", 3);


This function will repeat a particular string 3times and display on the screen

How to get last element of an array?

To get the last element use the end()

$arr = array('php', 'online', 'tutorials');
echo end($arr); // tutorials


How to define a variable in PHP?

How to define a variable in PHP?

define("ABC","Online Training");
echo ABC;//Online Training


Also if I change the value of the variable at later stages what will happen

define("ABC","Online Training");
echo ABC;//Online Training
define("ABC","Online Training Institute");
echo ABC;//Online Training


Even if we change the value it will display the previously set value only

How to start session and what will happen if I start session twice?

How to start session in PHP



What will happen if I write session_start(); twice

echo session_id();

echo "

echo session_id();


Reason : As per the above code writing it twice will not make any difference to the code and it will continue to keep the previous generated session_id();

What is the difference between DDL and DML in MySQL?


Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:

CREATE – to create table in the database
ALTER – alters the table of the database
DROP – delete table from the database
TRUNCATE – remove all records from a table, including all spaces allocated for the records are removed


Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:

SELECT – retrieve data from the a database
INSERT – insert data into a table
UPDATE – updates existing data within a table
DELETE – deletes all records from a table, the space for the records remain

How to add one day in PHP?

The following code is used many a times in our web development, we always try to add one day in today’s date to get tomorrow’s date. The following code will surely help you doing this.

$todayDate = date("Y-m-d");// current date

echo "Today: ".$todayDate."

//Add one day to today
$date = strtotime(date("Y-m-d", strtotime($todayDate)) . " +1 day");
echo "After adding one day: ".date('l dS \o\f F Y', $date)."


How to create ICS/iCal file using PHP?

Many a times we come across a requirement of creating ICS/iCal file from PHP, First of all iCal file is a calendar event file which can be imported in many calendar such as Google calendar, MS Outlook, etc. Its a standard file format. This code snippet gives you idea to create iCal file from PHP which can be imported to Google or Outlook

//This is the most important coding.
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=filename.ics");
echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN\n";
echo "VERSION:2.0\n";
echo "BEGIN:VEVENT\n";
echo "CLASS:PUBLIC\n";
echo "CREATED:20091109T101015Z\n";
echo "DESCRIPTION:Testing ICS Download\\n\\n\\nEvent Page\\n\\n\n";
echo "DTEND:20091208T040000Z\n";
echo "DTSTAMP:20091109T093305Z\n";
echo "DTSTART:20091208T003000Z\n";
echo "LAST-MODIFIED:20091109T101015Z\n";
echo "LOCATION:Anywhere have internet\n";
echo "PRIORITY:5\n";
echo "SEQUENCE:0\n";
echo "SUMMARY;LANGUAGE=en-us:Testing Event\n";
echo "UID:040000008200E00074C5B7101A82E008000000008062306C6261CA01000000000000000\n";
echo "X-MS-OLK-CONFTYPE:0\n";
//Here is to set the reminder for the event.
echo "BEGIN:VALARM\n";
echo "TRIGGER:-PT1440M\n";
echo "DESCRIPTION:Reminder\n";
echo "END:VALARM\n";
echo "END:VEVENT\n";


Where and How to use autoload function in PHP?

Many a times when we use OOP in PHP we define class files and include it in our code. But in PHP we can include class file on the go using autoload function.

function __autoload($class_name)
$file_path = "PATH_TO_CLASS_FILE";
if (file_exists($file_path))

The above code can be written any where in our code, at run time when we wish to create a object of any class, it will include the class file on the go and function would be accessed and required operation can be performed.

To explain the above with example, consider you created a class file in suppose “classes” folder and named it as “login.class.php” and while executing the code you create the object of the login class as

$obj = new login;

While executing the above code, it will first include the login class file and object would be created.

The advantages of this is that we can use n no of class files within a page without checking or including all class files manually

Comments in PHP

Comments in PHP

//This is a single line comment

This is
a comment
Multi line Comment

How to use variables in PHP?

Now you are very much familiar with displaying any sample text in PHP using syntax, today we would learn how php variables play an important roles in programming.

When we say “variables”, what instantly come to your mind?

Anything whose value keeps changing.

Variable in PHP are declared using “$”. I would elaborate it with example

$var_name = "value";

The above syntax is explained as “$var_name” is the name of the variable and “value” will be the value which we wish to assign to this variable.

This would be better understood with following example:

$my_name = "";
echo $my_name;

As you can see that we can assign any string to the variable and for displaying the value we can use the “echo” command

Print a string in PHP?

To start programming in PHP, lets start with running a small code to print a line:

echo "Hello World.!";

The following code will display “Hello World.!” when the code is executed on browser

What is PHP and where is it used?

Welcome to world of open source technology.

PHP is an open source programming language.

PHP is acronym for Hypertext Preprocessor, Sometimes people do refer it as “Personal Home Page”

PHP when used with MYSQL can be used for creating dynamic websites.

PHP is one of the powerful tools is open source technologies.

People now-a-days prefer using LAMP(LINUX-APACHE-MYSQL-PHP) technologies for creating various websites and web application.

Popular websites and application built on PHP

As we go about learning PHP and its dependent languages, we must first understand and see which all websites are built on PHP.

As I have stated in my earlier post advantages of PHP and its vast usage, PHP has penetrated deep into the internet industry.

To name a few popular application and websites built using PHP:

  1. : WordPress is a blogging tool which is used widely by many of the bloggers to have their own blog hosted. Its one of the best blogging tool and some times used as CMS as well

  2. : Joomla is one the oldest CMS tool which is developed on PHP

  3. : Magento is one of the popular Shopping Cart tool built on PHP. Magento is widely used for developing shopping cart application.

How to use Concat Operator in Smarty?

How to use concat operator

{$obj->calledFunc('string const')|cat:$var}

In my case I wanted to use in a project of SKADATE

{ assign var='test1' value="custom.event_activity"}
{ assign var='test2' value=$event.dto->getActivity()}
{ assign var='var' value="." }
{ assign var='test3' value=$test1|cat:$var}
{ assign var='test4' value=$test3|cat:$test2}

{text %.$test4}

How to set up cron job in Ubuntu using Webmin ?

Log in to Webmin at https://x.x.x.x:10000.
Click System
Click Scheduled Cron Jobs
You will see the list of create cron job
Click on Create a new scheduled cron job
You would be asked to fill up cron details
Select … for Execute cron job as and select the user under which the task will execute, e.g. root
The Active? field can be set to No if you don’t want this new job to be actually executed.
Enter the shell commands you want the Cron job to run in the Command field.In command section write the following code.

/usr/bin/wget "http://IP ADDR/PHP_FILE_PATH/FILE_NAME.php"

Anything that you enter into the Input to command field will be fed to the command as input when it is run.
Choose the times and dates on which you want the command to execute under When to execute.
Click the Create button to add the new Cron job.

Wednesday, 9 January 2013

Welcome to

Hello Friends

I am Sahil, I am starting this blog for PHP community to share my experiences