Home > php教程 > php手册 > php后台多用户权限组思路与实现程序代码分享

php后台多用户权限组思路与实现程序代码分享

WBOY
Release: 2016-06-06 20:40:08
Original
881 people have browsed it

很多时候我们再开发过程中需要考虑到多用户权限问题,这篇文章大家可以参考下

adminconfig.php 这是后台系统中所有文件权限配置。
fun.php 这是一个功能函数
left.php 网站后台根据用户登录的ID来加载相对应的功能菜单
op.php 调用adminconfig.php 默认权限文件
opsava.php 保存用户权限成一个php文件
好了我们先来看看 fun.php文件吧。
代码如下:

function findsub($keys ,$userid='abc' ) //此函数重要就是为了调用用户的权限信息
{
include('user/'.$userid.'.php');
foreach($bb as $key=>$submenu)
{
foreach($submenu as $subkey=>$menuitem)
{
if( $subkey == $keys )
{
return 1;
}
}
}
}
//下面为生成用户的php权限文件
//写入
function cache_write($name, $var, $values) {
$cachefile = 'op/www.hzhuti.com/'.$name.'.php';
$cachetext = "";
if(!swritefile($cachefile, $cachetext))
{
exit("File: $cachefile write error.");
}
}
//数组转换成字串
function arrayeval($array, $level = 0) {
$space = '';
for($i = 0; $i $space .= "t";
}
$evaluate = "Arrayn$space(n";
$comma = $space;
foreach($array as $key => $val) {
$key = is_string($key) ? '''.addcslashes($key, ''\').''' : $key;
$val = !is_array($val) && (!preg_match("/^-?d+$/", $val) || strlen($val) > 12) ? '''.addcslashes($val, ''\').''' : $val;
if(is_array($val)) {
$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
} else {
$evaluate .= "$comma$key => $val";
}
$comma = ",n$space";
}
$evaluate .= "n$space)";
return $evaluate;
}
//写入文件
function swritefile($filename, $writetext, $openmod='w') {
if(@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $writetext);
fclose($fp);
return true;
} else {
exit("File: $filename write error.");
return false;
}
}
?>

adminconfig.php 后台权限配置文件,以一个数组形式保存
代码如下:

$menus = array(
'news' => array(
'caption'=>'资讯管理',
'icon'=>'admin/icon_15.gif',
'sub' => array(
'newsaddtype' => array('caption' => '分类增加', 'url' => 'news/addtype.php'),
'newstypemange' => array('caption' => '分类管理', 'url' => 'news/typemange.php'),
'newsnewsend' => array('caption' => '发布资讯', 'url' => 'news/newsend.php'),
'newsnewmange' => array('caption' => '资讯管理', 'url' => 'news/newmange.php'),
'newscomments' => array('caption' => '评论管理', 'url' => 'news/comments.php'),
),
),
'ask' => array(
'caption'=>'问卷调查',
'icon'=>'admin/icon_15.gif',
'sub' => array(
'voteadmin_subject_add' => array('caption' => '添加投票主题', 'url' => 'vote/admin_subject_add.php'),
'voteadmin_subject' => array('caption' => '管理投票主题', 'url' => 'vote/admin_subject.php'),
'voteadmin_title_add' => array('caption' => '添加投票问题', 'url' => 'vote/admin_title_add.php'),
'voteadmin_title' => array('caption' => '管理投票问题', 'url' => 'vote/admin_title.php'),
'voteadmin_question_add' => array('caption' => '添加投票答案', 'url' => 'vote/admin_question_add.php'),
'voteadmin_question' => array('caption' => '管理投票答案', 'url' => 'vote/admin_question.php'),
'voteadmin_system' => array('caption' => '投票初始配置', 'url' => 'vote/admin_system.php')
),
),
'ads' => array(
'caption'=>'广告管理',
'icon'=>'admin/icon_15.gif',
'sub' => array(
'ada' => array('caption' => '增加广告', 'url' => 'ada.php'),
'adm' => array('caption' => '广告管理', 'url' => 'adm.php'),
'flashadd' => array('caption' => '焦点图广告增加', 'url' => 'flashadd.php'),
'flashad' => array('caption' => '焦点图广告管理', 'url' => 'flashad.php')
),
),
'mange' => array(
'caption'=>'系统管理员',
'icon'=>'admin/icon_15.gif',
'sub' => array(
'adminuser' => array('caption' => '管理员添加', 'url' => 'adminuser.php'),
'modpass' => array('caption' => '密码修改', 'url' => 'modpass.php')
),
),
);
?>

left.php文件很简单就是根据用户登录后ID读出权限
代码如下:

$userid = $_SESSION['adminid'];
include("op/admincofig.php");
include("op/ www.hzhuti.com /".$userid.".php");
foreach($bb as $key=>$submenu)
{
echo " nr ".$menus[$key]['caption']."
nr ";
foreach($submenu as $subkey=>$menuitem)
{
$tmparr = explode("|",$menuitem);
echo "4".$tmparr[0]."
nr";
}
echo " nr ";
}

op.php这个文件就是要调用我们的adminconfig.php文件,然后利用fun.php文件中函数把提供过来的数据保存成数组如图

点击保存我们调用了opsava.php 文件,代码如下
代码如下:

include("op/fun.php");
if( $_POST )
{
$sarray = $_POST ;
cache_write($userid,'bb',$sarray);//写入缓存
echo "<script>alert('编辑保存成功!');location='op.php?userid=".$userid."';</script>";
}
?>

保存成功后文件内容如
代码如下:
$bb=Array
(
'member' => Array
(
'user_search' => '会员升级管理|user_search.php'
),
'membercy' => Array
(
'cyuser_search' => '会员升级管理|cyuser_search.php',
'cyuser_search5' => '交友会员|cyuser_search.php?grade=5'
),
'userid' => 7,
'button' => '保存编辑'
)
?>

这样的话我们的left只要加载 7.php文件就只会显示这里在的文件,这样一个基本的后台权限就完成了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template