Wednesday, 1 June 2016

How to ENCRYPT and DECRYPT the Sensitive Data in PHP

      In PHP, generally we use base64_encode()  and base64_decode() methods to encrypt the data but it is not secure, anyone can extract data behind this encryption, But if you apply some extra security layer this will make user’s data more secure, means add some solt to data and then encrypt the data.
<?php
/*
* Author: prashanthi
* Website: http://prashanthikontemukkula.blogspot.in/
* Date: 01-06-2016
* App Name: encrypt, decrypt data
* Description: A simple OOPS based call to encrypt, decrypt your data
*/
class Secure {
   // Set your unique has keys
   private static $secretKey = 'phpdeveloper'; 
   private static $secretIv = 'ils';
   // Encryption method
   private static $encryptMethod = "AES-256-CBC"; 

   // pass string/number which you want to encrypt
   public static function encrypt($data) {
     $key = hash('sha256', self::$secretKey);
     $iv = substr(hash('sha256', self::$secretIv), 0, 16);
     $result = openssl_encrypt($data, self::$encryptMethod, $key, 0, $iv);
      return $result= base64_encode($result);
   }

   // pass encrypted data to decrypt
   public static function decrypt($data) {
     $key = hash('sha256', self::$secretKey);
     $iv = substr(hash('sha256', self::$secretIv), 0, 16);
     $result = openssl_decrypt(base64_decode($data), self::$encryptMethod, $key, 0, $iv);
      return $result;
   }
}

?>
First include above class one page where you want to use it after that calling it’s function.


include_once('secure.php');
//encrypt the data
echo $has = secure::encrypt("prashanthi");
//decrypt the data 
echo secure::decrypt('SXpTekhPN2NsNlJFWlRCWnVrNGxOZz09');




Saturday, 20 February 2016

Senior NTR Dialogue


         ఆచార్య దేవా!  యేమ౦టివి యేమ౦టివి జాతి నెపమున సూతసుతునకి౦దు నిలువ అర్హత లేద౦దువా.... ఎ౦త మాట ఎ౦త మాట??? ఇది ఛాత్ర పరీక్షయే కాని క్షత్రియ పరీక్ష కాదె? కాదు కాకూడదు.. ఇది కుల పరీక్షయే అ౦దువా....నీ త౦డ్రి భరధ్వాజుని జన్మమెట్టిది? అతి జుగుప్సాకరమైన నీ స౦భవమెట్టిది? మట్టి కు౦డలో పుట్టితివి కదా నీది యే కులము?.. ఇ౦త యేల, అస్మత్ పితామహుడు కురుకుల వృధ్ధుడైన ఈ శా౦తనవుడు శివ సముద్రల భార్యయైన‌ గ౦గా గర్భమున జనియి౦చలేదా... ఈయనది యే కులము? నాతో చెప్పిస్తివేమయ్యా మా వ౦శమునకు మూల పురుషుడైన వశిష్టుడు దేవ వేశ్యయగు ఊర్వశి పుత్రుడు కాడా...? అతడు ప౦చమజాతి కన్యయైన అరు౦ధతియ౦దు శక్తినీ ఆ శక్తి ఛ౦డలా౦గనయ౦దు పరాశరునీ ఆ పరాశరుడు పల్లె పడచు అయిన మత్స్యగ౦ధియ౦దు మా తాత వ్యాసునీ  ఆ వ్యాసుడు విధవరా౦డ్రైన మా పితామహి అ౦బికతో మా త౦డ్రినీ పిన పితామహి అయిన అ౦బాలికతో మా పిన త౦డ్రి పా౦డురాజునూ మా యి౦టి దాసితో ధర్మనిర్మాణజనుడని మీచే కీర్తి౦పబడుతున్న ఈ విదుర దేవుని కన లేదా...? స౦ధర్భావసరములను బట్టి క్షేత్రబీజ ప్రాధాన్యములతో స౦కరమైన మా కురు వ౦శము ఏనాడో కుల హీనమైనది కాగా నేడు కులము కులము అని వ్యర్ధ వాదములె౦దులకు?

