Saturday, July 20, 2013

Two way encryption in PHP Mcrypt

This class provides the functionality to encrypt and decrypt a text string. The class makes use of the PHP mcrypt extension which provides the ability to create two way encryption, or decoding of text messages.


<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

error_reporting(E_ALL);

class runCrypt 
{
    /**
    *
    * This is called when we wish to set a variable
    *
    * @access    public
    * @param    string    $name
    * @param    string    $value
    *
    */
    public function __set( $name, $value )
    {
        switch( $name)
        {
            case 'key':
            case 'ivs':
            case 'iv':
            $this->$name = $value;
            break;

            default:
            throw new Exception( "$name cannot be set" );
        }
    }

    /**
    *
    * Gettor - This is called when an non existant variable is called
    *
    * @access    public
    * @param    string    $name
    *
    */
    public function __get( $name )
    {
        switch( $name )
        {
            case 'key':
            return 'keee';

            case 'ivs':
            return mcrypt_get_iv_size( MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB );

            case 'iv':
            return mcrypt_create_iv( $this->ivs );

            default:
            throw new Exception( "$name cannot be called" );
        }
    }

    /**
    *
    * Encrypt a string
    *
    * @access    public
    * @param    string    $text
    * @return    string    The encrypted string
    *
    */
    public function encrypt( $text )
    {
        // add end of text delimiter
        $data = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv );
        return base64_encode( $data );
    }
 
    /**
    *
    * Decrypt a string
    *
    * @access    public
    * @param    string    $text
    * @return    string    The decrypted string
    *
    */
    public function decrypt( $text )
    {
        $text = base64_decode( $text );
        return mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv );
    }
} // end of class

?>



Example Usage


<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


// a new runCrypt instance
$crypt = new runCrypt;

// encrypt the string
$encoded = $crypt->encrypt( 'my message');
echo $encoded."\n";

// decrypt the string
echo $crypt->decrypt( $encoded ) . "\n";

?>

Round up to multiple in php


Here is a simple function that will round a number up to a given multiple. may this help you

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

function roundUpToMultiple( $number, $multiple)
{
    return ceil( $number/$multiple ) * $multiple;
} 


?>


Example Usage
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

echo roundUpToMultiple( 12, 7 );

?>

Thursday, June 27, 2013

PHP date validation

Date Validation yyyy-mm-dd
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$date = '2013-06-27';

function isValidDate($date)
{
 if(preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $date, $matches))
 {
  if(checkdate($matches[2], $matches[3], $matches[1]))
  {
   return true;
  }
 }
}

if(isValidDate($date)){
 echo 'valid date';
} else
{
 echo 'invalid date';
}

?>










Date Validation dd-mm-yyyy
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$date = '27-06-2013';

function isValidDate($date)
{
 if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $matches))
 {

  if(checkdate($matches[2], $matches[1], $matches[3]))
  {
   return true;
  }
 }
}

if(isValidDate($date))
{
 echo 'valid date';
} else
{
 echo 'invalid date';
}


?>








Date Validation mm-dd-yyyy

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$date = '06-27-2013';

function isValidDate($date)
{
 if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $matches))
{
  if(checkdate($matches[1], $matches[2], $matches[3]))
  {
   return true;
  }
 }
}

if(isValidDate($date)){
 echo 'valid date';
} else
{
 echo 'invalid date';
}

?>









Date Validation dd-mmm-yyyy
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$date = '27-Jun-2013';

function isValidDate($date)
{
 if(preg_match("/^(\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{4})$/", $date, $matches)) 
 {
  $month = array('Jan'=>'01'
         ,'Feb'=>'02'
         ,'Mar'=>'03'
         ,'Apr'=>'04'
         ,'May'=>'05'
         ,'Jun'=>'06'
         ,'Jul'=>'07'
         ,'Aug'=>'08'
         ,'Sep'=>'09'
         ,'Oct'=>'10'
         ,'Nov'=>'11'
         ,'Dec'=>'12'
        );

  if(checkdate($month[$matches[2]],$matches[1],$matches[3]))
  {
   return true;
  }
 }
}

if(isValidDate($date))
{
 echo 'valid date';
} else
{
 echo 'invalid date';
}

?>

PHP function to calculate percentage

This post shows you how to calculate a percentage using a division and multiplying its result by 100. This is the simpliest way to calculate a percentage in PHP
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


function percentage($x, $y, $precision) 
{

        // $precision define digit after the decimal point

 $result = round( ($x / $y) * 100, $precision );
 
 return $result;
}


echo percentage(2, 3, 2); // This will print 66.67

echo percentage(2, 3, 0); // This will print 67

?>

Friday, June 21, 2013

Replace characters with icons emoticons php

Learn to use PHP to change character sequences into emoticons or icons by using the str_replace function. You will want to alter the string data as it comes out of the MySQL database for display, not when the data is being inserted into the database.

# USAGE EXAMPLE 

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


$db_string = "Show me some <3 right now. It is raining today [umbr], that gives me peace [peace].";

echo $db_string;
echo "
"; $chars = array("<3", "[peace]", "[umbr]"); $icons = array("❤", "☮", "☂"); $new_str = str_replace($chars,$icons,$db_string); echo $new_str; ?>

Get Favicon From other Website Using PHP


Learn to get and render favicon.ico files dynamically from other servers in your PHP scripts. You can look for files and also process the DOM structure of external pages on the web. I recommend saving the favicon image file to your server converting it to a JPG or PNG file if it will be a frequently viewed image.

# USAGE EXAMPLE 

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/



function getfavicon($url){
    $favicon = '';
    $html = file_get_contents($url);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $links = $dom->getElementsByTagName('link');
    for ($i = 0; $i < $links->length; $i++){
        $link = $links->item($i);
        if($link->getAttribute('rel') == 'icon'){
            $favicon = $link->getAttribute('href');
 }
    }
    return $favicon;
}


$website = "http://4evertutorials.blogspot.com/"; # replace with your url
$favicon = getfavicon($website);
echo $favicon;

?>

Render XML file for Flash Photo Gallery with php

Learn how to render dynamic XML files for Flash(or any other) Photo Gallery applications using PHP loops to show the most current data at all times.


This script would be named something like galleryXML.php

This file reads a directory on its own to render the XML file, all you have to do it point it at a folder of images no matter how many are in it, and it will produce nice clean XML nodes for all of the images.




<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


# set the content type to xml
header("Content-Type: text/xml");

# Initialize the xmlOutput variable
$xmlBody = '';

# Specify Directory where images are 
$dir = "images/gallery/"; 

# Start XMLBody output
$xmlBody .= ""; 

# open specified directory using opendir() the function
$dirHandle = opendir($dir); 

# Create incremental counter variable
$i = 0;
while ($file = readdir($dirHandle)) 
{ 
      
      # if file is not a folder and if file name contains the string .jpg  

      if(!is_dir($file) && strpos($file, '.jpg'))
      {
        # increment $i by one each pass in the loop
         $i++; 
         $xmlBody .= '

    ' . $i . '
    ' . $dir . '' . $file . '
';
      } # close the if statement

} # End while loop


# close the open directory
closedir($dirHandle); 

$xmlBody .= "";

# output the gallery data as XML file for Flash Photo Gallery
echo $xmlBody; 


?>

 

© 2014 4everTutorials. All rights resevered.

Back To Top