Friday, December 14, 2012

Finding geo location of the ip address in PHP



      If you are using  google map  in your  application and  wanted to  show the user location google map in your application.  then we  the find the  ip  address  and  get the  geo location of the  ip  address. Bellow  is the  method   to  find the  city  by  passing  the  ip  address Using  PHP.


/**
 * Funtion  to  find the  city by passing the  IP Address.
 */
function geoCheckIP($ip)
{

//check, if the provided ip is valid

    if (!filter_var($ip, FILTER_VALIDATE_IP))
    {

        throw new InvalidArgumentException("IP is not valid");
    }

//contact ip-server

    $response = @file_get_contents('http://www.netip.de/search?query=' . $ip);

    if (empty($response))
    {

        throw new InvalidArgumentException("Error contacting Geo-IP-Server");
    }

//Array containing all regex-patterns necessary to extract ip-geoinfo from page

    $patterns = array();

    $patterns["town"] = '#City: (.*?)<br#i';

//Array where results will be stored

    $ipInfo = array();



//check response from ipserver for above patterns

    foreach ($patterns as $key => $pattern)
    {

//store the result in array

        $ipInfo[$key] = preg_match($pattern, $response, $value) && !empty($value[1]) ? $value[1] : 'not found';
    }
    /* I've included the substr function for Country to exclude the abbreviation (UK, US, etc..)
      To use the country abbreviation, simply modify the substr statement to:
      substr($ipInfo["country"], 0, 3)
     */
    $ipdata = $ipInfo["town"];

    return $ipdata;
}

geoCheckIP('50.56.112.226');


For  more  dinformation and send  me comments.


Getting Client IP using PHP


          To  track the client  location we  need the ip  address of the  user machine/ mobile  device. As PHP is a  Server  side  script we  can  easilly  track the user/client ip  address using the bellow  php function.  



function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    //check ip from share internet
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    //to check ip is pass from proxy
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ip = getRealIpAddr();

For  feedback ,  leave  me comments.

Fetching the images based on screen resolution

      One  of the  main  issues in the  mobile web  application  is to  fetch  the  images  based on the  mobile  screen  resolution .  There  are  different  method to fetch  the  images  but  im here explain,   fetching the  image  based on the  screen  resolution.   Normally in the android  mobile  we  will  calculate the DPI(density per  inches) and  in the  iphone PPI(pixel per inches) . Here  im  taking the  screen  resolution.


    function getResolution()
    {
        window.imagePath ='';
        //get screen resolution of customer
        var custHeight = screen.height;
        var custWidth = screen.width;
        //if the  screed width  less  then  screen  height
        if( custWidth < custHeight)
        {    
            if (custWidth <=  320)
            {
                window.imagePath = 'drawable-mdpi';
            }
            else if (custWidth <= 480)
            {
                window.imagePath = 'drawable-hdpi';
            }
            else
            {
                window.imagePath = 'drawable-xhdpi';
            }
        }
        else
        {
            //else if the  screed width  greater  then  screen  height 
            if (custWidth <=  480)
            {
                window.imagePath = 'drawable-mdpi';
            }
            else if (custWidth <= 600)
            {
                window.imagePath = 'drawable-hdpi';
            }
            else
            {
                window.imagePath = 'drawable-xhdpi';
            }
        }    
    } 

Just  call  the getResolution() method  when  the  page  first  loads.  then use  the  window.imagePath anywhere in your  application. window.imagePath is the  global  varible  here.

This post  will really  help  you.  For  feed  back  leave  me comments.






Hidding the Address Bar in Mobile Browser


          All Mobile  web application need  address  bar hiding  option,  becuase basically  mobile  screens are very  small  in size  compared  to  the  Desktop.

      Use bellow to  code  to  hide the  address.  It  may help you  to  solve the  other  sort  of  challenges.

     function hideAddressBar()
    {
        if(!window.location.hash)
        {
        if(document.height < window.outerHeight)
        {
            document.body.style.height = (window.outerHeight + 50) + 'px';
        }
         setTimeout( function(){ window.scrollTo(0, 1); }, 200 );
          setInterval("hideAddressBar()", 6000);
        }  
    }
  

Use  the  code  and call function hideAddressBar(); whenever you  required to  hide  the  address  bar.

