Saturday 27 August 2011

PHP code to generate your own captcha



3 Major Anti-spamming techniques used:
  1. Mathematical Operation like Random number + Random Number = -> The user must specify the answer
  2. Random word -> User must type the word
  3. Random question -> Obvious one which the user should answer correctly [ex: Are you human?]
How Captcha works?
<?php //This should be the first line as in the rule book :D  
session_start(); 
 //These variables store the Question and the answer
$ques ""; $ans "";  
//This is the MAJOR array, this holds all the random things, 
//like the question you need to ask. You can add up new ones easily 
 $words = array(
        
=> array("Num" => "Num"),
        
=> array("Are you human?" => "yes"),
        
=> array("Type 'one' " => "one"),
        
=> array("Type 'test' " => "test"),
        
=> array("AxHGA" => "AxHGA"),
        
=> array("zontek" => "zontek"),
        
=> array("12terd " => "12terd")
    );
    
//Now we need to pic up a random question, 

    //array_rand is the perfect thing to this
    
$r array_rand($words);
    
//Then we check about what we have in the select array
    
switch(key($words[$r])){
        
//If we have the "NUM" selected, that is a special one
        //Num means the user will be prompted to

        //do a simple addition like 5+6
        
case "Num":
            
//Pretty basic stuff, generate 2 random numbers and

            //tell the user to put the addition
            
$i rand(1,10);
            
$j rand(1,10);
            
$ans $i+$j;
            
$ques "$i + $j = ";
            break;
        default:
            
//If not a number, ask the user a question or

            //ask him to type a word
            
$key key($words[$r]);
            
$ques $key;
            
$ans $words[$r][$key];
            break;
    }
//NOW we put up the answer to the session variable 

 $_SESSION['cap'] = strtolower($ans); 
 //This would change the content type, 
 //or in english this would tell the browser that
 //what ever retuened by this script is an image 

 header('Content-Type: image/png');
//Following code is to generate the image from the test
//We first specify colour ranges, you can refer to the php manaul for more
 

$img imagecreatetruecolor(250,30);
//In the above code, the image size is set to 250x30  
$white imagecolorallocate($img,255,255,255);  
$grey imagecolorallocate($img,128,128,128); 
$black imagecolorallocate($img,0,0,0); 
 //Filling the rectangle with white as we need black text on white 
 imagefilledrectangle($img,0,0,399,29$white); $text $ques
 //THE below code is CRITICAL. This is the palce where we tell which font to use.
//Choose any ttf you like and name it as font.ttf 

//or change the following code, make sue
//you put the path to the file correctly 

//[i used STENCIL so that parsers will find it hard to detect]  
$font "./font.ttf";  
imagettftext($img,20,0,11,21,$grey,$font,$text); 
imagettftext($img,20,0,10,20,$black,$font,$text);  
//Creating a PNG image, i use png cuz i <3 png
//[really its so small and efficient ;)]  
imagepng($img); 
 //And then remove the memory parts once the output is given  
imagedestroy($img);  
?>

Write down index.php accordingly! It's done!

Download Script in PHP with a Countdown Timer before download Begins

Got the vacations and so it's coding time. Pretty basic php based download script with a counter before starting the download, you may have seen those kind of script sources all around. But I made some arrangements for this code to make it a bit more than a simple downloading script. Lets start then.


Background



What is a Download Script?

The best way to explain is the sites like sourceforge.net and cnet.com where when you select a file to download, it wont give you a direct link, but directs you to a page where a download will start after soem seconds or immediately or even you can select another mirror and stuff.

Why to use a Download Script?
  1. It makes your site/application more professional.
  2. You can easily implement things like download counter to the page.
  3. Can reduce a bit of certain automated scripts from downloading your script
How it Works?

Those scripts change the header content type through the script so the browser identifies anything comming from that script as something downloadable.
  1. download.php -> handle the things that the user sees
  2. downloader.php -> prepares and send the file to the user without showing to the user
Logic: Most of the time, download.php will be using an iframe to load the downloader.php and hence the user see no difference

