有时候开发项目时,需要用到权限控制,这里提供一种简单的方法,表单页面:
<table width="100%">
<tbody>
<tr height="20" >
<td align="left">
<input name="auth[project-index]" value="1" id="c_project" type="checkbox" onclick="setC('project')" <?php if ($auth['project-index']) echo 'checked' ?> >项目管理</td>
<td align="left">
<input class="c_project" name="auth[project-add]" type="checkbox" value="1" <?php if ($auth['project-add']) echo 'checked' ?> ><span>添加</span>
<input class="c_project" name="auth[project-edit]" type="checkbox" value="1" <?php if ($auth['project-edit']) echo 'checked' ?> ><span>修改</span>
<input class="c_project" name="auth[project-del]" type="checkbox" value="1" <?php if ($auth['project-del']) echo 'checked' ?> ><span>删除</span>
</td></tr>
<tr height="20">
<td align="left">
<input name="auth[dep-index]" value="1" id="c_dep" type="checkbox" onclick="setC('dep')" <?php if ($auth['dep-index']) echo 'checked' ?> >部门管理 </td>
<td align="left">
<input class="c_dep" name="auth[dep-add]" type="checkbox" value="1" <?php if ($auth['dep-add']) echo 'checked' ?> ><span>添加</span>
<input class="c_dep" name="auth[dep-edit]" type="checkbox" value="1" <?php if ($auth['dep-edit']) echo 'checked' ?> ><span>修改</span>
<input class="c_dep" name="auth[dep-del]" type="checkbox" value="1" <?php if ($auth['dep-del']) echo 'checked' ?> ><span>删除</span>
</td>
</tr>
<tr height="20">
<td align="left">
<input name="auth[user-index]" value="1" id="c_user" type="checkbox" onclick="setC('user')" <?php if ($auth['user-index']) echo 'checked' ?> >用户管理 </td>
<td align="left">
<input class="c_user" name="auth[user-add]" type="checkbox" value="1" <?php if ($auth['user-add']) echo 'checked' ?> ><span>添加</span>
<input class="c_user" name="auth[user-edit]" type="checkbox" value="1" <?php if ($auth['user-edit']) echo 'checked' ?> ><span>修改</span>
<input class="c_user" name="auth[user-del]" type="checkbox" value="1" <?php if ($auth['user-del']) echo 'checked' ?> ><span>删除</span>
</td>
</tr>
</tbody>
</table>
实现选中多个的JS方法(不要忘记引入jquery.js文件)
function setC(c) {
if($("#c_"+c).attr('checked')) {
$(".c_"+c).attr("checked",true);
} else {
$(".c_"+c).attr("checked",false);
}
}
假如选中项目添加和任务添加复选框,提交后用$_POST['auth']接收到的数据是这样的数组形式:array('project-add'=>1,'task-add'=>1)
用php函数转化成字符串:$auth = array2string($_post['auth']),存到角色表的auth字段中
/**
* 将数组转换为字符串
*/
function array2string($data, $isformdata = 1)
{
if ($data == '') return '';
if ($isformdata) $data = new_stripslashes($data);
return serialize($data);
}
/**
* 返回经stripslashes处理过的字符串或数组
*/
function new_stripslashes($string)
{
if (!is_array($string)) return stripslashes($string);
foreach ($string as $key => $val) $string[$key] = new_stripslashes($val);
return $string;
}
当这个用户登录时,获取它所属的角色的auth字段值$data['auth'],然后$UserInfo['auth'] = string2array($data['auth']),将字符串转化成数组
/**
* 将字符串转换为数组
*/
function string2array($data)
{
if ($data == '') return array();
return unserialize($data);
}
假如现在要判断它是否具有任务添加的权限,则
if($checkAuth('task-add')){
echo('具有任务添加的权限');
}
function checkAuth($string){
global $UserInfo;
if ($UserInfo['auth'][$string]){
return true;
}else {
return false;
}
}