Jump to content

[SOLVED] Need help with function


coldfiretech

Recommended Posts

Okay i have a list of functions in one file called functions.php

from page1.php i am calling the functions to validate some input from the user before moving on.. Im getting hung up on the cell phone field.. login and email work good..  hopefully someone will understand what ive got going on here! 

 

im stuck... any suggestions would be great  ;D

 

functions.php

<?php
$db_user = "matt";
$db_pass = "password";
$db_db = "mydb";
$db_host = "localhost";
$con = mysql_connect($db_host, $db_user, $db_pass) or die('MySQl Connection Error:'.mysql_error());

function check_username($usernm)
{
global $db_db;
global $con;
mysql_select_db($db_db, $con) or die('MySQL Error: Cannot select table');
$q = "SELECT `login` from users WHERE login = '$usernm'";
$r = mysql_query($q, $con) or die(mysql_error()."<br /><br />".$q);
if(mysql_num_rows($r) >0){
//username already exists
Return 0;
}else{
//username doesnt exist
Return 1;
}
mysql_close($con);
}


function check_email ($email)
{
global $db_db;
global $con;
mysql_select_db($db_db, $con) or die('MySQL Error: Cannot select table');
$q = "SELECT `email` from users WHERE email = '$email'";
$r = mysql_query($q, $con) or die(mysql_error()."<br /><br />".$q);
if(mysql_num_rows($r) >0){
//email already exists
Return 0;
}else{
//email doesnt exist
Return 1;
}
mysql_close($con);
}

function check_cellphone($phone, $phone2)
{
global $db_db;
global $con;
mysql_select_db($db_db, $con) or die('MySQL Error: Cannot select table');
$q = "SELECT * from contact WHERE '$phone' or '$phone2' = ci_cell_phone";
$r = mysql_query($q, $con) or die(mysql_error()."<br /><br />".$q);
if(mysql_num_rows($r) >0){
//cell phone entered already exists
Return 0;
}else{
//cell phone entered is okay
Return 1;
}
mysql_close($con);
}

function check_homephone($phone, $phone2)
{
global $db_db;
global $con;
mysql_select_db($db_db, $con) or die('MySQL Error: Cannot select table');
$q = "SELECT * from contact WHERE '$phone' or '$phone2' = ci_home_phone";
$r = mysql_query($q, $con) or die(mysql_error()."<br /><br />".$q);
if(mysql_num_rows($r) >0){
//home phone entered already exists
Return 0;
}else{
//home phone entered is okay
Return 1;
}
}

?>

 

and here is page1.php

 

<?php
include "test1.php";
$result = check_username("whambam");
$cellphone = "2532248006";
$homephone = "2532248007";
if ($result > 0) {
//username is good to use
//move to check email
$result2 = check_email("flite67@yahoo.com");
if ($result2 > 0) {
	$result3 = check_cellphone($cellphone, $homephone);
		if (result3 > 0){
		//cell phone is good / check home phone
			$result4 = check_homephone($cellphone, $homephone);
			if ($result4 > 0)
			{
			echo "everything is good to go";  //INSERT DATA INTO DATABASE
					} else {
			//home phone is no good
			echo "sorry but the home phone number you entered is already in our database ";
					}
			} else {
		echo "sorry but the cell phone number you entered is already in our database";
	}
} else {
echo "bad email";
}
} else {
echo "bad username";
//username is already in use
}
?>

Link to comment
Share on other sites

http://www.phpfreaks.com/tutorials

 

 

There are some basic OOP tutorials there

 

here is my basic OOP register class  (note input_clean is a function that strips tags and adds mysql_real_escape_string to the input custom function)

