Login   Register  
PHP Classes
elePHPant
Icontem

File: example/mysql.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nathan Lucas  >  Cipher  >  example/mysql.php  >  Download  
File: example/mysql.php
Role: Example script
Content type: text/plain
Description: MySQL example.
Class: Cipher
Encrypt and decrypt data with a single key
Author: By
Last change: Another use for Cipher. This encrypts data that will be stored in a MySQL db.
Date: 6 years ago
Size: 1,597 bytes
 

Contents

Class file image Download
<?php
require_once("./Cipher.php");
$cipher = new Cipher(MCRYPT_BLOWFISHMCRYPT_MODE_ECB);

$conn mysql_connect("host""user""pass");
mysql_select_db("dbname"$conn);

$key "a secret key.";

if (
$_POST) {
    
$name $cipher->encrypt($_POST['name'], $key);
    
$phone $cipher->encrypt($_POST['phone']);
    
$iv $cipher->getIV();
    
    
mysql_query("INSERT INTO `phones` (`id`, `name`, `phone`, `iv`) VALUES (NULL, '".$name."', '".$phone."', '".$iv."')"$conn);
    
    
/*
    CREATE TABLE `phones` (
        `id` int(10) unsigned NOT NULL auto_increment,
        `name` varchar(64) NOT NULL,
        `phone` varchar(64) NOT NULL,
        `iv` varchar(64) NOT NULL,
        PRIMARY KEY(`id`)
    );
    */
}
?>
<html>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            name: <input type="text" name="name" value="" />
            <br />
            phone: <input type="text" name="phone" value="" />
            <br />
            <input type="submit" value="Add Number" />
        </form>
        <hr />
        <pre>
            <?php
            
echo "\r\n";
            
$result mysql_query("SELECT * FROM `phones` ORDER BY `name` ASC");
            while (
$row mysql_fetch_object($result)) {
                
$name $cipher->decrypt($row->name$key$row->iv);
                
$phone $cipher->decrypt($row->phone);
                echo 
$name.":\n";
                echo 
$phone."\n\n";
            }
            echo 
"\r\n";
            
?>
        </pre>
    </body>
</html>