Introduction
Serializing is a technique that allows you to turn variables, arrays, functions or objects from PHP into a form that can be used to store them, and when needed, convert them back into PHP.
Serializing is useful for flatfile applications that store things in arrays, and are good for CMS features that can let you edit how it works to an advanced degree.
There are two primary functions used in serializing, and they are:
| serialize( $your_object_here ); |
This can be a variable, array, function or object. It turns it into the storable form.
| unserialize( $your_string_here ); |
This must be a serialized PHP string, otherwise it will fail. It turns it back into a data type that is useful to PHP, exactly as it was when you serialize()'d it.
Since it's a very simple function, but sometimes people don't know what to do with it, today we're going to write a very simple example that will do this:
- Take data from PHP and serialize it, then store it in a text file.
- Open up the serialized file, and then allow you to edit its values.
- Save it back into the text file.
It's pretty straightforward, so let's get started...
We're going to set up an array of people and their favourite foods...
| <?PHP
$array = array( "Steve" => "Bacon", "John" => "Radishes", "Peter" => "Sandwiches", "Will" => "Cupcakes", "David" => "Burgers" ); ?> |
Once we've got this set up, we can serialize it:
| <?php
print serialize($array); ?> |
And, it should look like this:
| a:5:{s:5:"Steve";s:5:"Bacon";s:4:"John";s:8:"Radishes";s:5:"Peter";s:10:"Sandwiches";s:4:"Will";s:8:"Cupcakes";s:5:"David";s:7:"Burgers";} |
As you can see, it looks somewhat like its PHP equivalent, but full of numbers. I'm not going to go into what the numbers mean, because I don't know, and I can't be bothered, so without further ado, we're now going to save this information into a .txt file. Make a new file called data.txt and CHMOD it to 777.
| <?php
$res = fopen("data.txt","r+"); // Opens a resource $string = serialize($array); // Sets a variable to identify the serialized array $write = fwrite($res,$string); // Writes it to the file if($write) { echo "Data written."; } else { echo "There was a problem."; } @fclose($res); ?> |
Now that you've done that, we're now going to construct a page that will:
- Unserialize the data
- Put it into an HTML form
- Allow editing
- Save it back in the text file
| <?php
if($_POST[submit]) { $newarray = array(); // New, blank array. foreach($_POST as $key => $val) { if($key!="submit") { // We want to exclude the submit button (it's part of $_POST) $newarray[$key] = $val; } } $newarray = serialize($newarray); // Serializes our new array. $res = fopen("data.txt","r+"); $write = fwrite($res,$newarray); if($write) { // If it works, which it will... echo "It worked!"; } else { // In the unlikely event of the plane crashing... echo "It didn't work.."; } } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" name="data" id="data"> <?php $data = unserialize(file_get_contents("data.txt")); // Unserializes the file's contents, therefore turning itself into an array. foreach($data as $key => $val) { // Iterates through the array echo $key. ' <input type="text" id="'.$key.'" name="'.$key.'" value="'.stripslashes($val).'" />'; // Writes an input box for each key. } ?> <input type="submit" id="submit" name="submit" value="Update" /> </form> |
And that's it! You can see a working example (and an HTML copy of this tutorial) at www.avengex.com.




