User Personalization with PHP: Home Page | WebReference

User Personalization with PHP: Home Page

By Leidago Noabeb


[next]

In this article, we will be looking at the home page of the application. This is the first page that the user will see after they have gone through the authentication process. The page will provide links and access to the rest of the application, except the admin section.

The Main Script

So what is on the page? The script has the following sections:

  • News headlines
  • Listing of the users stored bookmarks
  • Navigation panel
  • User image and name
  • Bookmark Recommendations

These functional requirements have already been mentioned in the introductory article to the series. In this article, we will actually implement them. Take a look at the home page screenshot below to see it in action:


See Figure 1

As with the previous scripts, we have sectionalized the page in PHP and HTML code. But as you will see later on, the presentation layer (HTML) is mixed in with some PHP code--especially where the users' color scheme is implemented. Below is the PHP section of the page that retrieves the necessary database information:


Keep in mind that when a user is authenticated, their user ID is stored in a session variable, called "$_SESSION['id']" (without the quotes). This is done in the login page. This piece of information is very important to the application since it is going to be used to identify the user and their access level. The code starts by including a file that contains the database connection information. This information is going to be used to make a connection to the database server. It also contains some functions that will be used throughout the application. The second block of code is a conditional, which checks to see if the user is allowed to access this page. It is a relatively simple check that involves a test to see if a session variable is set:

if(!isset($_SESSION['uname'])){

If the username is not set than the user is simply redirected to the login page. This is because any user that has come so far needs to be authenticated, if not then the application is compromised, thus we send the user back to the login page to be authenticated:

//redirect to login page
header("location:login/login.php");
}

Now, using the user id mentioned previously, the code retrieves the users' bookmarks:

$get_bookmarks = "SELECT * FROM bmarks WHERE uid = '".$_SESSION['id']."'";

Then it stores the number of rows or records retrieved:

$num = mysql_num_rows($res_bm);

We store the number of rows retrieved in a variable called $num. This variable will later be used to determine if we can build a dynamic table filled with the users' bookmark information.

Now, in the introductory section, we've mentioned the bookmarks recommendation section. The next SQL code is one of the first steps towards implementing the code that makes up a list of recommended URLs:

$get_bm1 = "SELECT url FROM bmarks WHERE uid ='".$_SESSION['id']."'";

The code then stores the retrieved list of URLs in a array:

while($row = mysql_fetch_assoc($res_bm1)){
array_push($s,$row['url']);

The last query in this section retrieves all of the bookmarks in the system that do not have the same ID as the current user:

$get_bm2 = "SELECT url FROM bmarks WHERE uid!='".$_SESSION['id']."'";

... and then stores the information into a array:

while($row2 = mysql_fetch_assoc($res_bm2)){
array_push($b,$row2['url']);

The first part of the home page shows the bookmarks that are stored by the current user. It also provides the user with a choice to either change or edit, delete the bookmark information. The following code implements the functionality:

So what does the code do exactly? It uses the $num variable that we stored earlier to retrieve the records that is located in the results of the first query. First we create a new table that will hosts the URL and descriptions thereof:

Now, we need to create dynamic rows with the information contained within the returned results of our query. We do this by first checking if the $num variable contains values that are greater than zero. This is because if the value contained in the variable is not greater than zero then no records are found and there will be no need to create dynamic rows:

&lt?php		  
	if($num > 0){

Next, assuming that the variable contains a value that is greater than zero, we create an associate array, using MYSQL's mysql_ fetch_assoc() function. This array will now contain all of the returned records from the $get_bookmarks query. To retrieve the URLs and their respective descriptions, we simply loop through the array:

Then we mix HTML and PHP to present the information that we are retrieving from the array. In this case, all of the URLs are represented by the $row['url'] variable and the description of the URLs is represented by the $row['desc'] variable:

To provide the user with the options to delete or change bookmark information, we provide a bookmark ID and links to the delete and changebookmarks.php scripts. By clicking on these hyperlinks, the user will be taken to these pages, where each option will be exercised:

With each iteration through the contents of the array, a new row will be created that will display the above information.

The Navigation Panel

The navigation panel will be the same for all of the scripts in this application. It has five options for the users' convenience. These options make it easy for the user to move backward and forward through the application. They are:

  • Logout
  • This option enables users to logout of the system. When a user clicks on this link, they are transferred back to the login page.

  • Home
  • This link brings the user to the home page.

  • Add Bookmarks
  • This link takes the user to the "Add Bookmarks" page where the user will be able to add new bookmarks.

  • Admin
  • This link will take the user to the Admin section of the website. This option is available to users with admin privileges only.

The code that makes up the navigation panel is straight forward except where we have to implement the admin only access:

The admin limitation is carried out by the PHP code that checks if the $_SESSION['admin'] variable contains the 'admin' value. If so, the option will appear on the navigation panel when the user reaches this page, otherwise it will not be listed.


[next]