The Code

  1. First we need to make a table with the links to the files. Use the following query for the table. You should add ALL the links that you allow the use to download. The download script is called from the "ID" of the selected product.

    Code:
    --
    -- Table structure for table `downloads`
    --
    
    CREATE TABLE IF NOT EXISTS `downloads` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `Name` text NOT NULL,
      `Link` text NOT NULL,
      `Counter` int(11) NOT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
  2. So we have the the table, add up some products. For this remeber the following tips:
    • We will be placing our download script in the root directory so you can easily access all the files elsewhere
    • Have relative links, and reduce having spaces and long names as file names.

    Let's add up some records as per the following fields:
    • ID - This is an Auto Increment value saying the product ID
    • Name - Product Name, used for the convenience of the user
    • Link - The MOST vital part, holds a RELATIVE link for the file
    • Count - Take how many time the file is downloaded
    Code:
    INSERT INTO `downloads` (`ID`, `Name`, `Link`, `Counter`) VALUES
    (1, 'Product1', 'files/prod1.zip', 0);
  3. Done with databases, now lets got for the real scripting. First we need a download.php script which will be the one interacting with the user
    ex: http://localhost/mysite/download.php?id=1

    download.php
    PHP Code:
    <?php
    define
    ("db_user","root"); //your databse user name define("db_pwd",""); //your database password define("db_name","zontek");  //your databasename define("db_host","127.0.0.1");
         
          
    //First getting the id specified as download.php?id=1
        
    $id htmlspecialchars($_GET['id']);
        
    //This variable is a bool to check for any error, as we should have tight security
        
    $error false;
        
    //Connecting to the database
        
    $conn mysql_connect(db_host,db_user,db_pwd);
        if(!(
    $conn)) echo "Failed To Connect To The Database!";
        else{   
            if(
    mysql_select_db(db_name,$conn)){
                try{
                    
    $qry "SELECT Name,Link FROM downloads WHERE ID=$id";
                    
    $result mysql_query($qry);
                    
    //Check whether there's a result with the id number
                    
    if(mysql_num_rows($result)==1){
                        while(
    $rows mysql_fetch_array($result)){
                            
    //If there's one take the details into variables
                            
    $nam $rows['Name'];
                            
    $link $rows['Link'];
                        }
                        
    //Updating the counter by 1
                        
    $qry "UPDATE downloads SET Counter = Counter+1";
                        
    mysql_query($qry);
                    }else{ 
    $error true; }
                }catch(
    Exception $e){
                    
    //If an Exception occurrs make $error true
                    
    $error true;
                }       
         
    // At last check whether $error is true, if so immediatly redirect to the home page so that an attacker cannot take use of any error
         
    if($errorheader("Location: index.php");           
               
            }
         }
    ?> <html>
    <head>
    <title>Download Script</title>
    <script type="text/javascript">
        window.onload = function(){
            //Creates a new iframe
            var ele = document.createElement('iframe');
            //Navigate the iframe to the downloader.php which in turn return the file
            ele.src = "downloader.php?id=<?php echo $id?>";
            //Hide the stuff from the user
            ele.style.display = "none";
            //Add the iframe to the document body
            document.body.appendChild(ele);
        }
    </script>
    </head>
    <body>
    Thank You For Downloading <?php echo $nam?> <!-- note the use of the Name field to show the user what he is downloading -->
    </body>
    </html>
  4. Now we make the downloader.php which will recieve the same ID as the download.php and then it will return a file back.

    downloader.php
    PHP Code:
    <?php
    define
    ("db_user","root"); //your databse user name define("db_pwd",""); //your database password define("db_name","zontek");  //your databasename define("db_host","127.0.0.1");
         
     
    $id htmlspecialchars($_GET['id']);
     
    $error false;
        
    $conn mysql_connect(db_host,db_user,db_pwd);
        if(!(
    $conn)) echo "Failed To Connect To The Database!";
        else{   
            if(
    mysql_select_db(db_name,$conn)){
                
    $qry "SELECT Link FROM downloads WHERE ID=$id";
                try{
                    
    $result mysql_query($qry);
                    if(
    mysql_num_rows($result)==1){
                        while(
    $rows mysql_fetch_array($result)){
                            
    $f=$rows['Link'];
                        }
                       
                        
    //Code above this part was more or less the same as the previous, but the code below plays an IMPORTANT role
                       
                        //First we need a name to the file we are sending to the user, by defualt it will be saying something which include the complete path + file name and some blahh. But it is better to have the same sweet name you put for the original fiel to be here as well :D
                        //pathinfo returns an array of information
                        
    $path pathinfo($f);
                        
    //basename say the filename+extension
                        
    $n $path['basename'];
                        
    //NOW comes the action, this statement would say that WHATEVER output given by the script is given in form of an octet-stream, or else to make it easy an application or downloadable
                        
    header('Content-type: application/octet-stream');
                        
    //This would be the one to rename the file
                        
    header('Content-Disposition: attachment; filename='.$n.'');
                        
    //Finally it reads the file and prepare the output
                        
    readfile($f);
                        
    //If you goto to the same page with your browser a download will start immediately
                    
    }else $error true;
                }catch(
    Exception $e){
                    
    $error true;
                }
                if(
    $errorheader("Location: index.php");
            }
      }
    ?>
  5. Now everything's fine, when you goto http://localhost/mysite/download.php?id=1 and if you have the file prod1.zip in files directory (files/prod1.zip) your download will start immediately or giveout a status bar saying a file is going to download.

    Lets put up a bit of a code so that a counter of 5 second go before the thing starts so that if the user doesnot wanna download the file he can quit before it. For that edit the html part of download.php as follow:
    PHP Code:
    <html>
    <head>
    <title>Download Script</title>
    <script type="text/javascript">
        //Count to 5 seconds
        var c = 6;
        window.onload = function(){
            count(); //Execute the method
        }   
        function count(){
            c -= 1;
            //If the counter is within range we put the seconds remaining to the <span> below
            if(c>=0) document.getElementById("time").innerText = " in " + c + " Seconds";
            else{
                //After the counter download the file and end the timer
                document.getElementById("time").innerText = "Now";
                download();
                return;
            }
            var counter2 = setTimeout("count()",1000);
            return;
        }
        function download(){
            //Creates a new iframe
            var ele = document.createElement('iframe');
            //Navigate the iframe to the downloader.php which in turn return the file
            ele.src = "downloader.php?id=<?php echo $id?>";
            //Hide the stuff from the user
            ele.style.display = "none";
            //Add the iframe to the document body
            document.body.appendChild(ele);
        }
    </script>
    </head>
    <body>
    Thank You For Downloading <?php echo $nam?> <!-- note the use of the Name field to show the user what he is downloading --><br />
    Your download will start <span id="time"></span>.
    </body>
    </html>
    Now the new file is like
    download.php
    PHP Code:
    <?php
    define
    ("db_user","root"); //your databse user name define("db_pwd",""); //your database password define("db_name","zontek");  //your databasename define("db_host","127.0.0.1");
         
          
    //First getting the id specified as download.php?id=1
        
    $id htmlspecialchars($_GET['id']);
        
    //This variable is a bool to check for any error, as we should have tight security
        
    $error false;
        
    //Connecting to the database
        
    $conn mysql_connect(db_host,db_user,db_pwd);
        if(!(
    $conn)) echo "Failed To Connect To The Database!";
        else{   
            if(
    mysql_select_db(db_name,$conn)){
                try{
                    
    $qry "SELECT Name,Link FROM downloads WHERE ID=$id";
                    
    $result mysql_query($qry);
                    
    //Check whether there's a result with the id number
                    
    if(mysql_num_rows($result)==1){
                        while(
    $rows mysql_fetch_array($result)){
                            
    //If there's one take the details into variables
                            
    $nam $rows['Name'];
                            
    $link $rows['Link'];
                        }
                        
    //Updating the counter by 1
                        
    $qry "UPDATE downloads SET Counter = Counter+1";
                        
    mysql_query($qry);
                    }else{ 
    $error true; }
                }catch(
    Exception $e){
                    
    //If an Exception occurrs make $error true
                    
    $error true;
                }       
         
    // At last check whether $error is true, if so immediatly redirect to the home page so that an attacker cannot take use of any error
         
    if($errorheader("Location: index.php");           
               
            }
         }
    ?> <html>
    <head>
    <title>Download Script</title>
    <script type="text/javascript">
        //Count to 5 seconds
        var c = 6;
        window.onload = function(){
            count(); //Execute the method
        }   
        function count(){
            c -= 1;
            //If the counter is within range we put the seconds remaining to the <span> below
            if(c>=0) document.getElementById("time").innerText = " in " + c + " Seconds";
            else{
                //After the counter download the file and end the timer
                document.getElementById("time").innerText = "Now";
                download();
                return;
            }
            var counter2 = setTimeout("count()",1000);
            return;
        }
        function download(){
            //Creates a new iframe
            var ele = document.createElement('iframe');
            //Navigate the iframe to the downloader.php which in turn return the file
            ele.src = "downloader.php?id=<?php echo $id?>";
            //Hide the stuff from the user
            ele.style.display = "none";
            //Add the iframe to the document body
            document.body.appendChild(ele);
        }
    </script>
    </head>
    <body>
    Thank You For Downloading <?php echo $nam?> <!-- note the use of the Name field to show the user what he is downloading --><br />
    Your download will start <span id="time"></span>.
    </body>
    </html>
So that's it, you can implement this easily to your site and such simple method will make you site look more user friendly and professional to the users and even your management of files become easier.

Convert html to pdf using dompdf in php


Introduction

This document is about converting an html page with images to a pdf file. You have to provide the content of the html file in this code and the code will convert the html content to a pdf content and either you can download it or you can save or you can do both. Images and css will also be included in the output pdf as it were in the source html page. You can see the example here of how a pdf will look like after converting this html sample to pdf. This html to pdf conversion is done using the code from dompdf. This is very simple and straight forward. Yet you can follow certain instructions for additional functionality which is well explained in the main site dompdf.
Besides, i have just used this code and i did not yet mastered. My purpose was to convert an html page including image and css to a pdf file and this code really worked well. My purpose was to generate a bill of orders with the company logo and the order listing with different colors. dompdf supports certain fonts which you can find in the download section or in the actual file which you can download directly from dompdf.

Code and Explanation


In the current folder(that is the folder where this html file is) i have two subfolders (cppdf and html) and a file named demo.php. cppdf is the folder which has all the files needed to convert html to pdf and the html folder is having sample html file with css and images. so the following code includes all the needed files like i had said above.
Here i give few lines of code which is present in the demo.php. When you download the complete sources you can find the file in the zip file.


Hope the above code is self explanatory. We include the main config file and include few globals.
Give a name to the output file and set $save_file to true which will mean that the file will be saved in the current working directory.
And then we read the total content of the html file which may have link attributes to include styles and image tags.
And here is the main picture.


$dompdf = new DOMPDF();
$base_path = $_SERVER['DOCUMENT_ROOT'].'/html/';
$dompdf->load_html($buff);
if ( isset($base_path) ) {
    $dompdf->set_base_path($base_path);
}
$dompdf->render();
We create an object dompdf.
And we set the base path.
What is this base path?
The base path parameter is used to resolve relative urls. So, if you have an image tag like so:
<img src="images/pretty.png"/>
and you set the base_path like so:
$dompdf->set_base_path("/var/www/");
dompdf will look for the image at /var/www/images/pretty.png on the webserver.
If you don't set the base_path parameter, and you run dompdf from the /var/www/dompdf-0.5 directory, dompdf will look for the image source at /var/www/dompdf-0.5/images/pretty.png, because the default base_path is the directory of the script being executed.
Hope this makes sense
Reference for this paragraph is taken from http://www.dashinteractive.net/dompdf/index.php?v=1514257


file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));
if (!headers_sent() )
$dompdf->stream($outfile);
The first line will save the converted pdf in the current directory and the next like will try to download the file if you have uncommented it like now. Else the file will be only saved in the disk. To make the download dialog appear you need the use the $dompdf->stream function.
Make sure your root folder have enough permission if you want to test the demo which will save a pdf file in that location.

Friday 26 August 2011

Mean, Median, Probability and Other Miscellaneous Math functions

These functions allow you to get the mean, the median, tell if a number is even or odd, strip an array of all strings and characters, and provide probability. The mean function works well if your trying to implement a rate system. just filter the input values of the array and it will work perfectly.

To get the mean just input an array. All non numbers will be stripped from the array and it will be re-ordered. The result is not rounded.

To get the median input an array. Like mean non numbers will be stripped. It will return the median as either a decimal or whole number depending on the length of the numbers range.

The even and odd functions take a number and return 0 or 1 if its not or is even or odd.

The non-numeric stripper function strips all non-numeric characters from and array and re-orders them so there are no blank spaces.

The probability function takes in either an array or single number and returns a ration of the probability of that number occurring.


The Code



Code: PHP
// returns the mean of a number list function mean($vals,$debug = 0) {             if(!is_array($vals))     {         return 0;     }     else     {             $val = arrayStripChar($vals);         $num = count($val);         $i = 0;         $m = 0;         for($i = 0; $i < $num; $i++)         {             $m += $val[$i];         }                 $mean = $m / $num;                 return $mean;     } }
Code: PHP
// returns the median of a number list function median($vals) {     if(!is_array($vals))     {         return 0;     }     else     {         $val = arrayStripChar($vals);         $num = count($val);         rsort($val);         $midNum = ($num + 1) / 2;         if(is_float($midNum))         {             $M = round($midNum);                         if(isEvenNum($num - $midNum))             {                 $mn = $M - 1;                 $mm = $mn - 1;                 $median = ($val[$mn] + $val[$mm]) / 2;                 return $median;             }             else             {                 $median = $val[$M -1];                 return $median;             }         }         else         {             if(isEvenNum($num - $midNum))             {                 $mn = $midNum - 1;                 $mm = $mn - 1;                 $median = array('a' => $val[$mn],'b' => $val[$mm]);                 return $mm;             }             else             {                 $median = $val[$midNum -1];                 return $median;             }         }     } }
Code: PHP
// function strips all non numeric values from an array and returns a new array that is clipped of all strings and character indexes function arrayStripChar($a) {     if(!is_array($a))     {         return 0;     }     else     {         $num = count($a);         $i = 0;         $j = 0;         $rtn = 0;         for ($i = 0; $i < $num; $i++)         {             if(!is_numeric($a[$i]))             {                 unset($a[$i]);             }         }         $n = array_values($a);                 return $n;     } }
Code: PHP
// evaluates if a number is odd or not function isOddNum($n) {     $odd = ($n % 2) ? 1 : 0;     return $odd; }
Code: PHP
// evaluates if a number even not function isEvenNum($n) {     $even = ($n % 2) ? 0 : 1;     return $even; }
Code: PHP
function probability($n,$nR) {     if(!is_array($n))     {         if(!is_array($nR))         {             $prob = $n . ":" . $nR;                         return $prob;         }         else         {             $val = arrayStripChar($nR);             $num = count($val);             $i = 0;             $p = 0;             for($i = 0; $i < $num; $i++)             {                 $p += $val[$i];             }             $prob = $n . ":" . $p;             return $prob;         }     }     else     {             if(!is_array($nR))         {             $nam = arrayStripChar($n);             $nem = count($nam);             $k = 0;             $p = array();             for($k = 0; $k < $nem; $k++)             {                 $p[$k] = $nam[$k] . ":" . $nR;             }             return $p;         }         else         {             $nam = arrayStripChar($n);             $nem = count($nam);             $k = 0;             $val = arrayStripChar($nR);             $num = count($val);             $i = 0;             $p = 0;             $c = array();             for($i = 0; $i < $num; $i++)             {                 $p += $val[$i];             }             for($k = 0; $k < $nem; $k++)             {                 $c[$k] = $nam[$k] . ":" . $p;             }             return $c;          }        } }