Location: PHPKode > projects > BSW_Bulletin > functions.php

function connectToDB() {
	global $mysql, $dbhost, $dbuser, $dbpass, $dbname,$mysql,$mysqli;

	($mysql = mysql_pconnect("$dbhost", "$dbuser", "$dbpass")) || die("Couldn't connect to MySQL");

	// select db:
	mysql_select_db("$dbname", $mysql) || die("Couldn't open db: $dbname. Error if any was: ".mysql_error() );
} // end func dbConnect();

function newUser($f_name,$l_name,$email,$username, $password) {
	Creating a New User Record in the DB:
	In this function we create a new user record in the db.

	We first build a query and save it into the $query variable.
	The query statement says:

	'Insert the value of $username and $password into the 'username'
	and 'password' columns in the 'users' table'

	global $mysql;

	$query1="INSERT INTO auth_users (f_name,l_name,email,username, password) VALUES('$f_name','$l_name','$email','$username', '$password')";
	//$query2="INSERT INTO master_name (f_name,l_name,email,username, password) VALUES('$f_name','$l_name','$email','$username', '$password')";
	$result1=mysql_query($query1,$mysql) or die("Died inserting username info into db.  Error returned if any: ".mysql_error());
//$result2=mysql_query($query2,$mysql) or die("Died inserting username info into db.  Error returned if any: ".mysql_error());

	return true;
} // end func newUser($username, $pass)

function displayErrors($messages) {
	Error Handling functions:
	An error handling function is useful to have in any project.

	This particular function takes an array of messages, and
	for each message displays it in a list using HTML
	<ul><li></li></ul> tags.
	print("<b>There were problems with the previous action.  Following is a list of the error messages generated:</b>\n<ul>\n");

	foreach($messages as $msg){
} // end func displayErrors($messages)

function checkLoggedIn($status){
	global $status;
		// if yes, check user is logged in:
		// ie for actions where, yes, user must be logged in(!)
		case "yes":
				header("Location: addtopic.php");

		// if no, check NOT logged in:
		// ie for actions where user can't already be logged in
		// (ie for joining up or logging in)
		case "no":
				The '===' operator differs slightly from the '=='
				equality operator.

				$a === $b if and only if $a is equal to $b AND
				$a is the same variable type as $b.

				for example, if:

				$a="2";	<-- $a is a string here
				$b=2;	<-- $b is an integer here

				then this test returns false:

				whereas this test returns true:
			if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){

                        header("Location: login.php");
	// if got here, all ok, return true:
	return true;
} // end func checkLoggedIn($status)

function checkPass($username, $password) {
	Password checking function:
	This is a simple function that takes the $username name and
	$password that a user submits in a form and checks that a
	row exists in the database where:

	the value of the 'username' column is the same as the value in $username
	the value of the 'password' column is the same as the value in $password

	If exactly one row is returned, then that row of data is returned.
	If no row is found, the function returns 'false'.
	global $mysql;

	$query="SELECT username, password status FROM auth_users WHERE username='$username' and password='$password'";
	$result=mysql_query($query) or die("checkPass fatal error: ".mysql_error());

	// Check exactly one row is found:
	if(mysql_num_rows($result)==1) {
		return $row;
	//Bad username:
	return false;
} // end func checkPass($username, $password)

function cleanMemberSession($username, $password) {
	Member session initialization function:
	This function initializes 3 session variables:
  $username, $password and $loggedIn.

	$username and $password are used on member pages (where you
	could allow the user to change their password for example).

	$loggedIn is a simple boolean variable which indicates
	whether or not the user is currently logged in.
$sql = mysql_query("update auth_users set status='yes' where username='$username'")or die(mysql_error());
setcookie("user", $username, $expire);

} // end func cleanMemberSession($username, $pass)

function flushMemberSession() {
	Member session destruction function:
	This function unsets all the session variables initialized
	above and then destroys the current session.
	// use unset to destroy the session variables
global $username,$password,$mysql;

$StringData = "\n";
fwrite($handling, $StringData); 

	// and use session_destroy to destroy all data associated
	// with current session:

	return true;
} // send func flushMemberSession()

function doCSS() {
	CSS Output:
	This function simply outputs some cascading style sheet
	data for markup by the user's browser.
<style type="text/css">
body{font-family: Arial, Helvetica; font-size: 10pt}
h1{font-size: 12pt}
} // end func doCSS()

# function validates HTML form field data passed to it:
function field_validator($field_descr, $field_data,
  $field_type, $min_length="", $max_length="",
  $field_required=1) {
	Field validator:
	This is a handy function for validating the data passed to
	us from a user's <form> fields.

	Using this function we can check a certain type of data was
	passed to us (email, digit, number, etc) and that the data
	was of a certain length.
	# array for storing error messages
	global $messages;

	# first, if no data and field is not required, just return now:
	if(!$field_data && !$field_required){ return; }

	# initialize a flag variable - used to flag whether data is valid or not

	# this is the regexp for email validation:

	# a hash array of "types of data" pointing to "regexps" used to validate the data:
		"alpha_space"=>"^[a-zA-Z ]+$",
		"alphanumeric_space"=>"^[a-zA-Z0-9 ]+$",

	# check for required fields
	if ($field_required && empty($field_data)) {
		$messages[] = "$field_descr is a required field.";

	# if field type is a string, no need to check regexp:
	if ($field_type == "string") {
		$field_ok = true;
	} else {
		# Check the field data against the regexp pattern:
		$field_ok = ereg($data_types[$field_type], $field_data);

	# if field data is bad, add message:
	if (!$field_ok) {
		$messages[] = "Please enter a valid $field_descr.";

	# field data min length checking:
	if ($field_ok && ($min_length > 0)) {
		if (strlen($field_data) < $min_length) {
			$messages[] = "$field_descr is invalid, it should be at least $min_length character(s).";

	# field data max length checking:
	if ($field_ok && ($max_length > 0)) {
		if (strlen($field_data) > $max_length) {
			$messages[] = "$field_descr is invalid, it should be less than $max_length characters.";
function confirm($dbpass){
global $pass;

Return current item: BSW_Bulletin