欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个记录用户访问页面的类,顺便记一下PHP4的重载方式

程序员文章站 2022-03-04 13:17:21
...
当用户浏览某页面时需要转向到注册页面,注册完成后希望用户能返回到之前想访问的页面,这个类或许有用,目前该类还在完善中.



<?php
/*
* @author badboy
* @2009-5-27
* @php4 类语法
* 类Tracker用于跟踪用户访问页面
* 页面信息存放于Cookie中,如果Cookie被禁用,此类将不起作用.
*/
class Tracker
{

var $max_pages = 10 ; //最多保留多少个页面信息
var $trackStack; //踪迹栈
var $cookieName; //cookie名称,默认为 $_SERVER['HTTP_HOST'].'_PAGE_TRACK'

/*
* 构造方法
*/
function Tracker()
{
$this->trackStack=array();
$this->cookieName = '_PAGE_TRACK';
$this->loadFromCookie();

//移除过多的页面
if(count($this->trackStack)>$this->max_pages)
{
for($i=0;$i<count($this->trackStack)-$this->max_pages;$i++)
{
array_shift($this->trackStack);
}

}

}


/*
* 将页面信息保存到记录栈中,此方法被重截.
* 无参数版本表示将当前页加到踪迹栈中
* 有参数版本表示将指定页加到踪迹栈中
*/
function addPage()
{
$num_args = func_num_args();
$arr_args = func_get_args();
switch($num_args)
{
case 0:
$this->addPageOverloading_1();
break;
case 1:
$this->addPageOverloading_2($arr_args[0]);
break;
}
}
function addPageOverloading_1()
{
$page = urlencode($_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
if($page!=$this->trackStack[count($this->trackStack)-1])
{
$this->trackStack[]=$page;
}
$this->mapToCookie();
}
function addPageOverloading_2($page)
{
$page = urlencode($page);
if($page!=$this->trackStack[count($this->trackStack)-1])
{
$this->trackStack[]=$page;
}
$this->mapToCookie();
}

/*
* 清除记录
*/
function clear()
{
$this->trackStack=array();
$this->mapToCookie();
}


function mapToCookie()
{
$cookies = implode('|||',$this->trackStack);
setcookie($this->cookieName,$cookies);
}

/*
* 从Cookie读取页面记录到$trackStack;
* 它们以|||分隔
*/
function loadFromCookie()
{
$cookies = $_COOKIE[$this->cookieName];
if(!$cookies)
return;
$this->trackStack = explode('|||',$cookies);
}


/*
* 返回栈中的页面
* 参数-1表示返回倒数一个页面,-2表示倒数第2个页面,0或空表示返回栈底的页面
* 此方法被重载
*/
function getPage()
{
$num_args = func_num_args();
$arr_args = func_get_args();
switch($num_args)
{
case 0:
return $this->getPageOverloading_1();
break;
case 1:
return $this->getPageOverloading_2($arr_args[0]);
break;
}
}
function getPageOverloading_1()
{
return urldecode($this->trackStack[0]);
}
function getPageOverloading_2($position)
{
if($position==0)
{
return $this->getPageOverloading_1();
}
else
{
return urldecode($this->trackStack[count($this->trackStack)+$position]);
}
}


}
?>
相关标签: PHP