Creating an Admin Panel


hey, this is my first tutorial so i hope you like it. The tutorial will teach / show you how to create an admin panel for your userysstem. i am using the levels:
6 = Admin
4 = Mod
2 = Validated user
I have tried to comment as much as possible!
So here goes
Admin.php
PHP Code
  1. <?
  2. session_start(); //allows session
  3. include "config.php"; // Includes the config
  4. if($logged[username] && $logged[userlevel] == 6)// Checks you are an admin
  5. {
  6. if($_GET[member])//gets the username
  7. {
  8. if (!$_POST[edit])//Checks if you are editing a user already
  9. {
  10. $user = mysql_query("SELECT * from members where username = '$_GET[member]'");// Selcts the right user from the database
  11. $users = mysql_fetch_array($user);
  12. echo "<form method=\"POST\">
  13. <table width=\"100%\">
  14. <tr>
  15. <td align=\"right\" width=\"25%\">
  16. User Level</td>
  17. <td align=\"left\">
  18. <select name=\"level\">
  19. <option value=\"2\">User</option>
  20. <option value=\"4\"> Mod </option>
  21. <option value=\"6\"> Admin</option>
  22. </select> </td>
  23. </tr>
  24. <tr>
  25. <td align=\"right\" width=\"25%\">
  26. Email</td>
  27. <td align=\"left\">
  28. <input type=\"text\" size=\"25\" maxlength=\"25\" name=\"email\"
  29. value=\"$users[email]\"></td>
  30. </tr>
  31. <tr>
  32. <td align=\"right\" width=\"25%\">Location</td>
  33. <td align=\"left\">
  34. <input size=\"25\" name=\"location\" value=\"$users[location]\"></td>
  35. </tr>
  36. <tr>
  37. <td align=\"right\" width=\"25%\">Age</td>
  38. <td align=\"left\">
  39. <input size=\"25\" name=\"age\" value=\"$users[age]\"></td>
  40. </tr>
  41. <tr>
  42. <td align=\"center\"></td>
  43. <td align=\"left\">
  44. <input type=\"submit\" name=\"edit\" value=\"Update\"></td>
  45. </tr>
  46. </table>
  47. </form> <form method=\"POST\">
  48. <table width=\"100%\">
  49. <tr>
  50. <td align=\"right\" width=\"25%\">
  51. User Level</td>
  52. <td align=\"left\">
  53. <select name=\"level\">
  54. <option value=\"2\">User</option>
  55. <option value=\"4\"> Mod </option>
  56. <option value=\"6\"> Admin</option>
  57. </select> </td>
  58. </tr>
  59. <tr>
  60. <td align=\"right\" width=\"25%\">
  61. Email</td>
  62. <td align=\"left\">
  63. <input type=\"text\" size=\"25\" maxlength=\"25\" name=\"email\"
  64. value=\"$users[email]\"></td>
  65. </tr>
  66. <tr>
  67. <td align=\"right\" width=\"25%\">Location</td>
  68. <td align=\"left\">
  69. <input size=\"25\" name=\"location\" value=\"$users[location]\"></td>
  70. </tr>
  71. <tr>
  72. <td align=\"right\" width=\"25%\">Age</td>
  73. <td align=\"left\">
  74. <input size=\"25\" name=\"age\" value=\"$users[age]\"></td>
  75. </tr>
  76. <tr>
  77. <td align=\"center\"></td>
  78. <td align=\"left\">
  79. <input type=\"submit\" name=\"edit\" value=\"Update\"></td>
  80. </tr>
  81. </table>
  82. </form>"; // Displays the form
  83. }else{
  84. $email = htmlspecialchars($_POST[email]);//removes all HTML
  85. $age = htmlspecialchars($_POST[age]);//removes all HTML
  86. $location = htmlspecialchars($_POST[location]);//removes all HTML
  87. $level = htmlspecialchars($_POST[level]);//removes all HTML
  88. echo "Thank you $logged[username], $_GET[member]'s Profile was updated!"; // Shows the message of your choice (feel free to change it)
  89. $update = mysql_query("Update members set location = '$location', age = '$age', level = '$level', email = '$email' where username = '$_GET[member]'"); // updates the user
  90. }
  91. }else{
  92. $adminuser = mysql_query("Select * from members order by username asc"); //Makes a list of all the users in alphabeticle order!
  93. while($adminusers = mysql_fetch_array($adminuser))
  94. {
  95. echo "<a href='admin.php?member=$adminusers[username]'>$adminusers[username]</a>"; //Displayes the links to edit the user.
  96. }
  97. }
  98. }else{
  99. //If someone is trying to get onto this page without being an admin then they wont get through!
  100. echo "Sorry, but only Admins may view this page. Please go back!";
  101. }
  102. ?>

Well thats all i can think of at the moment, Any problems comment below! Also for admin only pages the code is
PHP Code
  1. <?
  2. session_start(); //allows session
  3. include "config.php"; // Includes the config
  4. if($logged[username] && $logged[userlevel] == 6)// Checks you are an admin
  5. { echo "Admin stuff here";
  6. }else{
  7. echo "Sorry only admins!";
  8. }
  9. ?>

Hope this was a good tutorial!
MOD-Dan's Avatar
Author:
Views:
4,262
Rating:
Posted on Saturday 8th November 2008 at 09:06 PM
Dava
Dava's Avatar
god knows just remove one of them
Posted on Saturday 8th November 2008 at 06:22 PM
shedh
shedh's Avatar
Why is their two forms on the page?
Posted on Thursday 5th June 2008 at 05:22 AM
Adam981
Adam981's Avatar
Well, im back with a new host and ive tryed this again, but it just doesnt update the userrs level or information.. and also theres to forms displayed
Posted on Saturday 3rd May 2008 at 03:05 AM
Adam98
Adam98's Avatar
Ok well all the errors are fixed, just doesnt update the users information or rank.? Sigh:(
Posted on Saturday 3rd May 2008 at 02:41 AM
ShadowMage
ShadowMage's Avatar
okay, or if you want you can send me a pm
Posted on Saturday 3rd May 2008 at 02:29 AM
Adam98
Adam98's Avatar
Ok, ill give that a try.. host is being slow and stupid again. but thanks for the help, ill post and let u know if it worked.
Posted on Saturday 3rd May 2008 at 02:25 AM
ShadowMage
ShadowMage's Avatar
Replace:
Code
<form method="POST">


With
Code
<form method="POST">
Posted on Saturday 3rd May 2008 at 02:23 AM
Adam98
Adam98's Avatar
<?
session_start(); //allows session
include "config.php"; // Includes the config
if($logged[username] && $logged[userlevel] == 6)// Checks you are an admin
{
if($_GET[member])//gets the username
{
if (!$_POST[edit])//Checks if you are editing a user already
{
$user = mysql_query("SELECT * from members where username = '$_GET[member]'");// Selcts the right user from the database
$users = mysql_fetch_array($user);
echo "<form method="POST">
Posted on Saturday 3rd May 2008 at 02:21 AM
ShadowMage
ShadowMage's Avatar
Alright, show me the line above them real quick.

Code
$user = mysql_query("SELECT * from members where username = '".$_GET[member]."'");// Selcts the right user from the database
Posted on Saturday 3rd May 2008 at 02:18 AM
Adam98
Adam98's Avatar
$user = mysql_query("SELECT * from members where username = '$_GET[member]'");// Selcts the right user from the database
$users = mysql_fetch_array($user);