User Personalization with PHP: The Admin Section | WebReference

User Personalization with PHP: The Admin Section

By Leidago Noabeb


[next]

Welcome to the final section of the series. In this part of User Personalization application, we will be focusing on the Administration of the application. Administration of an application is necessary because any website or application that involves users becomes bloated and very slow. This can be because many users have stopped using the application without removing some of the information that they were using. As a result, a lot of redundant data is left in the system that causes it to become slow. For this reason, we need to have a section in the application that will do the 'clean up' of redundant data or 'house keeping'. The first script that we will look at in this section is the entry page of the section.

The Index Script

Whenever a user logs into the system, their access level is determined. A user will have either an access level that is normal or admin. If a user is admin, then they have access to the admin section of the application. You will note that when a user is not admin the navigation panel does not include a link to the admin section. The index page is the first page in the admin section and presents the user with two management options, which is to manage both users and bookmarks. These options provide links to both sections. The page has the following look and code:

See Figure 1

Below is the code that makes up the page:

The page has two hyperlinks that either takes you to the user or bookmark management pages. Once you click on either link, you will be able to exercise some of the options that are available on those pages. There is also other code:

The code above does two things, first it opens a session and then it checks if the user has admin access. If the user has admin access, they are allowed to view the page otherwise, the user is redirected to the login page, because either the user was trying to access the page directly or the user was not authenticated. This bit of code will be placed on each page of the admin section.

Managing Bookmarks

Bookmarks are at the heart of our system. It is what the system is all about. Therefore, it is no surprise that we need to manage it. Users will be adding all kinds of links to the system some that they will need and others that they will not. It is the ones that they don't need that will cause our system to slow down, because each bookmark is stored in a database and this database is used by all of the registered users, which over time can become too many for the database to handle depending on the popularity of the application. Therefore, this script has to reduce the amount of any redundant bookmark information. The users themselves do actually have the ability to add or remove any bookmarks that they don't need, but because we don't want to rely on the user to do this we need to have overall control of the situation; hence this script. So what does the script do? Basically, it retrieves all of the bookmarks in the system and provides an option to remove a given bookmark from the database. Once the user clicks on the delete option, they are taken to the delete script that actually does the job of removing the bookmark from the database. Below is what the script looks like:

See Figure 2

The page has both an HTML and PHP section. Below is the main PHP code:

The first test that is carried out is to check if the user has the right to be on this page. To determine the users access privileges, the code checks if a session variable called $_SESSION['level'] is set to 'admin':

if($_SESSION['level'] !== 'admin'){

If the user does not have admin level access then the user is sent to the login page:

header("location:../login/login.php");

Next we set an error message variable and then start to retrieve all of the bookmarks in the system. We do this by running a query like so:

$get_bookmarks = "SELECT * FROM bmarks ORDER by bid";

Then we run the query and make provision for any errors that may occur while the query is being executed. If no errors are found, we retrieve a count of the number of records that is available:

The HTML section of the page is responsible for displaying a list of all the bookmarks in the system. The page also includes a navigation panel that enables the user to access different parts of the admin section. It contains an HTML table that will host a list of bookmarks. This table will be part dynamic and part static. Below is the code for the entire page:

Let's take a step-by-step look through the code. The very first PHP code that we get is to display any error messages that may have occurred while the database data was being retrieved:

Then the code continues to build a table with two headers, URL and Action:

After which it starts with building the dynamic portion of the table. First, it determines if the $num variable that we created in the main PHP code has a value greater than zero:

if($num > 0){

.. if so, it creates a while() loop and sets a new array called $row to store all of the records:

while($row = mysql_fetch_assoc($res_bm)){

To create the dynamic table rows, the code iterates through the $row array and with each iteration builds a dynamic table row that lists the bookmark and the action that needs to be taken:

If no bookmarks are found in the system, then the code says so:


[next]