Location: PHPKode > projects > The Referral Database > referralsdb/user_auth_fns.php
<?php

function login($username, $password)
{

/*********************************************
 *
 * Check if username/password pair exists:
 * value of 0 means database error
 * value of 1 means the user/password pair exists
 * value of 2 means the user/password pair does not exist
 * Make sure you handle error reporting when calling login
 * (See admin.php for an example)
 *
 *********************************************/

   // Connect to the database
   $conn = db_connect();
   if (!$conn)
   {
      return 0;
   }

   // Check to see if username/password pair exists
   $result = mysql_query("select * from Auth 
                         where username='$username'
                         and password = password('$password')");
                         
   // No result means database error
   if (!$result)
   {
      return 0;
   }

   // At least one row matches the username/password function
   if (mysql_num_rows($result) > 0)
   {
      return 1;
   }

   // No rows match
   else 
   {
      return 2;
   }
}

function check_admin_user()
{

/*********************************************
 *
 * Check if the user is logged in:
 * value of 0 means not logged in
 * value of 1 means  logged in
 * Handle handle these values when calling check_admin_user()
 *
 *********************************************/

   // If session is registered, they are logged in   
   if (isset($_SESSION['admin_user']))
   {
      return true;
   }

   // If no session is registered, they are not logged in
   else
   {
      return false;
   }
}

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())) {
      echo "<B>Error - Could not connect to the database.</B></BR>";
      return false;
    }

    $result = mysql_query( "update Auth 
                            set password = password('$new_password')
                            where username = '$username'");
    if (!$result) 
    {
      print "<B>Database error - could not change password</B></BR>";
      return false;  // not changed
    }
    else
    {
      return true;  // changed successfully
    }
  }
  else
  {
    echo "The old password that you entered was incorrect.<BR>";
    return false; // old password was wrong
  }
}

function insert_category($catname)
{
/*********************************************
 *
 * inserts a new category into the database:
 * value of 0 means database error
 * value of 1 means sucessful insert
 * value of 2 means category already exists
 *
 *********************************************/
   $conn = db_connect();
   if (!$conn)
   {
      // Database error
      return 0;
   }

   // Query database to see if category already exists
   $query = "select *
             from Categories
             where Cat_Name='$catname'";
   $result = mysql_query($query);
   
   if (!$result)
   {
      // Database error
      return 0;
   }

   if (mysql_num_rows($result) > 0)
   {
      // Category already exists - 
      // At least one row matches the category name
      // i.e. don't add the category
      return 2;
   }

   else
   {
      // Category doesn't exist -
      // Go ahead and add the category

      $query = "insert into Categories values
               ('', '$catname')"; 
      $result = mysql_query($query);
      if (!$result)
      {
         // Database error
         return 0;
      }
      else
      {
         // Successful insert
         return 1;
      }
   }
}

?>
Return current item: The Referral Database