Thursday, 17 December 2015

How to return an PHP array from an AJAX call ?


              Is it possible to return PHP array to front end in Ajax call? Yes, it is possible through JSON.

JSON: JSON stands for Javascript Object Notation, It is a light weight data interchange format
the JSON Format like

{"employees":[
    {"firstName":"Sri", "lastName":"Ram"},
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Krish", "lastName":"Rao"}
]}


In PHP we have pre-defined methods like json_encode() and json_decode() methods. The json_encode method takes the array as input parameter it returns the JSON format data. json_decode method takes the json format and it returns array.
Here we need json_encode method to achieve our goal
example:
arrayreturn_file.php
               <?php 
                    $arr=array(a=>1,b=>2,c=>3);
                    //convert the array to json format
                     print_r(json_encode($arr));
                ?>
The above code return {"a":1,"b":2,"c":3}
Here a,b, c are keys and 1,2,3 are values

ajaxcall.php:
        <script>
             var arr=new array();
             $.ajax({
               url: arrayreturn_file.php,
               type:"POST",
               success:function(data){
                     //parse the json data
                     var arr = jQuery.parseJSON(data);
// through keys we can access the values
alert(arr.a); // return 1
alert(arr.b); // return 2
alert(arr.c); // return 3

                }
             });
        </script>
Note: Instead of JSON we can use XML also,but XML is the old technology than JSON



Friday, 3 July 2015

How to CREATE and EXECUTE Store Procedure in PHP.

Definition:  

        What is Store Procedure: Store Procedure is the database object.
Store procedure is pre-compiled code. By using store procedure we are able to call the functions. Store procedure is the

HOW TO CREATE STORE PROCEDURE:

Write and execute below code in mysql.

DELIMITER  $$
CREATE PROCEDURE procedurename()
BEGIN

          SELECT columnname1, columnname2 FROM  Database.table ;

END $$

DELIMITER  ;


HOW TO CALL THE STORE PROCEDURE TO PHP using PDO:

include 'pdodatabase.php';
$sql="CALL procedurename";
try{
      $stmt=$databaseobject->prepare($sql);
      $stmt->execute();
      $result=$stmt->fetchAll();
}
catch(exception $e){
     exit;
}
foreach($result as $rs){
    echo $r["columnname1"];
}

Thursday, 21 May 2015

How to convert excel sheet into PDF File in PHP STEP by STEP


           In PHP we have many number of open source plugins available to convert EXCEL to PDF. Here im using PHPEXCEL and HTML2PDF plugins to complete my task. To convert EXCEL to PDF, First of all, we need to read the excel data and then that values write on to PDF.
 
           Firstly, Arrange the data in required format in excel, then read the the excel data by using PHPEXCEL plugin. click here to download PHPEXCEL plugin.
          Next, click here to download HTML2PDF plugin.

       For example, the excel data is like below.




Find the Step by process for read the excel data:

Step 1: Add the plugins to project folder, then find the IOFactory.php file in plugin and include the path to required file and alse find the html2pdf.class.php file from HTML2PDF Plugin, like below.

 <?php 
     include  "PHPExcelOrg/Classes/PHPExcel/IOFactory.php";
     require_once "html2pdf/html2pdf.class.php";
?>
Step 2: Save the Uploaded excel file into tempory folder and create instance for the excel file to read the data
 <?php 
        $target_dir = "uploads/";//folder to save the uploaded excel
        $target_file = $target_dir.basename($_FILES["exampleInputFile"]["name"]);
        if(isset($_POST["process"]))
        {
            //check the size, increase the limit accordingly
           if ($_FILES["exampleInputFile"]["size"] > 2000000) {
           echo "Sorry, your file is too large.";
           $uploadOk = 0;
       }
      else{
         if(move_uploaded_file($_FILES["exampleInputFile"]["tmp_name"],$target_file))
         {          
            $filepath=$target_file;
            $fpath = basename($_FILES["exampleInputFile"]["name"]);
            $uploaded_dir = $_SERVER['DOCUMENT_ROOT'].'/payslip/payslip';
            $inputFileName = $uploaded_dir.'/'.$target_file;
             //  Read your Excel workbook
         try {
             $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
             $objReader = PHPExcel_IOFactory::createReader($inputFileType);
             $objPHPExcel = $objReader->load($inputFileName);
         } catch (Exception $e) {
            die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
               . '": ' . $e->getMessage());
          }
      }
  }
 
