php中将一个对象保存到Session中的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php中将一个对象保存到Session中的方法。分享给大家供大家参考。具体如下:

要保存对象到session其实很简单,我们可以使用session_register()函数,下面是使用范例

person_class.inc.php如下:

<?php
//
//File: person_class.inc.php
//Contains the class definition necessary to let an object be a session
//variable.
//
class Person
{
  var $name;
  var $email;
  //
  // A simple function to illustrate the point
  //
  function clean_name ()
  {
    $name = preg_replace("/h(.)+/i", "\\1", $this->name);
    return substr($name, 0, 15);
  }
}
?>

main.php文件如下:

<?php
//
//File: main.php
//Here is where we save and retrieve the object
//
include_once 'person_class.inc.php';
session_register('someperson');
if (!$someperson) {
  $someperson = new Foo;
  $someperson->name = "Item Raja";
  $someperson->email = "itemraja@php.net";
  $someperson->clean_name();
}
?>
<a href="somePage.php">Click Here</a>

somPage.php文件如下:

<?php
//
//File: somePage.php
//Print out the name without initializing the
//class and setting the variables
//
include_once 'person_class.inc.php';
session_register('foobar');
print $foobar->name;
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP开发中AJAX技术的简单应用

AJAX无疑是2005年炒的最热的Web开发技术之一,当然,这个功劳离不开Google。我只是一个普通开发者,使用AJAX的地方不是特别多,我就简单的把我使用的心得说一下。(本文假设用户...

php发送http请求的常用方法分析

本文实例讲述了php发送http请求的常用方法。分享给大家供大家参考,具体如下: http请求有get,post。 php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。...

PHP中ltrim与rtrim去除左右空格及特殊字符实例

本文实例分析了PHP中ltrim与rtrim去除左右空格及特殊字符的用法。分享给大家供大家参考,具体如下: 一、PHP中ltrim定义如下: ltrim(string,charlist)...

PHP下对数组进行排序的函数

经常,开发人员发现在PHP中使用这种数据结构对值或者数组元素进行排序非常有用。PHP提供了一些适合多种数组的排序函数,这些函数允许你在数组内部对元素进行排列,也允许用很多不同的方法对它们...

php抽象类使用要点与注意事项分析

本文实例分析了php抽象类使用要点与注意事项。分享给大家供大家参考。具体分析如下: php抽象类使用要点与注意事项如下: 1、用 abstract 来修饰一个类,那么这个类就是抽象类;抽...