Store An Array
Posted by Pete | Posted in PHP Functions, PHP Tutorials | Posted on 11-10-2009-05-2008
1
In this tutorial we will learn how to encrypt an array with base64 and then serialize it so we are able to store it in a database without loosing the integrity or values.
So lets start by creating 2 functions the first of which will encrypt and serialize the array.
function store_encrypt($array) { // Start function with function name store_encrypt. if (!is_array($array)) { // Check if $array is an array or not if it isnt return false. return false; // return false. } else { foreach ($array as $key => $value) { // Cycle through the array. $key = base64_encode($key); // Encode the key with base 64. $value = base64_encode($value); // Encode the value with base 64. $array[$key] = $value; // Put the key and value back into the array } return serialize($array); // Use the PHP serialize function to save the array, type and values and return the function. } }
So lets see how we can use this function.
<? $array = array("first_name" => "Peter", "last_name" => "Kelly", "website" => "http://www.pk-tuts.co.uk"); $info = store_encrypt($array); echo $info; ?>
So now you are able to store the array in the database. But how do we get it back, Well this is where the second function come into play.
function unstore_uncrypt($value) { // Start function with function name unstore_uncrypt. if (!is_array($array)) { // Check if $array is an array or not if it isnt return false. return false; // return false. } else { $array = unserialize($value); // Restore the value into an array. foreach ($array as $key => $value) { // Cycle through the array. $key = base64_decode($key); // Decode the key from base 64. $value = base64_decode($value); // Decode the value from base 64. $array[$key] = $value; // Put they key and value back into the array. } return $array; // Return the array. } }
So lets use this function.
<? $info = "STRING FROM PREVIOUS FUNCTION" $array = unstore_uncrypt($info); echo "<pre>"; print_r($array); echo "
“;
?>
Put the string that was returned from the previous function in $info then run the example. You will now have the array as previous.
So now lets put it all together.
<?php function store_encrypt($array) { // Start function with function name store_encrypt. if (!is_array($array)) { // Check if $array is an array or not if it isnt return false. return false; // return false. } else { foreach ($array as $key => $value) { // Cycle through the array. $key = base64_encode($key); // Encode the key with base 64. $value = base64_encode($value); // Encode the value with base 64. $array[$key] = $value; // Put the key and value back into the array } return serialize($array); // Use the PHP serialize function to save the array, type and values and return the function. } } function unstore_uncrypt($value) { // Start function with function name unstore_uncrypt. if (!is_array($array)) { // Check if $array is an array or not if it isnt return false. return false; // return false. } else { $array = unserialize($value); // Restore the value into an array. foreach ($array as $key => $value) { // Cycle through the array. $key = base64_decode($key); // Decode the key from base 64. $value = base64_decode($value); // Decode the value from base 64. $array[$key] = $value; // Put they key and value back into the array. } return $array; // Return the array. } } $conn = mysql_connect("DATABASE_LOCATION","DATABASE_USER","DATABASE_PASSWORD"); // Connect to mysql server if (!$conn) die ("Could not connect MySQL"); // If unable to connect die mysql_select_db("DATABASE_NAME",$conn) or die ("Could not open database"); // Now your connected select the correct database $array = array("first_name" => "Peter", "last_name" => "Kelly", "website" => "http://www.pk-tuts.co.uk"); // Create the array. $info = store_encrypt($array); // Run the function $add_array = mysql_query("INSERT into `table` (`id`, `array`) values ('', '".$info."');"); // Insert into the table $result = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT 1"); // Select one row from table and order by id descending. $r = mysql_fetch_array($result); // Select the data. $uncrypt = unstore_uncrypt($r['array']); // Get the array stored in the database and run the function to uncrypt it echo "<pre>"; // Do some formatting so the array looks pretty. print_r($uncrypt); // Echo the array. echo "
";
?>
The base64 is just something extra and can be removed as encrypting with base64 will take up more space than without encryption. If you have any questions or would like to leave feedback please leave a comment.



Pete, I would point out a couple of things that you may want to think about. The first is a matter of semantics most likely, but it has to do with the use of the word “encrypt”. Encryption typically entails encoding data with a ‘key’ of some sort to protect that data. It can then not be easily decrypted without knowing the key….thereby protecting the contents.
Encoding on the other hand is a method of changing or transforming the format of the data so that it can be stored in an easier or different fashion. The base64 functions will encode/decode, but not encrypt as there is no inherent security.
The other thing is that the way your functions are setup, they would only encode the first level of multi-dimensional arrays. I would probably approach things differently by using the serialize() function FIRST, which will handle an array of unlimited depth, then perhaps use base64 encoding to make the data more universally transportable/storable/etc.
I hope this helps!