?>
Step 3: Find the highest columns and highest rows.
    
   <?php

       $sheet = $objPHPExcel->getSheet(0);// first sheet
      $highestRow = $sheet->getHighestRow();  //print 2
      $highestColumn = $sheet->getHighestColumn();  //print C
?>
Step 4: read the Excel cells data.
<?php
          //  Loop through each row of the worksheet in turn
         //  leave the header of the excel so start from second row then $row=1
        $cellValue = array();
        for ($row =1; $row <= $highestRow; $row++)
        {
              for($column = 0; $column < count($columnIndex); $column++)
{
$cellValue[$i][$column] = $objPHPExcel->getActiveSheet()
                       ->getCellByColumnAndRow($columnIndex[$column],$row)->getValue();
if($cellValue[$i][$column]==''||$cellValue[$i][$column]==NULL)
                        {
$cellValue[$i][$column]=0;
}
      }
        }
?>
Step 5 : Include the HTML2PDF plugin then Create the html data using excel values.

//if we have multiple rows then use loop

for($i =1; $row <= $highestRow; $i++){
 $content='<div><table><tr><td>FirstName:</td><td>'.$cellValue[$i][1].'</td></tr>
                                        <tr><td>LastName:</td><td>'.$cellValue[$i][2].'</td></tr>
                                       <tr><td>Address:</td><td>'.$cellValue[$i][3].'</td></tr>
                             </table>
                   </div>';
        try
        {
            $html2pdf = new HTML2PDF('P','A4','en',true, 'UTF-8');
            $html2pdf->WriteHTML($content);
            $html2pdf->Output('pdf/filename.pdf', 'F');//give the path

        }
        catch(HTML2PDF_exception $e)
        {
            echo $e;
            exit;
        }
}

OUTPUT of PDF:

Sunday, 27 July 2014

What is Trigger in SQL Server

Trigger:

             A Trigger is a database object similar to a stored procedure that executes in response to certain actions that occur in your database environment.

Triggers are 2 types:

     1. After trigger(For trigger)
     2.Instead of Trigger

After trigger(For trigger):

           After trigger are stored procedures that occurs after a data manipulation statement has occurred in the database, such as a delete statement.

Instead of Trigger:

         Instead of Trigger are objects that will execute instead of data manipulation statements in database engine. 
For example, attaching an INSTEAD-OF INSERT trigger will tell the the database to engine to execute that trigger instead of executing that  statement that would insert values into that table.

Syntax: 

CREATE TRIGGER trigger_name
ON {table| view}
[WITH ENCRYPTION]
{{AFTER|FOR | INSTEAD OF | }{[INSERT][,][DELETE] [UPDATE]}
AS
         sql_statement[....n]
}
}


Sunday, 20 July 2014

Difference between WCF and WPF


WCF - Windows COMMUNICATION Foundation
WPF - Windows PRESENTATION Foundation.
WCF deals with communication (in simple terms - sending and receiving data as well as
formatting and serialization involved), WPF deals with presentation (UI)


The quick answer is:  
      Windows Presentation Foundation (WPF) is basically a way of displaying
user interface. 
      Windows Communication Foundation (WCF) is a framework for creating service oriented 
applications. 
       As for which one you should use, it depends on your requirement. Usually an application written 
in WPF, ASP.NET..etc called the WCF service to do some processing at the server-side and the
 service returns the result to the application that called it.