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 ..';

Facebook Share by php script

Share your content into facebook wall.you can use facebook in build code or javascript code or php code.
here we can use php code to share link into facebook wall.its very simple to use.
Just Add Your URL and Title.
  

Example Script:
//msg to share 
 $tt = "hello boss"; 
 //which url you want to share  
$url_p = "http://tutjunction.com"; 
 $url = urlencode($url_p);  
//share by 
 $text = "jaspal rana";  
$title = urlencode($text); 
 //facebook api  
$fb = "http://www.facebook.com/sharer.php?t=".$title."&u=".$url;

<!--click this link to share-->
<a href="<?php echo $fb; ?>">share link</a>

Facebook Like Button Script

Here we learn how to add facebook subscribe into your application or project.Facebook like button code for any url.Its done by facebook javascript library.





Use following code :

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

//data-href =add you facebook link

<div class="fb-subscribe" data-href="https://www.facebook.com/url" data-layout="box_count" data-show-faces="true" data-width="450">
</div> 
 
Screenshot :
 

Facebook Registration Form Plugin

Facebook Registration Plugin when we can use.just simple use this code into your website.Its completedone in Html IFrame.It's very easy.







Just Run this Code :

<iframe style="" src="https://www.facebook.com/plugins/registration? client_id=113869198637480& redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fecho%2F& fields=name,birthday,gender,password,location,email" frameborder="no" scrolling="auto" width="100%" height="330"></iframe>



 
 

Check Domain Name Using Php Script

We can check our DNS record to verify the existence of a domain name or not. We can see if facebook.com is there in DNS record by using checkdnsrr function. This function returns True if available and returns False if not available.The type can be A, MX, NS, PTR,SOA, AAAA, CNAME, SRV,A6 NAPTR, TXT or ANY.




Here is the simple code to check DNS records.

$domain="facebook.com";
if(!checkdnsrr($domain,'MX')) {
echo "Not there ";
}else {
echo " It is there "; }
 
//out put: It is there 

 

Create PDF File using Dompdf

The best option for creating pdf file or report,I suggest only Dompdf.The Dompdf is fullfill all requirement.In Dompdf you have to create simple pdf file,dynamic data file,generate database related pdf file.dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP.dompdf is an HTML to PDF converter.


 
Download Dompdf :
  1. Link 1
  2. Link 2
  3. Link 3
Example For Create PDF :
$html = ("<html>");
 $html .= ("<head>");
 $html .= ("<style>");
 $html .= ("</style>");
 $html .= ("</head>");
 $html .= ("<body>");

$html.=("<table width='500' border='1' cellspacing='1' cellpadding='1'>\n");
$html .= ("<tr>");
$html .= ("<td>hello</td>");
$html .= ("<td>welcome</td>");
$html .= ("</tr>");
 $html .= ("</table>");
 $html .= ("<h2>Generated PDF</h2>\n");
 $html .= ("<p>date_today</p>\n");
 $html .= ("</div>");

 $html .= ("<p>$table->COMMENT</p>\n");

 $html .= ("</body>");
 $html .= ("</html>");

 function render($html,$filename)
 {
  require_once("../dompdf_config.inc.php");
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->set_paper("a4", "landscape" );
  $dompdf->render();
  $dompdf->stream("$filename.pdf");
 }

 $filename = ("GeneratedPDF");

 render($html,$filename);
}
 

Hide URL From Browser Using Curl Script in Php

First you have to intialise the curl function using ” curl_init()  “.
then set url into curl function like  “curl_setopt($my_curl, CURLOPT_URL, “http://tutjunction.com/”)” . 
then set header false by ” CURLOPT_HEADER “.  
then afer execute the curl and close the curl connection.

Example : 

$my_curl = curl_init();// cURL resource

// WE will add options to our curl
curl_setopt($my_curl, CURLOPT_URL, "http://twitter.com/");
curl_setopt($my_curl, CURLOPT_HEADER, 0);

curl_exec($my_curl); //execute curl function now and display
curl_close($my_curl); // close the curl connection

Create Ms Word File Using Php Script

Here use simple php file function for creating ms word or doc report dynamic.Use following code. Step 1:  

$fp = fopen("Report.doc", 'w+');

The fopen() function is used to open files in PHP.
The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:

Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists.

$str="welcome";  
add file content to $str.
fwrite($fp, $str);
fclose($fp); 
write data into file and finally close the file.
Final Code : 
$fp = fopen("reports/Assign_Report.doc", 'w+');
$str ='welcome';
fwrite($fp, $str);
fclose($fp);