Home > Backend Development > PHP Tutorial > Implement persistence under PHP_PHP tutorial

Implement persistence under PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:26:01
Original
1030 people have browsed it

Implementing persistence under PHP
The concept of "persistence" was the first time that the author came into contact with Java. Through this feature, application objects can be converted into a series of byte streams (this is called object serialization). to adapt to network transmission or saving. The most amazing thing is that serialized objects can also be reassembled and restored to their previous appearance. This means that the mechanism automatically compensates for differences between operating systems. In other words, an object that is serialized on a Windows machine can be transferred over the network to a Linux machine and reassembled without error. "Persistence" can make application objects not limited by the application's running time - an object can be serialized, then saved to disk, and assembled when needed again, which can achieve a "persistent" effect.
What’s exciting is that PHP also supports this feature, and it has been supported since PHP3. It is implemented through the two functions Serialize() and Unserialize(). In fact, development environments like ASP also implicitly support this feature - saving application objects in Session or Application objects is a manifestation of persistence, but unfortunately, ASP does not explicitly provide this interface.
In PHP, variables of almost any type (this includes Integer, Boolean, Float, Array, and Object) can be serialized. The reason why I say "almost" is because only the Resource type does not support serialization. This is entirely because the Resource type in PHP is actually a pointer. As for the String type, since it is a byte stream itself, there is no need for serialization at all.
The following will introduce the usage of the two functions Serialize() and Unserialize():
string serialize (mixed value): Returns the byte stream after the value is serialized;
mixed unserialize (string str): Returns the object assembled from str.
The following are application examples of these two functions:
//class.inc.php file, used to save class information
//User information class for testing
class Userinfo
{
var $username;
var $password;
var $datetime;


function Userinfo($username, $password, $datatime)
{
$this -> username = $username;
$this -> password = $password;
$this -> datetime = $datetime;
}
function output()
{
echo "User Information ->
";
echo "Username: ".$this -> username."
";
echo "Password: ".$this -> username."
";
echo "Datetime: ".$this -> username."
";
}
}
?>
//login.php file, used to register new users
//Import class files
require_once("class.inc.php");
//New object
$user = new Userinfo ($_POST[username], $_POST[password], date("Y-n-j H:i:s"));
//Serialization object
$user = Serialize($user);
/ /Write objects to the local database
$con = mysql_connect();
mysql_select_db("test");
mysql_query($con, "INSERT INTO testTable (id, userinfo) VALUES (1, $user )");
mysql_close($con);
?>
//userinfo.php file, used to display user information
require_once("class.inc.php ");
//Get the object from the database
$con = mysql_connect();
mysql_select_db("test");
$result = mysql_query($con, "SELECT * FROM testTable WHERE id=1");
$record = mysql_fetch_assoc($result);
$user = Unserialize($record[userinfo]);
//Output user information
$user -> output( );
mysql_free($result);
mysql_close($con);
?>
In object serialization, the most important thing is that the object must be included in the "assembly" page class definition information, otherwise an error will occur. Of course, the above is just for testing. In actual applications, in order to prevent the contents of the serialized object from being changed, it is generally necessary to "digitally sign" the byte stream, and then "digitally sign" it during assembly. Verification to prevent object information from being illegally tampered with.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531996.htmlTechArticle Implementing persistence under PHP The concept of "persistence" was the first time that the author came into contact with Java. Through this Feature that converts an application object into a sequence of bytes (this is called...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template