Web Development with PHP and Mysql

Development of the RemindMe Service

Previous | Next


User Management

Once I finish developing the site I'd like to let other people use it, such as my friends and family. This requires that I populate the bday_users table with data from the user registration form on the front page.

There are several other tasks that are also associated with each user, such as logging in, changing preferences, and changing password. In order to handle these tasks and manage the data associated with a user, I will use a class.

The data that I need my class to handle includes:

The functions that I'd like my class to perform are:

The class that I've designed is as follows:

class remindme_user {
	// name of database class to use for access
	var $m_database_class = "remindme_db";
	// table to use
	var $m_user_table = "bday_users";
	
	// relevant user data
	var $m_user_id;	
	var $m_user_email;
	var $m_user_password;
	
	// database connection instance
	var $m_db;
	
	function db_start() {
		$name = $this->m_database_class;
		if (!is_object($this->m_db))
	    	$this->m_db = new $name;
  	}

	// constructor
	function remindme_user($p_identifier = "") {
		$this->db_start();

		$this->m_user_id = 0;
		$this->m_user_email = "";
		$this->m_user_password = "";
		
		if ($p_identifier != "" and $p_identifier != "0") {
			$l_sql = "SELECT * FROM " . $this->m_user_table;
			if (is_numeric($p_identifier))
				$l_sql .= " WHERE user_id = " . $p_identifier;
			else
				$l_sql .= " WHERE user_email = '" . $p_identifier . "'";
			$this->m_db->query($l_sql);
			if ($this->m_db->next_record()) {
				$this->m_user_id = $this->m_db->f("user_id");
				$this->m_user_email = $this->m_db->f("user_email");
				$this->m_user_password = $this->m_db->f("user_password");
			}
		}
	}
	
	function isValidUser () {
		// this is 0 if the user isn't set, so will be false,
		//  otherwise it will be >0, and true
		return $this->m_user_id;
	}
	
	function verifyPassword ($p_password = "") {
		if (md5($p_password) == $this->m_user_password) {
			// return
			return TRUE;
		} else
			return FALSE;
	}
	
	function update() {	
		if ($this->m_user_id == 0)
			$l_sql = "INSERT INTO " . $this->m_user_table .
					" (user_email, user_password, created_date) VALUES ('" .
					$this->m_user_email . "', '" .
					$this->m_user_password . "', now())";
		else
			$l_sql = "UPDATE " . $this->m_user_table .
					" SET user_email = '" .	$this->m_user_email . "', " .
					"  user_password = '" . $this->m_user_password . "' " .
					" WHERE user_id = " . $this->m_user_id;
		$this->m_db->query($l_sql);
			
		if ($this->m_user_id == 0)
			$this->m_user_id = $this->m_db->last_id();
	}
	
	function setUserEmail ($p_user_email = "") {
		$this->m_user_email = $p_user_email;
	}
	
	function getUserEmail () {
		return $this->m_user_email;
	}

	function setUserPassword ($p_password = "") {
		$this->m_user_password = md5($p_password);
	}

	function getUserID () {
		return $this->m_user_id;
	}
		
}

Let me step through the interesting parts of the class with you. The first section sets up the class member variables.

The rest of the class is comprised of the functions necessary for manipulating the data within the class.

	function db_start() {
		$name = $this->m_database_class;
		if (!is_object($this->m_db))
	    	$this->m_db = new $name;
  	}

The db_start() function instantiates the database class specified in the $m_database_class variable, which is needed for any database access functions.

	// constructor
	function remindme_user($p_identifier = "") {
		$this->db_start();

		$this->m_user_id = 0;
		$this->m_user_email = "";
		$this->m_user_password = "";
		
		if ($p_identifier != "" and $p_identifier != "0") {
			$l_sql = "SELECT * FROM " . $this->m_user_table;
			if (is_numeric($p_identifier))
				$l_sql .= " WHERE user_id = " . $p_identifier;
			else
				$l_sql .= " WHERE user_email = '" . $p_identifier . "'";
			$this->m_db->query($l_sql);
			if ($this->m_db->next_record()) {
				$this->m_user_id = $this->m_db->f("user_id");
				$this->m_user_email = $this->m_db->f("user_email");
				$this->m_user_password = $this->m_db->f("user_password");
			}
		}
	}

The constructor for the class first initializes the database. It then assigns default values for the member variables describing the user.

Next, if a user identifier (either user_id or user_email) was passed to the class, the constructor attempts to find the matching user in the database. If a user is found, the member variables for the user are populated.

	function update() {	
		if ($this->m_user_id == 0)
			$l_sql = "INSERT INTO " . $this->m_user_table .
					" (user_email, user_password, created_date) VALUES ('" .
					$this->m_user_email . "', '" .
					$this->m_user_password . "', now())";
		else
			$l_sql = "UPDATE " . $this->m_user_table .
					" SET user_email = '" .	$this->m_user_email . "', " .
					"  user_password = '" . $this->m_user_password . "' " .
					" WHERE user_id = " . $this->m_user_id;
		$this->m_db->query($l_sql);
			
		if ($this->m_user_id == 0)
			$this->m_user_id = $this->m_db->last_id();
	}

The update function saves the information currently in the class to the database. By checking the $m_user_id variable, it determines whether or not it is a new user or an existing user so that it can build the necessary query.

After the query has been executed, the $m_user_id value is fetched if it is a new user.

	function setUserEmail ($p_user_email = "") {
		$this->m_user_email = $p_user_email;
	}
	
	function getUserEmail () {
		return $this->m_user_email;
	}

	function setUserPassword ($p_password = "") {
		$this->m_user_password = md5($p_password);
	}

	function getUserID () {
		return $this->m_user_id;
	}

The remaining four functions are used for public access to the member variables. While not strictly needed within PHP (which has no true concept of public/private variables), it is good programming practice to make use of them. The setUserPassword function demonstrates a good example of why, as it automatically md5 encrypts the password. If we were directly accessing the $m_user_password variable through code, we would have to remember to apply the md5() function each time we updated it. Using the public access functions does this for us and reduces the errors we'll have later on.


Previous | Next
  1. Introduction
  2. Research
  3. Data Modelling
  4. Database Access
  5. Website Design
  6. User Management
  7. Account Creation / Logging In
  8. Page Layout
  9. Reminder Events
  10. Sending Reminders
  11. Conclusion