Creating a Admin page!


In this tutorial, I will show you how to create a working admin page! I will also show you how to make it on every page!

Alright lets start off.

Admin.php:
PHP Code
  1. <?php
  2. include('config.php'); // gets the database and sitename if needed
  3. session_start(); // starts session
  4. if($logged[username]){
  5. $checkuser = $logged['username']; // gets the username for the code below
  6. $user = mysql_query("SELECT userlevel FROM members WHERE username = '$checkuser' ORDER BY id DESC LIMIT 1") or die (mysql_error());
  7. $fetchuser = mysql_fetch_assoc($user);
  8. $admin = $fetchuser['userlevel']; // will get the userlevel from the sql database
  9. if($admin == 2) {
  10. echo "Zup, admin? Your userlevel is $admin"; // displays admin message (userlevel 6)
  11. }
  12. if($admin == 4) {
  13. echo "Zup Mod? Your Userlevel is $admin"; // displays mod message (userlevel 4)
  14. }
  15. if($admin == 6) {
  16. echo "Sorry, members do not have access to this page."; // displays normal user message (userlevel 2)
  17. }
  18. } else {
  19. echo "You are not logged in, login, then come back this this page."; // no account logged in
  20. }
  21. ?>


As you can see each level has its own spot, which you can change.

PHP Code
  1. if($admin == 2)

If you see this you know that the user is a admin.

PHP Code
  1. if($admin == 4)

If you see this you know that the user is a mod.

PHP Code
  1. if($admin == 6)

If you see this you know that the user is a member (no access).

Unlike the other Admin page, this one uses MYSQL to actually get the user level on the page, and I tested it, it works.

Now, if you want to only allow some pages to be accessed by admins or mods without a huge chunk of code then do these steps:

1. Open config.php
2. At the end of the script before the ?> add:

PHP Code
  1. $checkuser = $logged['username']; // gets the username for the code below
  2. $user = mysql_query("SELECT userlevel FROM members WHERE username = '$checkuser' ORDER BY id DESC LIMIT 1") or die (mysql_error());
  3. $fetchuser = mysql_fetch_assoc($user);
  4. $admin = $fetchuser['userlevel'];


Alright, so now we just made the mysql in the config file so that we can access it from anywhere as long as we include the config file. Heres an example:

PHP Code
  1. <?php
  2. include('config.php');
  3.  
  4. if($admin == 6) {
  5. echo "Admin";
  6. }
  7. ?>


If you include the config file, you can always use the $admin variable as long as you include that config.

It is a simple way of making a admin page, but it works.

Changing the pages for admins / mods / ect. :

Alright, its fairly simple, If you seen the code above, then its easy. Here:

PHP Code
  1. if($admin == 6) {
  2. echo "Zup, admin? Your userlevel is $admin"; // displays admin message (userlevel 6)
  3. }


That code above in the "echo" will show that message if the user is an admin. To change the messages for that user just take the " and " and change it to the level you want. Make sure if you are using a form to put ' instead of ".
I hope this tutorial helps you a lot. s
Diablosblizz's Avatar
Views:
2,880
Rating:
Posted on Thursday 21st June 2007 at 02:40 PM
new2old
new2old's Avatar
MCP you need to add them yourself
Posted on Saturday 9th June 2007 at 01:15 PM
ShadowMage
ShadowMage's Avatar
Code
if($admin == 2) {
echo "Zup, admin? Your userlevel is $admin"; // displays admin message (userlevel 6)
}
if($admin == 4) {
echo "Zup Mod? Your Userlevel is $admin"; // displays mod message (userlevel 4)
}
if($admin == 6) {
echo "Sorry, members do not have access to this page."; // displays normal user message (userlevel 2)
}


The problem with that is level 2 is member and level 6 is admin :P
Posted on Tuesday 29th May 2007 at 01:32 AM
MCP
MCP's Avatar
NVM I fixed that. Now it says "Zup, admin? Your userlevel is 6" Where do I see the admin controls now?
Posted on Tuesday 29th May 2007 at 01:30 AM
MCP
MCP's Avatar
I use this script and it says I am not admin. I set myself to admin. Why can't I access this panel?
Posted on Monday 14th May 2007 at 12:30 PM
ShadowMage
ShadowMage's Avatar
same with
PHP Code
  1. <?
  2. echo "<a href="link">name</a>";
  3. ?>
Posted on Monday 14th May 2007 at 10:49 AM
Dean
Dean's Avatar
yes but if you do echo(" <a href="link.php"> "); you will get a T String error on line w,e
Posted on Sunday 13th May 2007 at 01:46 PM
Diablosblizz
Diablosblizz's Avatar
Same thing. echo (" ....... ") is the same as echo ".......";
Posted on Saturday 12th May 2007 at 10:41 PM
Dean
Dean's Avatar
if you do echo(" <a href="link.php"> "); you will need to make it echo(" <a href='link.php'> "); OR echo(" <a href="link.php"> ");
Posted on Saturday 12th May 2007 at 09:43 PM
Diablosblizz
Diablosblizz's Avatar
True. I tend to use the ' more because its simple. But " does work too.
Posted on Saturday 12th May 2007 at 09:37 PM
SkillMaster
SkillMaster's Avatar
Make sure if you are using a form to put ' instead of ".

Or just use before " or '