<?php
#class used to process a user registration
class register_process{
#variable decleration
var $submit = 0;
var $sent = 0;
var $email = "";
var $email2 = "";
var $password = "";
var $password2 = "";
var $securityq = "";
var $securitya = "";
var $true_captcha = "";
var $enter_captcha = "";
var $errors = array();
var $messages = array();
var $send_string = "";
var $userid = "";

#sets object's variable from defined POST inputs on the form.
function define_inputs(){
	$this->email = input_clean($_POST['email']);
	$this->email2 = input_clean($_POST['email2']);
	$this->password = input_clean($_POST['password']);
	$this->password2 = input_clean($_POST['password2']);
	$this->securityq = input_clean($_POST['secruity_question']);
	$this->securitya = input_clean($_POST['security_answer']);
	$this->true_captcha = $_SESSION['captcha'];
	$this->enter_captcha = input_clean($_POST['captcha']);
}	 
#errors reporting during class execution runs are ran using the $this->error_report()  command
function error_report($error,$type){
	#Type 1 is an error message meanign "bad" 
	if($type === 1){
		$this->errors[] = $error;
	}
	#non type 1 errors are messages such as a successful update or change in data.
	else{
		$this->messages[] = $error;
	}
}

#checks email various ways to verify its a proper email on multiple levels
function check_email($email,$email2){
	#verify that mail1 = mail2 so the user input is true to what they wished to typed
	if($email != $email2){
		$this->error_report("The two emails provided did not match.",1);
	}
	#regex verification that the user supplied an address simlar to user@test.com 
	$pattern = "^.+@[^\.].*\.[a-z]{2,}$";
	if (!eregi($pattern, $email)){
		$this->error_report("The email supplied did not match the criteria for a valid email address (example user@gmail.com)",1);
	}
	#verification user's entered addres is not already in the database as the email address is the user's login username
	$q = "Select Email from `".USERS_TABLE."` Where Email = '".$email."'";
	$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
	if(mysql_num_rows($r) >0){
		$this->error_report("The email provided is already registered.",1);
	}
}
#checks password for match, length and alphanumeric
function check_password($password,$password2){
	#verification user input is true to their intentions
	if($password != $password2){
		$this->error_report("The two passwords provided did not match.",1);
	}
	#verification password is of the desired minimal 6 character length
	if(strlen($password) <5){
		$this->error_report("The password must be at least 5 characters long.",1);
	}
	#regex verification password is alphanumeric only eregi is requried for non case specific pattern as given
	$pattern = "[A-Z0-9]";
	if(!eregi($pattern,$password)){
		$this->error_report("The password can only contain alphanumeric characters.",1);
	}
}
#checks that a valid security question is provided and an answer is given
function check_security($q,$a){
	#verify aganist cURL injection of an invalid security question from table
	$q = "Select QuestionID from `".SQ_TABLE."` Where QuestionID = '".$q."'";
	$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
	if(mysql_num_rows($r) == 0){
		$this->error_report("Please select a valid security question.",1);
	}
	#Verify an answer of at least 4 characters is given so it isn't left blank.
	if(strlen($a) < 3){
		$this->error_report("Please provide a valid security answer.",1);
	}
}
#checks captcha entered for consistency
function check_captcha($true,$enter){
	#verify user cpatcha matchs visual image a cURL and CPU injection prevention technique
	if($true != $enter){
		$this->error_report("The captcha code entered did not match.",1);
	}
}
#returns count number of errors of it is greatre than 0 form will not process
function errors_return(){
	return count($this->errors);
}
#displays errors as a list in the document
function errors_display(){
	echo "<ul class=\"errors\">\n";
		foreach($this->errors as $value){
			echo "<li>".$value."</li>\n";
		}
	echo "</ul>\n";
}
#displays messages as a list in the document
function messages_display(){
	echo "<ul class=\"messages\">\n";
		foreach($this->messages as $value){
			echo "<li>".$value."</li>\n";
		}
	echo "</ul>\n";
}
#md5 1-way encryption of a password for db entry.
function crypt_pass($pass){
	return md5($pass);
}
#used to set the user's activation string length is set to 24 characters and the function gen_string from the function's library is used (custom function)
function set_string(){
	$this->send_string = gen_string(24);
}
#processes form
function send_form(){
	#sets string
	$this->set_string();
	#insert query
	$q = "Insert into `".USERS_TABLE."`
		(Email, Password, Date_Joined, Security_ID, Security_Answer,Active,Active_Code)
		VALUES(
			'".$this->email."', '".$this->crypt_pass($this->password)."', NOW(), '".$this->securityq."', '".$this->securitya."', '0', '".$this->send_string."'
		)";
	$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
	$this->sent = 1;
	#set's user id in the object
	$this->userid = mysql_insert_id();
}
#sends user the email required to activate their accounts headers are tested and have worked successfully. 
function send_mail(){
	$subject = "New Account Creation at ".EMAIL_DOMAIN;
	$headers = "From: ".EMAIL_DOMAIN." <info@".EMAIL_DOMAIN.">\r\n";
	$headers .= "Reply-To: info@".EMALI_DOMAIN."\r\n";
	$headers .= "Return-Path: info@".EMAIL_DOMAIN."\r\n";
	$headers .= "BCC: info@".EMAIL_DOMAIN."\r\n";
	$headers .= "X-Mailer: PHP/" . phpversion()."\r\n";
#activation email message.  Must be tabbed left since it is a text message and not an html document.
$message = "
Welcome to ".EMAIL_DOMAIN."\n
Your Account is currently NOT activated you can do so by going to
http://".EMAIL_DOMAIN."/url/activate.php?id=".$this->userid."_".$this->send_string."\n
Your username is this email address (".$this->email.")\n
Your password is: ".$this->password."\n
If you are having issues please contact the server administrators at info@".EMAIL_DOMAIN.".\n
Thanks,
".EMAIL_DOMAIN."\n\n
This is a auto-generated message please do not respond to it.";
	#verification mailer function works
	if(mail($this->email,$subject,$message,$headers)){
		$this->error_report("You have been sent an email with information on activating your account.",2);
	}
	else{
		$this->error_report("There was an issue sending your email",1);
	}
}
}
#####END OF REGISTER CLASS
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.