<?php
require_once('db_fns.php');
function register($username, $email, $password)
// register new person with db
// return true or error message
{
// connect to db
$conn = db_connect();
if (!$conn)
return 'Could not connect to database server - please try later.';
// check if username is unique
$result = mysql_query("select * from user where username='$username'");
if (!$result)
return 'Could not execute query';
if (mysql_num_rows($result)>0)
return 'That username is taken - go back and choose another one.';
// if ok, put in db
$result = mysql_query("insert into user values
('$username', password('$password'), '$email')");
if (!$result)
return 'Could not register you in database - please try again later.';
return true;
}
function login($username, $password)
// check username and password with db
// if yes, return true
// else return false
{
// connect to db
$conn = db_connect();
if (!$conn)
return false;
// check if username is unique
$result = mysql_query("select * from user
where username='$username'
and passwd = password('$password')");
if (!$result)
return false;
if (mysql_num_rows($result)>0)
return true;
else
return false;
}
function check_valid_user()
// see if somebody is logged in and notify them if not
{
global $HTTP_SESSION_VARS;
if (isset($HTTP_SESSION_VARS['valid_user']))
{
echo 'Logged in as '.$HTTP_SESSION_VARS['valid_user'].'.';
echo '<br />';
}
else
{
// they are not logged in
do_html_heading('Problem:');
echo 'You are not logged in.<br />';
do_html_url('login.php', 'Login');
do_html_footer();
exit;
}
}
function change_password($username, $old_password, $new_password)
// change password for username/old_password to new_password
// return true or false
{
// if the old password is right
// change their password to new_password and return true
// else return false
if (login($username, $old_password))
{
if (!($conn = db_connect()))
return false;
$result = mysql_query( "update user
set passwd = password('$new_password')
where username = '$username'");
if (!$result)
return false; // not changed
else
return true; // changed successfully
}
else
return false; // old password was wrong
}
function get_random_word($min_length, $max_length)
// grab a random word from dictionary between the two lengths
// and return it
{
// generate a random word
$word = '';
//remember to change this path to suit your system
$dictionary = '/usr/dict/words'; // the ispell dictionary
$fp = fopen($dictionary, 'r');
if(!$fp)
return false;
$size = filesize($dictionary);
// go to a random location in dictionary
srand ((double) microtime() * 1000000);
$rand_location = rand(0, $size);
fseek($fp, $rand_location);
// get the next whole word of the right length in the file
while (strlen($word)< $min_length || strlen($word)>$max_length || strstr($word, "'"))
{
if (feof($fp))
fseek($fp, 0); // if at end, go to start
$word = fgets($fp, 80); // skip first word as it could be partial
$word = fgets($fp, 80); // the potential password
};
$word=trim($word); // trim the trailing \n from fgets
return $word;
}
function reset_password($username)
// set password for username to a random value
// return the new password or false on failure
{
// get a random dictionary word b/w 6 and 13 chars in length
$new_password = get_random_word(6, 13);
if($new_password==false)
return false;
// add a number between 0 and 999 to it
// to make it a slightly better password
srand ((double) microtime() * 1000000);
$rand_number = rand(0, 999);
$new_password .= $rand_number;
// set user's password to this in database or return false
if (!($conn = db_connect()))
return false;
$result = mysql_query( "update user
set passwd = password('$new_password')
where username = '$username'");
if (!$result)
return false; // not changed
else
return $new_password; // changed successfully
}
function notify_password($username, $password)
// notify the user that their password has been changed
{
if (!($conn = db_connect()))
return false;
$result = mysql_query("select email from user
where username='$username'");
if (!$result)
{
return false; // not changed
}
else if (mysql_num_rows($result)==0)
{
return false; // username not in db
}
else
{
$email = mysql_result($result, 0, 'email');
$from = "From: support@phpbookmark \r\n";
$mesg = "Your PHPBookmark password has been changed to $password \r\n"
."Please change it next time you log in. \r\n";
if (mail($email, 'PHPBookmark login information', $mesg, $from))
return true;
else
return false;
}
}
?>
- url_fns.php
<?php
require_once('db_fns.php');
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] = addslashes($row[0]);
}
return $url_array;
}
function add_bm($new_url)
{
// Add new bookmark to the database
echo "Attempting to add ".htmlspecialchars($new_url).'<br />';
global $HTTP_SESSION_VARS;
$valid_user = $HTTP_SESSION_VARS['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;
}
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;
}
function recommend_urls($valid_user, $popularity = 1)
{
// We will provide semi intelligent recomendations to people
// If they have an URL in common with other users, they may like
// other URLs that these people like
if (!($conn = db_connect()))
return false;
// find other matching users
// with an url the same as you
if (!($result = mysql_query("
select distinct(b2.username)
from bookmark b1, bookmark b2
where b1.username='$valid_user'
and b1.username != b2.username
and b1.bm_URL = b2.bm_URL
")))
return false;
if (mysql_num_rows($result)==0)
return false;
// create set of users with urls in common
// for use in IN clause
$row = mysql_fetch_object($result);
$sim_users = "('".($row->username)."'";
while ($row = mysql_fetch_object($result))
{
$sim_users .= ", '".($row->username)."'";
}
$sim_users .= ')';
// create list of user urls
// to avoid replicating ones we already know about
if (!($result = mysql_query("
select bm_URL
from bookmark
where username='$valid_user'")))
return false;
// create set of user urls for use in IN clause
$row = mysql_fetch_object($result);
$user_urls = "('".($row->bm_URL)."'";
while ($row = mysql_fetch_object($result))
{
$user_urls .= ", '".($row->bm_URL)."'";
}
$user_urls .= ')';
// as a simple way of excluding people's private pages, and
// increasing the chance of recommending appealing URLs, we
// specify a minimum popularity level
// if $popularity = 1, then more than one person must have
// an URL before we will recomend it
// find out max number of possible URLs
if (!($result = mysql_query("
select bm_URL
from bookmark
where username in $sim_users
and bm_URL not in $user_urls
group by bm_URL
having count(bm_URL)>$popularity
")))
return false;
if (!($num_urls=mysql_num_rows($result)))
return false;
$urls = array();
// build an array of the relevant urls
for ($count=0; $row = mysql_fetch_object($result); $count++)
{
$urls[$count] = $row->bm_URL;
}
return $urls;
}
?>
Thanks a lot.And help me please.I really need help.