Specifications

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 doesnt, we add this to the start of the URL. After weve done this, we can actu-
ally 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.
LISTING 24.22 The add_bm() function from url_fns.phpThis Function Adds New
Bookmarks to the Database
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;
}
Building Practical PHP and MySQL Projects
P
ART V
528
30 7842 ch24 3/6/01 3:34 PM Page 528