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遍历替换目录下文件指定内容的方法

本文实例讲述了php遍历替换目录下文件指定内容的方法。分享给大家供大家参考,具体如下: 在php中目录访问需要遍历了然后文件需要一个个打开进行访问操作了,下面我们来看一段php 替换目录...

asp.net Repeater控件的说明及详细介绍及使用方法

Repeater 控件不具备内置的呈现功能,这表示用户必须通过创建模板为 Repeater 控件提供布局。当该页运行时,Repeater 控件依次通过数据源中的记录为每个记录呈现一个项。...

php实现检查文章是否被百度收录

php实现检查文章是否被百度收录

网站都有个后台,后台发表新闻与产品,发完后如果你要去查看该页面有没有被百度收录,还要通过第三方工具或直接去百度搜。最近在做SEO,每天都要查看前一天发的文章有没有被收录,就这个工作就是一...

PHP版国家代码、缩写查询函数代码

复制代码 代码如下: <?php function transCountryCode($code) { $index=array('AA'=>'阿鲁巴', 'AD'=>...

PHP版微信第三方实现一键登录及获取用户信息的方法

本文实例讲述了PHP版微信第三方实现一键登录及获取用户信息的方法。分享给大家供大家参考,具体如下: 注意,要使用微信在第三方网页登录是需要“服务号”才可以哦,所以必须到官方申请。 一开始...