Wednesday 16 July 2014

Redirect HTTP to HTTPS using mod_rewrite

Redirect http to https htaccess


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond $1 !^children/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

</IfModule>

Sunday 13 July 2014

Connect to BSNL Broadband using Router

Configure Beetel 110TC1 ADSL2+ Router for BSNL


Open http://192.168.1.1 in your browser. Use the following username password to login

username : admin
password : admin or password

You should be greeted with the Beetel 110TC1 configuration page.
Click on Interface Setup
For Virtual Circuit, you can choose either the default PVC0 or PVC2. If you choose PVC0, then you need to set VPI = 0 and VCI = 35 (This is specific to BNSL).
 If you choose PVC2, the default values are 0 and 35.


NB :- For me  Virtual Circuit:  Status PV0 is Deactivated with VPI = 1 and VCI = 32
  
PVC2 status Activated with VPI = 0 and VCI = 35
Servicename : BSNL
Username:uname
Password: password
Encapsulation:  PPPoE LLC
Bridge Interface: Deactivated
Get IP Address: Dynamic


Under the ‘PPPoA/PPPoE’ section enter the user name/password combination provided by BSNL. For me the username as first two characters of my first name follower by my telephone number along with the STD Code. That is ni80xxxxxxxx. The password is ‘password’.
Save your changes.
The next step is to set the DNS entries, navigate to the sub-tab ‘LAN’ under ‘Interface Setup’.
Under DHCP, change the DNS Relay to ‘Use ZAuto Discovered DNS Server Only’. 

Tuesday 22 April 2014

jQuery: Difference between parent(), parents() and closest()

HTML

<xmp>
<html lang="en">
  <head>
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  </head>
  <body>
     <div>
        
            <a href="https://www.blogger.com/blogger.g?blogID=5288802523561781990#" id="anchorTag">Click here</a>
         


      </div>
</body>
</html>

jQuery
   $('#anchorTag').parent();      //This would return p, immediate parent of a#anchorTag.
parents(selector) : This returns the multiple ancestors. Its not restricted to only one level of parent/ancestors of the element. This is the main difference between the parent and parents method.
In the above HTML Snippet. If i do
   $('#anchorTag').parents();        //This would return all the ancestors (p, div, body, html)

   //On passing selector it will filter down the matched parent elements by selector

   $('#anchorTag').parents('div');   //This would give me only div
closest(selector) : It works like parents(), except that it returns only one parent/ancestor. This is ideal for the situation i mentioned earlier. If i want to check for existence of element in the ancestry tree, then i would prefer to use the closest rather than parents as it only target the selector rather than filtering it from all the ancestor elements. Here is example
$('#anchorTag').closest();           //This would give me empty object as we have not mentioned the selector
$('#anchorTag').closest('div');      //This would give me the div.
Hope this post has helped in terms of using the parent, parents and closest methods of jQuery library. Thank you for reading.

Monday 14 April 2014

Export database table data to excel format

Export database table data to excel format

Select the table to be exported to Excel Format
excel.php
<table style="width:100%;left:0px;position:relative;empty-cells:show;" border=0 cellpadding=0 cellspacing=0>
<tr height=50>
    <td style="color:#CC3300;font-size:13px;font-weight:bold;">Select the table to be exported to Excel Format</td>
   
</tr>
<tr>
    <td style="color:blue;font-size:12px;">Select Table</td>
</tr>
<tr>
    <td style="color:blue;font-size:12px;">
    <select name="selected_table" >
    <option value=""> </option>
    <?php 
            $fromdb=$_SESSION['COMPANY_DB'];
            $sql = "SHOW TABLES FROM ".$fromdb;
            $result = mysql_query($sql);
            while ($row = mysql_fetch_row($result))
            {
                        $table=$row[0];
                        echo "<option value=$table>$table</option>";
            }   
    ?>
    </select>
    </td>
