Home php教程 php手册 亲密接触:在PHP下实现持久化

亲密接触:在PHP下实现持久化

Jun 21, 2016 am 09:06 AM
mysql php

 “持久化”这个概念是笔者在Java中首次接触到的,通过这个特性,可以将应用程序对象转化成一系列字节流(这被称作对象序列化),以适应网络传输或保存。最奇妙的是,被序列化的对象还可以被重新装配,还原成以前的样子。这意味着,该机制能自动补偿操作系统间的差异。换句话说,一个在Windows系统的机器上被序列化的对象可以通过网络传输到一台Linux系统的机器上准确无误的重新装配。“持久化”可以使应用程序对象不受应用程序运行时间的限制——可以将一个对象序列化,然后保存到磁盘上,在再次需要时进行装配,能圆满实现一种“持久”效果。
  
  令人兴奋的是,PHP也支持这一特性,而且从PHP3就开始支持了,它是通过Serialize()和Unserialize()这两个函数来实现的。其实,像ASP这样的开发环境也隐式支持这一特性——在Session或Application对象中保存应用程序对象就是一种持久化的表现,但遗憾的是,ASP并没有显式提供这一接口。
  
  在PHP中,几乎任何类型(这包括Integer、Boolean、Float、Array和Object)的变量都可以被序列化。之所以说“几乎”,是因为唯独Resource类型不支持序列化,这完全是因为PHP中的Resource类型其实是指针的缘故。至于String类型,由于它本身就是字节流,所以根本没有序列化的必要。
  
  下面将介绍Serialize()和Unserialize()两个函数的用法:
  
  string serialize (mixed value):返回value被序列化后的字节流;
  mixed unserialize (string str):返回将str进行装配后的对象。
  
  下面是这两个函数的应用实例:
  
    //class.inc.php文件,用于保存类的信息
  
  //用于测试的用户信息类
  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文件,用于注册新用户
  
  //导入类文件
  require_once("class.inc.php");
  
  //新建对象
  $user = new Userinfo($_POST['username'], $_POST['password'], date("Y-n-j H:i:s"));
  //序列化对象
  $user = Serialize($user);
  
  //将对象写入本地数据库
  $con = mysql_connect();
  mysql_select_db("test");
  mysql_query($con, "INSERT INTO testTable (id, userinfo) VALUES ('1', '$user')");
  mysql_close($con);
  ?>
  
    //userinfo.php文件,用于显示用户信息
  
  require_once("class.inc.php");
  
  //从数据库中取出对象
  $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']);
  //输出用户信息
  $user -> output();
  mysql_free($result);
  mysql_close($con);
  ?>
  
  在对象序列化中,最重要的是在“装配”的页面中一定要包含该对象的类的定义信息,否则会出现错误。当然,上里只是用于测试,在实际的应用中,为了防止序列化后的对象的内容被更改,一般还要对字节流进行“数字签名”,在装配时,再对“签名”进行验证,以防止对象信息被非法篡改。



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Useful Resources CakePHP Useful Resources Sep 10, 2024 pm 05:27 PM

The following resources contain additional information on CakePHP. Please use them to get more in-depth knowledge on this.

See all articles