| only for RuBoard - do not distribute or recompile |
Now we'll move on and look at how a user's bookmarks are stored, retrieved, and deleted.
Users can add bookmarks by clicking on the Add BM link in the user menu. This will take them to the form shown in Figure 24.9.

Again, this script is simple and uses just the output functions, so we will not go through it here. When the form is submitted, it calls the add_bms.php script, which is shown in Listing 24.21.
<?
require_once("bookmark_fns.php");
session_start();
do_html_header("Adding bookmarks");
check_valid_user();
if (!filled_out($HTTP_POST_VARS))
{
echo "You have not filled out the form completely.
Please try again.";
display_user_menu();
do_html_footer();
exit;
}
else
{
// check URL format
if (strstr($new_url, "http://")===false)
$new_url = "http://".$new_url;
// check URL is valid
if (@fopen($new_url, "r"))
{
// try to add bm
if (add_bm($new_url))
echo "Bookmark added.";
else
echo "Could not add bookmark.";
}
else
echo "Not a valid URL.";
}
// get the bookmarks this user has saved
if ($url_array = get_user_urls($valid_user));
display_user_urls($url_array);
display_user_menu();
do_html_footer();
?>
Again this script follows the pattern of validation, database entry, and output.
To validate, we first check whether the user has filled out the form using filled_out().
We then perform two URL checks. First, using strstr(), we see whether the URL begins with http://. If it doesn't, we add this to the start of the URL. After we've done this, we can actually check that the URL really exists. As you might recall from Chapter 17, "Using Network and Protocol Functions," we can use fopen() to open an URL that starts with http://. If we can open this file, we assume the URL is valid and call the function add_bm() to add it to the database.
Note that fopen() will only be able to open files if your server has direct access to the Internet. If it needs to access other HTTP servers via a proxy server, fopen() will not work.
This function and the others relating to bookmarks are all in the function library url_fns.php. You can see the code for the add_bm() function in Listing 24.22.
function add_bm($new_url)
{
// Add new bookmark to the database
echo "Attempting to add ".htmlspecialchars($new_url)."<BR>";
global $valid_user;
if (!($conn = db_connect()))
return false;
// check not a repeat bookmark
$result = mysql_query("select * from bookmark
where username='$valid_user'
and bm_URL='$new_url'");
if ($result && (mysql_num_rows($result)>0))
return false;
// insert the new bookmark
if (!mysql_query( "insert into bookmark values
('$valid_user', '$new_url')"))
return false;
return true;
}
This function is fairly simple. It checks that a user does not already have this bookmark listed in the database. (Although it is unlikely that they would enter a bookmark twice, it is possible and even likely that they might refresh the page.) If the bookmark is new, then it is entered into the database.
Looking back at add_bm.php, you can see that the last thing it does is call get_user_urls() and display_user_urls(), the same as member.php. We'll move on and look at these functions next.
In the member.php script and the add_bm() function, we used the functions get_user_urls() and display_user_urls(). These functions get the user's bookmarks from the database and display them, respectively. The get_user_urls() function is in the url_fns.php library, and the display_user_urls() function is in the output_fns.php library.
The get_user_urls() function is shown in Listing 24.23.
function get_user_urls($username)
{
// extract from the database all the URLs this user has stored
if (!($conn = db_connect()))
return false;
$result = mysql_query( "select bm_URL
from bookmark
where username = '$username'");
if (!$result)
return false;
//create an array of the URLs
$url_array = array();
for ($count = 1; $row = mysql_fetch_row ($result); ++$count)
{
$url_array[$count] = $row[0];
}
return $url_array;
};
Let's briefly step through this function. It takes a username as parameter, and retrieves the bookmarks for that user from the database. It will return an array of these URLs or false if the bookmarks could not be retrieved.
The array from get_user_urls() can be passed to display_user_urls(). This is again a simple HTML output function to print the user's URLs in a nice table format, so we won't go through it here. Refer back to Figure 24.6 to see what the output looks like. The function actually puts the URLs into a form. Next to each URL is a check box that enables bookmarks to be marked for deletion. We will look at this next.
When a user marks some bookmarks for deletion and clicks on the Delete BM option in the menu, the form containing the URLs will be submitted. Each one of the check boxes is produced by the following code in the display_user_urls() function:
echo "<td><input type=checkbox name=\"del_me[]\"
value=\"$url\"></td>";
The name of each input is del_me[]. This means that, in the PHP script activated by this form, we will have access to an array called $del_me that will contain all the bookmarks to be deleted.
Clicking on the Delete BM option activates the delete_bms.php script. This script is shown in Listing 24.12.
<?
require_once("bookmark_fns.php");
session_start();
do_html_header("Deleting bookmarks");
check_valid_user();
if (!filled_out($HTTP_POST_VARS))
{
echo "You have not chosen any bookmarks to delete.
Please try again.";
display_user_menu();
do_html_footer();
exit;
}
else
{
if (count($del_me) >0)
{
foreach($del_me as $url)
{
if (delete_bm($valid_user, $url))
echo "Deleted ".htmlspecialchars($url).".<br>";
else
echo "Could not delete ".htmlspecialchars($url).".<br>";
}
}
else
echo "No bookmarks selected for deletion";
}
// get the bookmarks this user has saved
if ($url_array = get_user_urls($valid_user));
display_user_urls($url_array);
display_user_menu();
do_html_footer();
?>
We begin this script by performing the usual validations. When we know that the user has selected some bookmarks for deletion, we delete them in the following loop:
foreach($del_me as $url)
{
if (delete_bm($valid_user, $url))
echo "Deleted ".htmlspecialchars($url).".<br>";
else
echo "Could not delete ".htmlspecialchars($url).".<br>";
}
As you can see, the delete_bm() function does the actual work of deleting the bookmark from the database. This function is shown in Listing 24.25.
function delete_bm($user, $url)
{
// delete one URL from the database
if (!($conn = db_connect()))
return false;
// delete the bookmark
if (!mysql_query( "delete from bookmark
where username='$user'and bm_url='$url'"))
return false;
return true;
}
As you can see, this is again a pretty simple function. It attempts to delete the bookmark for a particular user from the database. One thing to note is that we want to remove a particular username-bookmark pair. Other users might still have this URL bookmarked.
Some sample output from running the delete script on our system is shown in Figure 24.10.

As in the add_bms.php script, when the changes to the database have been made, we display the new bookmark list using get_user_urls() and display_user_urls().
| only for RuBoard - do not distribute or recompile |