hope,  It  may help  you. If you have  any  query  leave  me comments.


Tuesday, April 3, 2012

Simulate submitting a form using cURL

                 So what is cURL anyway? cURL stands for “Client URL", and it is a library of functions that can be used to connect through a wide range of protocols, such as HTTP, FTP, telnet and so on. cURL also speaks HTTPS, so it can be used to communicate with secure servers.
What we are going to use is, cURL HTTP. cURL supports POST and GET methods, file uploads, cookies, user/password authentications, even using proxy servers for connecting.
It can literally be used to programmatically simulate browsing behavior. It can connect to a remote site, login by posting username and password to the login form or by using HTTP authentication, then retrieve pages or upload files. All of this using pure PHP code.

So how do I use cURL to post data?

Begin by creating a new connection.
  $curl_connection =
  curl_init('http://www.domainname.com/target_url.php');
A new connection is created using curl_init() function, which takes the target URL as parameter (The URL where we want to post our data). The target URL is same as the "action" parameters of a normal form, which would look like this:
<form method="post" action="http://www.domainname.com/target_url.php">
Now let's set some options for our connection. We can do this using the curl_setopt() function. Go to curl_setopt() reference page for more information on curl_setopt() and a complete list of options.
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_connection, CURLOPT_USERAGENT,
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

What options do we set here?
First, we set the connection timeout to 30 seconds, so we don't have our script waiting indefinitely if the remote server fails to respond.
Then we set how cURL will identify itself to the remote server. Some servers will return different content for different browsers (or agents, such as spiders of the search engines), so we want our request to look like it is coming from a popular browser.
CURLOPT_RETURNTRANSFER set to true forces cURL not to display the output of the request, but return it as a string.
Then we set CURLOPT_SSL_VERIFYPEER option to false, so the request will not trigger an error in case of an invalid, expired or not signed SSL certificate.
Finally, we set CURLOPT_FOLLOWLOCATION to 1 to instruct cURL to follow "Location: " redirects found in the headers sent by the remote site.
Now we must prepare the data that we want to post. We can first store this in an array, with the key of an element being the same as the input name of a regular form, and the value being the value that we want to post for that field.
For example,if in a regular form we would have:

<input type="text" name="firstName" value="Name">
<input type="hidden" name="action" value="Register">
we add this to our array like this:
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register'
Do the same for every form field.
Data will be posted in the following format:
key1=value1&key2=ವಲುಎ೨
In order to format the data like this, we are going to create strings for each key-value pair (for example key1=value1), put them in another array ($post_items) then combine them in one string using PHP function implode() .
foreach ( $post_data as $key => $value)
{
    $post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
Next, we need to tell cURL which string we want to post. For this, we use the CURLOPT_POSTFIELDS option.

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
Finally, we execute the post, then close the connection.

$result = curl_exec($curl_connection);
curl_close($curl_connection);
By now, the data should have been posted to the remote URL. Go check this, and if it did not work properly, use curl_getinfo() function to see any errors that might have occurred.
print_r(curl_getinfo($curl_connection));
This line displays an array of information regarding the transfer. This must be used before closing the connection with curl_close();
You can also see number and description of the error by outputting curl_errno($curl_connection) and curl_error($curl_connection).
So let's put everything together. Here is our code:


<?php
//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
  curl_init('http://www.domainname.com/target_url.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
                curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
?>

Post form data without using cURL

If your hosting server does not come with cURL installed (though this is rare as cURL is installed on most commercial hosting servers) and you also don’t have access to server in order to install it, there are alternatives.
One of them is using PHP’s functions fsockopen() and fputs() to send properly formatted data to a remote server. Here is a sample of code that does just this:
<?php
//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;
//we are going to need the length of the data string
$data_length = strlen($post_string);
//let's open the connection
$connection = fsockopen('www.domainname.com', 80);
//sending the data
fputs($connection, "POST  /target_url.php  HTTP/1.1\r\n");
fputs($connection, "Host:  www.domainname.com \r\n");
fputs($connection,
    "Content-Type: application/x-www-form-urlencoded\r\n");
fputs($connection, "Content-Length: $data_length\r\n");
fputs($connection, "Connection: close\r\n\r\n");
fputs($connection, $post_string);
//closing the connection
fclose($connection);
?>