先把今天要用的幾個函數羅列出來:
//explode()轉換成數組,implode()轉化成字符串
explode("分隔符",需要被分割的字符串或變量)
$priv="product,index";
explode(",",$priv);
//判斷一個字符串是否存在於一個數組中
in_array(被判斷的,數組)
$now_page="index";
inarray($now_page,$priv);
//將數組用分隔符分成字符串
join("分隔符",數組)
join() 函數是 implode() 函數的別名。
//字符串替換
str_replace(find,replace,string,count)
count是可選的一個變量,對替換數進行計數。
該函數對大小寫敏感。請使用 str_ireplace() 執行對大小寫不敏感的搜索。
$priv="product,index";
str_replace("product","apple",$priv)
//輸出"apple,index"
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i)
);
echo "Replacements: $i";
?>
輸出:
Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1
<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr)
);
?>
輸出:
Array ( [0] => B [1] => [2] => ! )
//string position 字符位置
strpos(字符串,要找的)
return 1.找得到返回字符位置,2.找不到,返回false
$priv = "apple,index";
$i = strpos($priv,"ex");
var_dump($i); //輸出9
//file_put_contents() 函數把一個字符串寫入文件中。
與依次調用 fopen(),fwrite() 以及 fclose() 功能一樣。
file_put_contents(file,data,mode,context)
參數 | 描述 |
---|---|
file | 必需。規定要寫入數據的文件。如果文件不存在,則創建一個新文件。 |
data | 可選。規定要寫入文件的數據。可以是字符串、數組或數據流。 |
mode | 可選。規定如何打開/寫入文件。可能的值:
|
context | 可選。規定文件句柄的環境。 context 是一套可以修改流的行為的選項。若使用 null,則忽略。 |
//file_get_contents(文件名)
file_get_contents() 函數把整個文件讀入一個字符串中。
PHP中file() 函數和file_get_contents() 函數的作用都是將整個文件讀入某個介質,其主要區別就在於這個介質的不同。
file() 函數是把整個文件讀入一個數組中,然后將文件作為一個數組返回。數組中的每個單元都是文件中相應的一行,包括換行符在內。如果失敗,則返回 false。
file_get_contents() 函數是把整個文件讀入一個字符串中。和 file() 一樣,不同的是 file_get_contents() 把文件讀入一個字符串。file_get_contents() 函數是用於將文件的內容讀入到一個字符串中的首選方法。如果操作系統支持,還會使用內存映射技術來增強性能。
把文本文件a.txt的內容保存到一個數據下面是a.txt的內容如下.
aaaaaa
bbbbbb
bbbbccc
好了,就這三行,現在看file_get_contents的操作方法.
$content = file_get_content('a,txt');
$temp =str_replace(chr(13),'|',$content);
$arr =explode('|',$temp);
也不多只有三行就完成了,那么我們來看看file函數吧.
$content = file('a.txt');
就完成了,效果與上面完全相同的.
//file_exists(文件名) 判斷文件是否存在
//end() 函數將數組內部指針指向最后一個元素,並返回該元素的值(如果成功)。
以上基本就是做管理員權限需要用到的函數了,現在把代碼貼出來
//admin_group_add。管理員組添加頁
//鏈接數據庫
require("config.php");
//檢測COOKIE
require("check.php");
//權限添加,修改
//1.在管理員數據表增加權限字段
//2.修改用戶界面,增加權限checkbox
//3.接受前台提交POST
//4.把post變成字符串存入數據庫
if($_POST)
{
$groupname = $_POST['groupname'];
//由於表單提交的name屬性值為$priv,為一個數組,因此將表單提交的數組轉變成字符串
$private = join(",",$_POST['priv']);
//將$pri存入數據庫里
$sql = "INSERT INTO `admin_group`(`groupname`,`private`) VALUES ('{$groupname}', '{$private}')";
$result = mysql_query($sql);
if(!$result)die(mysql_error());
}
$priv = array(
"index" => array(
"index" => "新聞列表",
"add_news" => "新聞添加",
"category_news" => "新聞分類管理",
"edit_news" => "新聞修改",
"message" => "評論管理"
),
"case" => array(
"case" => "案例列表"
),
"product" => array(
"product" => "產品列表管理",
"add_product" => "產品添加",
"edit_product" => "產品修改",
"category_product" => "產品分類管理"
),
"admin" => array(
"admin" => "用戶列表管理",
"add_admin" => "用戶添加",
"edit_admin" => "用戶修改",
"admin_group" => "用戶組管理",
"admin_group_add" => "用戶組權限添加"
)
);
HTML表單代碼:
<form method="post" action="">
<fieldset> <!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
用戶組名稱:<input type="text" name="groupname" /><br />
<p>請選擇用戶權限:</p>
<?php foreach($priv as $child_priv):?>
<?php foreach($child_priv as $p_key => $p):?>
<input type="checkbox" value="<?php echo $p_key?>" name="priv[]" /><?php echo $p?><br />
<?php endforeach; ?>
<hr />
<?php endforeach; ?>
<input type="button" onclick="setCheck('priv[]',0)" value="全不選" />
<input type="button" onclick="setCheck('priv[]',1)" value="全選" />
<input type="button" onclick="setCheck('priv[]',2)" value="反選" />
<input class="button" type="submit" value="提交新的用戶組" />
</fieldset>
<div class="clear"></div><!-- End .clear -->
</form>
//admin_group。管理員組列表頁,//admin_group_check。管理員權限檢測頁
//連接數據庫
require("config.php");
//檢測COOKIE
require("check.php");
//后台的管理權限檢測
//按后台的文件名,作為索引,去判斷管理員是否有權限
//index.php,product.php,admin.php,case.php
//總管:ALL,丫鬟:product,index , 苦力:product,case,index , 家丁:case,product, 奶媽:case
//當我們打開頁面時,判斷當前登錄的是哪個賬號,找出相應的權限。
//explode()轉換成數組,in_array(),判斷一個字符串是否存在於一個數組中,
//打開平常頁面的處理
//1.讀取當前的登錄賬號
$userid = $_COOKIE["admin_userid"];
//2.按當前登陸賬號,獲取權限列表
$sql = "SELECT
`admin_group`.`private`
FROM
`admin_member`
JOIN `admin_group` ON `admin_member`.`group` = `admin_group`.`groupname`
where `admin_member`.`id` = '{$userid}' ";
$result = mysql_query($sql);
if(!$result)
{
die(mysql_error());
}
$row = mysql_fetch_assoc($result);
$priv = $row['private'];
$priv_dat = explode(",",$priv);
//3.獲取當前頁面的文件名
function getNowPageName()
{
//利用超全局變量$_SERVER獲取當前文件名
$page_url = $_SERVER['PHP_SELF'];
//用explode()函數將當前頁面的名字按'/'分隔成數組,end()函數取最后一組,可得到:XXX.php,XXX代表文件名
$page_name = end(explode('/',$page_url));
//將.php'后綴去掉
$filename = str_replace('.php','',$page_name);
return $filename;
}
$filename = getNowPageName();
//4.沒權限跳轉到歡迎頁面
if(!in_array($filename,$priv_dat))
{
echo "<script>alert('太監不得入內!');location.href='index.php';</script>";
}
$sql = "SELECT * FROM `admin_group` where 1";
$result = mysql_query($sql);
if(!$result)
{
die(mysql_error());
}
//讀取數據庫里的`admin_group`表格
$sql = "SELECT * FROM `admin_group` where 1 LIMIT $offset,$per_num";
$result = mysql_query($sql);
if(!$result)
{
die(mysql_error());
}
<tbody>
<?php while($row = mysql_fetch_assoc($result)):?>
<tr>
<td><input type="checkbox" /></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['groupname']?></td>
<?php endwhile; ?>
</tbody>