</tr>
</table>
<BR></br>
<input type=submit name="export" value="Export">
export.php
if($export)
{
            $my_table=$_REQUEST['selected_table'];
            $result = mysql_query('select * from '.$my_table);
            $count = mysql_num_fields($result);
           
            for ($i = 0; $i < $count; $i++){
            $header .= mysql_field_name($result, $i)."\t";
            }
           
            while($row = mysql_fetch_row($result)){
            $line = '';
            foreach($row as $value){
            if(!isset($value) || $value == ""){
            $value = "\t";
            }else{
            # important to escape any quotes to preserve them in the data.
            $value = str_replace('"', '""', $value);
            # needed to encapsulate data in quotes because some data might be multi line.
            # the good news is that numbers remain numbers in Excel even though quoted.
            $value = '"' . $value . '"' . "\t";
            }
             
           
            $line .= $value;
            }
            $data .= trim($line)."\n";
            }
            # this line is needed because returns embedded in the data have "\r"
            # and this looks like a "box character" in Excel
            $data = str_replace("\r", "", $data);
           
             
            # Nice to let someone know that the search came up empty.
            # Otherwise only the column name headers will be output to Excel.
            if ($data == "") {
            $data = "\nno matching records found\n";
            }
           
            header("Content-type: application/x-msdownload");
            # This line will stream the file to the user rather than spray it across the screen
            //header("Content-Type: application/vnd.ms-excel; name='excel'");
           
           
           
            header("Content-Disposition: attachment; filename=excelfile.xls");
            header("Pragma: no-cache");
            header("Expires: 0");
           
            echo $header."\n".$data;
           
            //print "done";
}
header("Location: excel.php");

PHP Interview Questions

How to prevent web browser to image caching?       

The simple way to prevent image caching is to append time stamp with the name on image.
Ex- <img src=’image.jpg?<?php echo time() ?>’ />

What is the difference between Primary Key and Unique key?    

Both Primary Key and Unique Key enforces uniqueness of the column on which they are defined. But by default Primary Key creates a Clustered Index on the column, where are Unique Key creates a Non clustered Index by default. Another major difference is that, Primary Key doesn’t allow Nulls, but Unique Key allows one NULL only.

What is the difference between $name and $$name?

$name is variable where as $$name is reference variable
like $name=Sadiq and $$name=Akhtar so $Sadiq value is Akhtar.

What’s the difference between md5(), crc32() and sha1() crypto on PHP?        

The major difference is the length of the hash generated. CRC32 is, evidently, 32 bits, while sha1() returns a 128 bit md5() returns a 160 bit value. This is important when avoiding collisions.

jQuery interview questions


How is body onload() function is different from document.ready() function used in jQuery?

1. We can have more than one document.ready() function in a page where we can have only one onload function.
2. Document.ready() function is called as soon as DOM is loaded where body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.
  • Is jQuery a library for client scripting or server scripting?
    Ans: Client scripting
  • Is jQuery a W3C standard?
    Ans: No
  • What are jQuery Selectors?
    Ans: Selectors are used in jQuery to find out DOM elements. Selectors can find the elements via ID, CSS, Element name and hierarchical position of the element.
  • The jQuery html() method works for both HTML and XML documents?
    Ans: It only works for HTML.
  • Which sign does jQuery use as a shortcut for jQuery?
    Ans: $(dollar) sign.
  • What does $("div") will select?
    Ans: It will select all the div element in the page.
  • What does $("div.parent") will select?
    Ans: All the div element with parent class.
  • What is the name of jQuery method used for an asynchronous HTTP request?
    Ans: jQuery.ajax()
  • What is CDN and what are the advantage of loading jQuery framework from CDN?
  • How to load jQuery locally when CDN fails?
  • What is jQuery.noConflict()
  • Difference between $(this) and 'this' in jQuery
  • jQuery empty() vs remove()
  • jQuery remove() vs detach()
  • Is window.onload is different from document.ready()
  • How to check if element is empty
  • width() vs css('width') and height() vs css('height')
  • How to Check element exists or not in jQuery
  • Difference between bind() vs live() vs delegate() function
  • How to use jQuery Selectors with Examples
  • How to Disable-Enable all controls of page using jQuery
  • What are the possible problems can occur when jQuery and Ajax used in ASP.NET?
  • Why to use Google hosted jQuery CDN?
  • Q1. What is jQuery?
    Ans: jQuery is Lightweight and CrossBrowser JavaScript Library/Framework which helps in to traverse HTML DOM, make animations, add Ajax interaction, manipulate the page content, change the style and provide cool UI effect. The biggest advantage over Javascript is that it reduces lines of code as huge code written in JavaScript, can be done easily acheived with jQuery in few lines.

    Q2. Why jQuery?
    Ans: Due to following functionality.
           1. Cross-browser support (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)
           2. AJAX functions
           3. CSS functions
           4. DOM manipulation
           5. DOM transversal
           6. Attribute manipulation
           7. Event detection and handling
           8. JavaScript animation
           9. Hundreds of plug-ins for pre-built user interfaces, advanced animations, form validation etc.
           10. Expandable functionality using custom plug-ins

    Q3. Is jQuery replacement of Java Script?
    Ans: No. jQuery is not a replacement of JavaScript. jQuery is a different library which is written on top of JavaScript. jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. 

    Q4. What is the basic need to start with jQuery?
    Ans: To start with jQuery, one need to make reference of it's library. The latest version of jQuery can be downloaded from jQuery.com.

    Q5. What does dollar Sign ($) means in JQuery?
    Ans: Dollar Sign is nothing but it's an alias for JQuery. Take a look at below jQuery code. 
    $(document).ready(function(){
    });
    
    Over here $ sign can be replaced with "jQuery" keyword. 
    jQuery(document).ready(function(){
    });
    Q6. Is there any difference between body onload() function document.ready() function used in jQuery?
    Ans: document.ready() function is different from body onload() function due to 2 reasons.
           1. We can have more than one document.ready() function in a page where we can have only one body onload function.
           2. document.ready() function is called as soon as DOM is loaded where body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.

    Q7. Can we have multiple document.ready() function on the same page?
    Ans: YES. We can have any number of document.ready() function on the same page.

    Q8. What are the different type of selectors in Jquery?
    Ans: There are 3 types of selectors in Jquery
           1. CSS Selector
           2. XPath Selector
           3. Custom Selector

    Q9. Name some of the methods of JQuery used to provide effects?
    Ans: Some of the common methods are :
           1. Show()
           2. Hide()
           3. Toggle()
           4. FadeIn()
           5. FadeOut()

    Q10. What is JQuery UI?
    Ans: jQuery UI is a library which is built on top of jQuery library. jQuery UI comes with cool widgets, effects and interaction mechanism. 

