Tuesday, January 22, 2013

Difference Between Perl and CGI

CGI is a standard that provides an interface between a webserver, such as Apache, and clients through a (CGI) script, which can be written in any programming language. However, scripting languages are often used. CGI Scripts take the request from client and will call appropriate functions to return the result to the requested clients. There are many language that could function as CGI language like Perl, C, C++, Tcl, Unix Shell Script, etc. However, Perl is without a doubt the most used languages for CGI scripting.

Perl is itself a high-level, interpreted, dynamic programming language. It was originally developed way back in 1987 to make generating reports easier. It caught on with programmers and has grown and expanded ever since, leading up to the latest release of Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary Unix tools, facilitating easy manipulation of text files. It is also used for system administration, network programming, and applications that require database access. Perl is nicknamed “the Swiss Army chainsaw of programming languages” due to its flexibility and adaptability.

Page Refresh using php script

This a nice one in php.A Refresh or reload or redirect page using php script or function.just amazing functionality provide by php.Here we can not require any javascript,jquery or ajax to do this.Use only simple php " header() " function to perform this task.We can use this function any where in project or website.




Example :

//refresh after 5 second
header("refresh:5;");

//reload after 5 second to specific path
header("refresh:5; test.php");

Find Odd And Even Number in Php

Find Odd Or Even Number using php Script.Here two way to find this solution just see the below php script.

First : Modulo Operator (%)

$num=10;
if($num%2==0)
{
echo "Even";
}
else
{
echo "Odd";
}  

Second Way :Bitwise Operator '&'
$num=9;
if($num&1)
{
echo "Odd"; 
}
else
{
echo "Even"; 
}

Wednesday, January 9, 2013

Detecting Ajax Request with PHP Script

Here’s a script that  useful to check if a request that comes to a PHP page was made via an Ajax call or a simple form post. This method uses the $_SERVER['HTTP_X_REQUESTED_WITH'] request to determine if data was sent to a specific page using an xmlhttprequest.The script is pretty self explanatory,we are  checking to see if the request was sent via an xmlhttprequest.





Example Script :
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
// If its an ajax request execute the code below
echo 'This is an ajax request!';
exit;
}
//if it's not an ajax request echo the below.
echo 'This is clearly not an ajax request!';

Automatic Load a page Using Ajax

Here we can learn about how to Automatically load page after 5 second using ajax and jquery.First we get little information about Ajax.
AJAX = Asynchronous JavaScript and XML.
AJAX is not a new programming language, but a new way to use existing standards.
AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.


Example script :
Create two files :
1) main file
2) load file
create main.php this is main file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">
</script>
<script>
 setInterval(
function()
{
$('#content').fadeOut('slow').load('load.php').fadeIn("slow");}, 3000);
</script>
<title>Auto Load Page Div using Jquery - similar to twitter Live Search</title>
</head>
<body>
<div id="content" style="background-color:#ffffcc;font-size:24px;font-weight:bold;width:600px;margin:auto 10px;">
Please wait.</div>
<a href="#" target="_blank">Home</a>
</body>
<html>
The second file is load.php this file load every 5 second.
echo 'This content is loaded  via ajax every 5 seconds ..';