PHP background Multi-user permission group ideas and Implementation program code

Source: Internet
Author: User

Web site development must have a website backstage, have the background naturally to the user has the same role to assign, especially the case of multi-user system, such as I a system to have more than one administrator, then I these management to be divided into, edit, friendship Connection, administrator, then we have permission and role assignment, Today, let's look at a system I used to make.

First introduce the file,

Adminconfig.php This is the configuration of all file permissions in the background system.

Fun.php This is a function function

left.php Web site background based on the user login ID to load the corresponding function menu

op.php calling adminconfig.php default permission file

opsava.php Save user Rights into a PHP file

Well, let's take a look at the fun.php file first.

The code is as follows Copy Code

<?
Functions Findsub ($keys, $userid = ' abc ')//This function is important in order to invoke the user's permission information
{
Include (' user/'. $userid. php ');
foreach ($bb as $key = $submenu)
{
foreach ($submenu as $subkey = $menuitem)
{
if ($subkey = = $keys)
{
return 1;
}
}
}
}

The following is a PHP permission file that generates a user
Write
function Cache_write ($name, $var, $values) {
$cachefile = ' op/user/'. $name. PHP ';
$cachetext = "<?phprn". ' $ '. $var. ' = '. Arrayeval ($values). " Rn?> ";
if (!swritefile ($cachefile, $cachetext))
{
Exit ("File: $cachefile write error.");
}
}

//array converted to string
function Arrayeval ($array, $level = 0) {
$space = ';
 for ($i = 0; $i <= $level; $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;
}

Write file
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 Background permissions configuration file, saved as an array

The code is as follows Copy Code

<?
$menus = Array (

' News ' = Array (
' Caption ' = ' information management ',
' Icon ' = ' admin/icon_15.gif ',
' Sub ' = = Array (
' Newsaddtype ' = = Array (' caption ' = ' + ' category added ', ' url ' = = ' news/addtype.php '),
' Newstypemange ' = = Array (' caption ' = ' category management ', ' url ' = ' news/typemange.php '),
' Newsnewsend ' + array (' caption ' = ' release info ', ' url ' = ' news/newsend.php '),
' Newsnewmange ' = = Array (' caption ' = ' information management ', ' url ' = ' news/newmange.php '),
' newscomments ' = = Array (' caption ' = ' review management ', ' url ' = ' news/comments.php '),

),
),

' Ask ' = = Array (
' Caption ' and ' survey ',
' Icon ' = ' admin/icon_15.gif ',
' Sub ' = = Array (
' Voteadmin_subject_add ' = = Array (' caption ' + ' Add ' Poll theme ', ' url ' = ' vote/admin_subject_add.php '),
' Voteadmin_subject ' = = Array (' caption ' = ' Manage Poll theme ', ' url ' = ' vote/admin_subject.php '),
' Voteadmin_title_add ' = = Array (' caption ' + ' Add poll question ', ' url ' = ' vote/admin_title_add.php '),
' Voteadmin_title ' = = Array (' caption ' = ' Manage voting issues ', ' url ' = ' vote/admin_title.php '),
' Voteadmin_question_add ' = = Array (' caption ' + ' Add poll answer ', ' url ' = ' vote/admin_question_add.php '),
' voteadmin_question ' = = Array (' caption ' = ' Manage voting answers ', ' url ' = ' vote/admin_question.php '),
' Voteadmin_system ' = = Array (' caption ' = ' voting initial configuration ', ' url ' = ' vote/admin_system.php ')
),
),
' Ads ' = = Array (
' Caption ' = ' advertising management ',
' Icon ' = ' admin/icon_15.gif ',
' Sub ' = = Array (
' ada ' + = Array (' caption ' + ' + ' Add ad ', ' url ' = ' ada.php '),
' adm ' + = Array (' caption ' + ' advertising management ', ' url ' = ' adm.php '),
' Flashadd ' = = Array (' caption ' + ' focus map ad add ', ' url ' = ' flashadd.php '),
' Flashad ' = = Array (' caption ' + ' focus map ad management ', ' url ' = ' flashad.php ')
),
),


' Mange ' = Array (
' Caption ' = ' system Administrator ',
' Icon ' = ' admin/icon_15.gif ',
' Sub ' = = Array (
' Adminuser ' = = Array (' caption ' = ' admin add ', ' url ' = ' adminuser.php '),
' Modpass ' = = Array (' caption ' = ' password modified ', ' url ' = ' modpass.php ')
),
),
);
?>

The left.php file is simple enough to read the permission based on the user's login ID.

The code is as follows Copy Code

<?
$userid = $_session[' Adminid ');
Include ("op/admincofig.php");
Include ("op/user/". $userid. ". PHP ");

foreach ($bb as $key = $submenu)
{
echo "<tr><td bgcolor=" #D879A7 "style=" Line-height:22px;text-align:center;color: #ffffff; " > Nr <font color= "#EA6A8D" face= "Webdings" ></font><b> ". $menus [$key] [' caption ']." </b><br/></td></tr> nr <tr ><td bgcolor= "f5e8f1" > ";
foreach ($submenu as $subkey = $menuitem)
{
$tmparr = Explode ("|", $menuitem);
echo "<font color=" #EA6A8D "face=" webdings ">4</font><a href=". $tmparr [1]. "' target=" mainframe ">" . $tmparr [0]. " </a><br> nr ";
}
echo "</td></tr> nr";
}

op.php This file is to call our adminconfig.php file, and then use the fun.php file function to save the data supplied to the array

Click Save we called the opsava.php file, the code is as follows

The code is as follows Copy Code

<?
Include ("op/fun.php");
if ($_post)
{
$sarray = $_post;
Cache_write ($userid, ' BB ', $sarray);//write Cache
echo "<script>alert (' Edit saved successfully! '); location= ' op.php?userid= ". $userid." '; </script> ";

}
?>

After saving successfully, the contents of the file

The code is as follows Copy Code

<?php
$BB =array
(
' Member ' = Array
(
' User_search ' + ' member upgrade Management |user_search.php '
),
' Membercy ' = Array
(
' Cyuser_search ' + ' member upgrade Management |cyuser_search.php ',
' Cyuser_search5 ' = ' Friends member |cyuser_search.php?grade=5 '
),
' UserID ' = 7,
' Button ' = ' Save edit '
)
?>

In this case, our left just loads the 7.php file and only shows the file here, so a basic background permission is done.

Original station original tutorial reprint annotated Source: http://www.php100.com/phper/php.html

PHP background Multi-user permission group ideas and Implementation program code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.