PHP-Mysql Interview Questions

PHP, Ajax, Javascript , HTML, Mysql

How can we submit a form without a submit button?

The main idea behind this is to use Java script submit() function in order to submit the form without explicitly clicking any submit button. You can attach the document.formname.submit() method to onclick, onchange events of different inputs and perform the form submission. you
can even built a timer function where you can automatically submit the form after xx seconds once the loading is done (can be seen in online test sites).

In how many ways we can retrieve the data in the result set of MySQL using PHP?

You can do it by 4 Ways
1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc
 
What is the difference between mysql_fetch_object and mysql_fetch_array?

mysql_fetch_object() is similar tomysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).
 
What is the difference between $message and $$message?
 
It is a classic example of PHP’s variable variables. take the following example.$message = “Mizan”;$$message = “is a moderator of PHPXperts.”;$message is a simple PHP variable that we are used to. But the $$message is not a very familiar face. It creates a variable name $mizan
with the value “is a moderator of PHPXperts.” assigned. break it like this${$message} => $mizanSometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically.
 
How can we extract string ‘abc.com ‘ from a string ‘http://info@abc.com’
using regular expression of PHP?

preg_match(”/^http:\/\/.+@(.+)$/”,’http://info@abc.com’,$found);
echo $found[1];
 
How can we create a database using PHP and MySQL?

We can create MySQL database with the use of
mysql_create_db(“Database Name”)
 
What are the differences between require and include, include_once and require_once?

The include() statement includes and evaluates the specified file.The documentation below also applies to require(). The two constructs are identical in every way except how they handlefailure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missingfile to halt processing of the page.
include() does not behave this way, the script will continue regardless.
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only differencebeing that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.include_once() should be used in cases where the same file might be included and evaluated more than once during a particularexecution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.
require_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.
 
Can we use include (”abc.PHP”) two times in a PHP page “makeit.PHP”?
 
Yes we can use include() more than one time in any page though it is not a very good practice.

 
What are the different tables present in MySQL, which type of table is generated when we are creating a table in the following syntax:
create table employee (eno int(2),ename varchar(10)) ?

Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23 and as a result if we do not specify the table name explicitly it will be assigned to the default engine.
 
How can we encrypt the username and password using PHP?
 
The functions in this section perform encryption and decryption, and compression and uncompression:
encryption decryptionAES_ENCRYT() AES_DECRYPT()ENCODE() DECODE()
DES_ENCRYPT() DES_DECRYPT()
ENCRYPT() Not available
MD5() Not available
OLD_PASSWORD() Not available
PASSWORD() Not available
SHA() or SHA1() Not available
Not available UNCOMPRESSED_LENGTH()

Write a select query that will be displayed the duplicated site name and how many times it is duplicated ?

SELECT sitename, COUNT(*) AS NumOccurrences FROM tbl_sites GROUP BY sitename HAVING COUNT(*) > 1

 What does isNaN function do?

 Return true if the argument is not a number.