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

yipeiwu_com5年前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给图片加文字水印

注释非常的详细了,这里就不多废话了 <?php /*给图片加文字水印的方法*/ $dst_path = '/zb_users/upload/202003/qndictx...

php函数实现判断是否移动端访问

忘记在哪里获取的函数了,刚才不了心在一个包里面找到,临时保存起来 复制代码 代码如下: /**  * 是否移动端访问访问  *  * @return boo...

php字符串截取的简单方法

复制代码 代码如下:strpos(string,find,start)实例:复制代码 代码如下:<?php  echo strpos("Hello world!","wo...

php中call_user_func函数使用注意事项

本文实例讲述了php中call_user_func函数使用注意事项。分享给大家供大家参考。具体分析如下: call_user_func函数的注意事项:parse error: synta...

THINKPHP内容分页代码分享

在使用Thinkphp开发的内容管理系统里面,很多东西都要自己开发,内容分页当然也是要自己开发的,下面是我根据查资料自己整理的方法: 1、首先是在后台编辑内容的时候需要插入分页符,不同的...