function findmatch() {
 if (window.XMLHttpRequest) {
 xmlhttp = new XMLHttpRequest();
 } else {
 xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');

xmlhttp.onreadystatechange = function() {
 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
 document.getElementById('results').innerHTML = xmlhttp.responseText;

xmlhttp.open('GET', 'search.inc.php', true);




<form id="search" name="search">
 Type a name:<br>
 <input type="text" name="search_text" onkeydown="findmatch();">

</body> </html>



 if(isset($_GET['search_text'])) {
 $search_text = $_GET['search_text'];
 echo $search_text;


Comments Off on Ajax

Filed under Uncategorized


Server variable-

  1.  _SERVER[‘SCRIPT_NAME’]  => will show current page name on which this fun called
  2. _SERVER[‘HTTP_HOST’] => Show server IP.
  3. _SERVER[‘REMOTE_ADDR’]  => Non reliable  way to get user’s IP.
  4. _SERVER[‘HTTP_CLIENT_IP] => Give router ip if there is router other wise client IP.
  5. _SERVER[‘HTTP_X_FORWARDED_FOR] => If user using proxy, it will give proxy IP.


  1. header(‘Location: ‘.$redirect_location);
  2. <?php ob_start();?>  …… <?php ob_end_flush/clean();


Use htmlentites( $_GET[‘var’]) for security purpose other wise user can input dangerous code. when you are displaying data get from POST and GET methods always use HTMLENTITES() function.



  1. <?php
  2. session_start ();
  3. $_SESSION[‘name’] = ‘amit’;
  4. ?>


  1. <?php
  2. session_start();
  3. echo $_SESSION[‘name’];
  4. ?>


  1. <?php
  2. session_start();
  3. unset( $_SESSION[‘name’]); // only delete single session with has name ‘name’
  4. session_destroy();  //this will delete all session data for current user
  5. ?>


Cookie- session destroy when user close client(browser) while cookie can set for years.

  1. setting a cookie (set.php)
    • setcookie(‘username’, ‘alex’, time()+60);  //here 60 is 60 seconds
  2. view.php
    • echo $_COOKIE[‘username’];
  3. delete cookie (unset.php)
    • setcookie(‘username’, ‘alex’,  time()-500);   // change time to past


Read XML file- simplexml_load_file(‘fileName’);

  • example.xml
                     <name> Alex </name> 
                     <name> amit </name>
  • index.php
    1. <?php
    2. $xml = simplexml_load_file(‘example.xml’);
    3. echo $xml->producer[1]->name;
    4. //or
    5. foreach ($xml->producer as $producer) {
    6.      echo $producer->name;
    7. ?>

Connect php with Mysql-

  1. mysql_connect(‘host_ip’, ‘user_name’, ‘password’);
  2. mysql_select_db(‘mysql_db_name’);
  3. Search a query in php-
    • $query = select `name` from `table_name` order by `id`;
    • mysql_query($query);
    •  mysql_error();  //will return error from last query.


$connect_error = 'Could not connect!';

$mysql_host = "localhost";
$mysql_name = 'root';
$mysql_pass = '';

$mysql_db = 'amit_db';
if(!mysql_connect($mysql_host, $mysql_name, $mysql_pass) || !mysql_select_db($mysql_db)

required 'connect.php';

$query =  "select `name` from  `user_table`  order by `id`";
if( $query_run = mysql_query($query) )
   while($query_row = mysql_fetch_assoc($query_run)
      $name = $query_row['name']'
      echo " user name is $name ";
else {
       echo mysql_error();



Leave a comment

Filed under Uncategorized


Array Functions II

Another common thing to do with arrays is sort them. Handily enough, PHP has a sort() function for just such an occasion!

$array = array(5, 3, 7, 1);
print join(", ", $array);
// prints "1, 3, 5, 7"

PHP also has the opposite function:rsort().

$array = array(5, 3, 7 ,1);
print join(":", $array);
// prints "7:5:3:1"
Lastly, we use join(glue, array) so we can easily print out the representations of our sorted arrays in this exercise.

Function exercise –

Create an array() with the names of your closest family and friends. The more names in your array, the more fun your game will be! After you’ve created your list, sort it and randomly select a name from the list.  When you have your winning name,print it to the screen in caps so everyone knows how awesome the winner is.

Functions we’ll use here: array(),array_push(), sort(), count(),rand(), and strtoupper(). Play around with some of the other functions you’ve learned as well.


PHP String function –

$stringInLowerCase = strtolower(string);

strstr() Finds the first occurrence of a string inside another string (case-sensitive)
strtolower() Converts a string to lowercase letters
substr_replace() Replaces a part of a string with another string


substr_ireplace() Replaces a part of a string with another string ignoring case

we can also use array of string inside substr_replace to replace

Leave a comment

Filed under Uncategorized

Design Pattern

Observer design pattern-

Q- If  subject’s state changes to frequently how will you modify subject so that he notify observer only on an important event.

A- Use the setChanged type of method for more details check Head first design pattern page 78 (104/681).

Q- How different design principle apply on observer design pattern?



Observer Design Patter and OOP Principles

Decorator design pattern-

Decorator design pattern is based on a class should be closed for modification and open for extension. and uses composition to achieve runtime Power to decorate an object.

A disadvantage of Decorator is  sometimes it add a lot of small subclasses in design and this occasionally results in a design that is less straightforward for others to understand.

Leave a comment

Filed under Uncategorized

Dependency, Association, Aggregation and Composition

Dependency –

A dependency typically (but not always) implies that an object accepts or receive a reference or object to a class as part of  as a method parameter, instantiates, or uses another object. And a modification in accepted class may break current class as well. Dependency is represented by dashed arrow starting from dependent class to its dependency. A dependency is very much implied by an association.

Association –

its represent the ability of one instance to send a message to another instance. This is typically implemented with a pointer or reference instance variable althogh it might also be implemented as a method argument, or the creation of a local variable. In simple words, it  describes how many elements are taking part in an interaction.

Aggregation –

Aggregation and composition are two types of association. Aggregation differs from ordinary composition in that it don’t imply ownership. In composition when the owning object is destroyed, so are the contained objects.

Leave a comment

Filed under Uncategorized

Difference between c and C++

  1. C is procedural language and C++ is procedural and oop language.
  2. C does not support public, private way of data hiding C++ support while in C you can do some-type of implementation hiding by opaque data structure.
  3. C does not support namespace while C++.
  4. C does not have exception handling while c++ support it.
  5. C is not a OOP language so it does not support  inheritance, encapsulation, polymorphism, virtual function ,operator overloading.
  6. C does not have constructor destructor concepts.
  7. C does not have RAII or smart Pointer mach.
  8. C does not have template.
  9. In C default linkage of const variable is external while in C++ it is internal linkage that’s why you can  create array of const specific size. below code will work only in c++.
    1. const int size =10;
    2. int arr[size]
  10. C++ support inline function.
  11. C++ support different STL containers.

Leave a comment

Filed under C++, Uncategorized

Kernel FAQ!

  1. http://www.careercup.com/question?id=2754
  2. As kernel can access user space memory, why should copy_from_user is needed?
    1. one of the major requirement in system call implementation is to check the validity of user parameter pointer passed as argument, kernel should not blindly follow the user pointer as the user pointer can play tricks in many ways. Major concerns are: 1. it should be a pointer from that process address space – so that it cant get into some other process address space. 2. it should be a pointer from user space – it should not trick to play with a kernel space pointer. 3. it should not bypass memory access restrictions.
      that is why copy_from_user() is performed. It is blocking and process sleeps until page fault handler can bring the page from swap file to physical memory.
    2. If kernel directly accesses the user data structure, system will panic if it’s not a valid address( eg NULL pointer). To avoid this situation, copy_from_user(..) is used. This function will fail, if proper user address is not provided and does not bring down the system.
  3. how a function from one user process can be called in other user process?
    1. Use Remote Procedure Calls (RPC) to call functions in another process ???
  4. mention 4 IPCs used in user level process in linux
    1. PIPE, Queue, Socket, Shared memory, signal, semaphore.
  5. what is bus error? common causes of bus errors?
    1. The first thing that needs to be addressed is: What is a bus? A bus is a communication unit that allows the CPU to interact with peripherals, there are different type of buses such as PCI, I2C, MDIO, Memory Buses, etc. Normally each bus would have its own protocol for transmitting data across devices, for example in the case of PCI you can have timeout errors or windows errors (data is directed to unknown addresses/devices). In memory, bus errors would refer to alignment but other errors could be attributed to physical HW problems such as faulty connections. Other type of bus errors could be single and multiple bit errors, this could be addressed by using ECC memory.
  6. . describe the steps to add a new global sysctl() or fnctl() paramater.
    1. sysctl is used to modify kernel parameters at runtime. The parameters are listed under /proc/sys/.
      Procfs is required for sysctl support in Linux. You can use sysctl to both read and write sysctl data.
      You must login as the root user to use any one of the following command.
      Method # 1: Setting value via procfs
      You can use standard echo command to write data to variables:
      # echo “value” > /proc/sys/location/variable
      Method # 2: Temporary on the command line
      Use sysctl command with -w option when you want to change a sysctl setting:
      sysctl -w variable=value
      Method # 3: Configuration file /etc/sysctl.conf
      This is recommended way. First open /etc/sysctl.conf file
      # vi /etc/sysctl.conf
      Now add value:
      variable = value
      Close and save the changes. Type the following command to load sysctl settings from the file /etc/sysctl.conf file:
      # sysctl -p

      1.  sysctl -a to see available parameters.
  7. how to find a child process in unix
    1. pstree
  8. What is difference between wake_up ( ) and wake_up_interruptible ( ) APIs in linux kernel ?
    When should should use which one, how it should be decided ?
  9. User application will block on IOCTL, in kernel untill interrupt arrives this IOCTL will be blocked. When interrupt comes IOCTL should retun. Desing this mechanism.
  10. What is difference between sleep_on ( ) and interruptible_sleep_on ( ) APIs in linux kernel ?
    When should should use which one, how it should be decided ?
  11. How to write a perticular value on real hardware register or hardware address in device driver programming ?
    1. It all depends on how the real hardware is connected to the device driver module (like I2C bus etc). If the device driver can access the hardware register (like a I/O mapped memory) we have to type cast the memory of the register address to the pointer of the data type it stores then write to that pointer address. Ex:
      *(uint16 *)0x1234 = (uint16) data;
  12. How SpinkLocks work in SMP and UP architectures.
    1. On UP,
      With preemption enabled, spinlock will disable preemption.
      With preemption disabled, spinlock is no-op.
  13. How do you debug a process which is consuming 90% of CPU?
    1. get the pid of process with top command  one way to debug is use oprofiler. enable opfrofiler in kernel and compile code now start oprofiler using opcontorl -start and launch that process. you will come to know that which part of the process consuming to much cpu.
    2. 2nd way is attach gdb to that process id and take bt to know what is the stack dump of process
  14. In unix, what is a defunct process ?
    1. Its a zombie. A child process that is dead but its entry in the process table hasnt been removed so that the parent process can get the exit status information of this child.
      Use ps v -u username and check under STAT column with Z value.
  15. After fork, does new process get file handles and locks?
    1. The child process has its own copy of the file decriptors for the parent process. Each of the file descriptors for a child process refer to the same open file description with the corresponding file descriptor of the parent process.
      The child process does not inherit file locks set by the parent process.
  16. how can you periodically print the cpu usage of a given process id in Linux
    1. top -p <pid>

Leave a comment

Filed under Uncategorized