- 精品下载 | 实用查询 | 词典查询 | 桌面壁纸 | 网址 | 笑话 | FLASH频道 | 天气文章资讯 | 站长工具 | 证件办理 | 闪字生成 | 广告代码 | 在线手册 | 有问必答
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> PHP >> 正文
站内文章搜索:           

面向过程与面向对象的简单比较

作者:佚名    文章来源:网络转载    更新时间 :2007-11-26 23:52:52
最近打开我以前做的一个项目,系统结构中使用了4个包含文件对登录用户的权限进行判断,属典型的面向过程写法,可能很多朋友以前都写过这样的代码。我把这些代码整理了一下,写成一个权限判断的简单类,以比较一个面各对象和面向过程之间的差异。
代码如下(其中省略了部分代码)。

sesson1.php
<?php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,并是否具有系统管理员权限
* 程序员:xiangli
* 日期:2002-07-19
*/

$UserName = $HTTP_COOKIE_VARS['UserName1'];//用户名
if ( empty($UserName) || $HTTP_COOKIE_VARS['Level'] != 1 )
{
    header("Location: ../right.phtml");
}
?>

session2.php
<?php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,并是否具有操作员权限
* 程序员:xiangli
* 日期:2001-07-19
*/

$UserName = $HTTP_COOKIE_VARS['UserName1'];//用户名
$Level = $HTTP_COOKIE_VARS['Level'];//权限级别

if ( empty($UserName) || $Level > 2 )
{
    header("Location: ../index.phtml");
}
?>

session3.php
<?php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,用户是否具有普通用户权限
* 程序员:xiangli
* 日期:2001-07-19
*/

if ( empty($UserName1) || $Level > 3 )
{
    header("Location: ./right.phtml");
}
?>

session4.php
<?php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,用户是否具有企业用户权限
* 程序员:xiangli
* 日期:2001-08-11
*/

if ( empty($_COOKIE['ClientName']) || $_COOKIE['Level'] != 4 )
{
    #header("Location: ../client_login.phtml");
}
?>

调用:
<?
include_once("/lib/session1.php");
include_once("/lib/session2.php");
include_once("/lib/session3.php");
include_once("/lib/session4.php");
?>

合并后的权限判断类:
sessionPower.php
<?php
/**
* @功能:根据cookie的值判断用户是否已经登录及用户的权限
* @程序员:xiangli
* @日期:2002-12-20
*/

class sessionPower{
    var Username;//用户名
    var Level;//用户权力级别
    
    /**
    * 判断用户是否已经登录
    */
    function sessionPower()
    {
        $this->UserName = $HTTP_COOKIE_VARS['UserName'];//用户名
        $this->Level = $HTTP_COOKIE_VARS['Level'];//权限级别

        if ( $this->UserName == "" || $this->Level == "" )
        {
            header("Location: ../index.phtml");
        }
    }
    
    /**
    * 是否具有系统管理员权限
    */
    function adminPower()
    {
        if ( $HTTP_COOKIE_VARS['Level'] != 1 )
        {
            header("Location: ../right.phtml");
        }
    }
    
    /**
    * 是否具有操作员权限
    */
    function operatorPower()
    {
        if ( $this->Level > 2 )
        {
            header("Location: ../index.phtml");
        }
    }
    
    /**
    * 是否具有普通用户权限
    */
    function generalPower()
    {
        if ( $this->Level > 3 )
        {
            header("Location: ./right.phtml");
        }
    }
    
    /**
    * 用户是否具有企业用户权限
    */
    function enterprisePower()
    {
        if ( $this->Level != 4 )
        {
            #header("Location: ../client_login.phtml");
        }
    }    
}
?>

调用:
<?
include_once("/lib/sessionPower.php");
$sessionPower = new sessionPower();
$sessionPower->adminPower();
$sessionPower->operatorPower();
$sessionPower->generalPower();
$se

[1] [2] 下一页

 
【相关文章:】
没有相关文章

发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★
网友评论:
数据载入中,请稍后……