Not recognising user type

Hello,

I have little knowledge with PHP and I was assigned to try to fix some of the things that don’t work in a website. The website basically deals with two different users, a trader who can post articles and a blogger who can post blogs. When a user registers to become a trader for some reason he is saved as a blogger. Here is the registration code can you please tell me what it’s doing wrong ?

public function process()
{
$date = date(‘Y-m-d’);
$userid = uniqid();
$captchaError = ‘’;
if($this->input->post(‘Submit’))
{
//---------------------------------FORM VALIDATION STARTS HERE---------------------------------
$this->form_validation->set_error_delimiters(’’, ‘’);
$this->form_validation->set_rules(‘fname’, ‘Full name’,‘required’);
$this->form_validation->set_rules(‘email’, ‘Email’, ‘required|valid_email|is_unique[tbl_user.email]’);
$this->form_validation->set_rules(‘password’, ‘password’, ‘trim|required|min_length[6]|matches[cpassword]’);
$this->form_validation->set_rules(‘cpassword’, ‘Password confirmation’, ‘required’);
$this->form_validation->set_rules(‘mycheck[]’, ‘Buyer or Supplier’,‘required’);
$this->form_validation->set_rules(‘material[]’, ‘materials’,‘required’);
$this->form_validation->set_rules(‘company’, ‘Company name’, ‘required’);
$this->form_validation->set_rules(‘cname’, ‘Contact name’,‘required’);
$this->form_validation->set_rules(‘cemail’, ‘Contact email’, ‘required|valid_email’);
$this->form_validation->set_rules(‘nation’, ‘Country’, ‘required’);
$this->form_validation->set_rules(‘city’, ‘City’,‘required’);
$this->form_validation->set_rules(‘fax’);
$this->form_validation->set_rules(‘mobile’);
$this->form_validation->set_rules(‘phone’);
$this->form_validation->set_rules(‘website’);
$this->form_validation->set_rules(‘address’);
$this->form_validation->set_rules(‘zip’);
$this->form_validation->set_rules(‘content’, ‘Tell something about urself’, ‘required’);
$this->form_validation->set_rules(‘captchaText’, ‘captcha text’, ‘required’);
//-----------------------------------FORM VALIDATION ENDS HERE--------------------------------------

//------------------------------------CAPTCHA CHECK------------------------------------------
if($this->input->post(‘captchaText’))
{
$expiration = time()-7200; // Two hour limit
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

		// Then see if a captcha exists:
			$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
			$binds = array($_POST['captchaText'], $this->input->ip_address(), $expiration);
			$query = $this->db->query($sql, $binds);
			$row = $query->row();

			if ($row->count == 0)
			{
			$captchaError =  "You must submit the word that appears in the image";
			}
		}

//--------------------------------------CAPTCHA CHECK ENDS HERE----------------------------

//----------------------------------FORM VALIDATION RETURN ERRORS---------------------------
if ($this->form_validation->run() == FALSE || $captchaError!=’’)
{
$data[‘captcha’] = $this->getCaptcha();
$data[‘captchaError’] = $captchaError;
$data[‘pageTitle’]=‘Registration | Error’;
$this->load->view(‘register-trader’,$data);
}
//-----------------------------------------------END---------------------------------------

//---------------------------------------INSERT DATA INTO DATABASE-----------------------
else
{
if($this->input->post(‘material’))
{
$material = ‘’;
foreach($this->input->post(‘material’) as $value)
{
$material.= $value.’,’;
}
$material = rtrim($material,’,’);
}
$mycheck = $this->input->post(‘mycheck’);
$mycheckOne = ‘’;
$mycheckTwo = ‘’;
if(!empty($mycheck[0])){$mycheckOne = $mycheck[0];}
if(!empty($mycheck[1])){$mycheckTwo = $mycheck[1];}
$config[‘file_name’] = uniqid();
$config[‘upload_path’] = UP_PATH;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘max_size’] = ‘1000’;
$config[‘max_width’] = ‘1024’;
$config[‘max_height’] = ‘768’;
$this->load->library(‘upload’, $config);
if ( ! $this->upload->do_upload(‘userfile1’))
{
$error = $this->upload->display_errors();
$data = array(
‘supplier’=>$mycheckOne,
‘buyer’=>$mycheckTwo,
‘title’=>$this->input->post(‘company’),
‘cname’=>$this->input->post(‘cname’),
‘material’=>$material,
‘email’=>$this->input->post(‘email’),
‘phone’=>$this->input->post(‘phone’),
‘fax’=>$this->input->post(‘name’),
‘mobile’=>$this->input->post(‘mobile’),
‘web’=>$this->input->post(‘website’),
‘country’=>$this->input->post(‘nation’),
‘city’=>$this->input->post(‘city’),
‘address’=>$this->input->post(‘address’),
‘zip’=>$this->input->post(‘zip’),
‘content’=>$this->input->post(‘content’),
‘date’=>$date,
‘userid’=>$userid,
‘status’=>0
);

       	}   
		else
		{
			$data = array('upload_data' => $this->upload->data()); 
			$filepath = $data['upload_data']['file_name'];
			$config['image_library'] = 'gd2';
			$config['source_image'] = UP_PATH.$filepath;
			$config['new_image'] = UP_PATH.'thumbs/';
			$config['create_thumb'] = TRUE;
			$config['thumb_marker'] = '';
			$config['maintain_ratio'] = TRUE;
			$config['width'] = 75;
			$config['height'] = 50;
			$this->load->library('image_lib', $config);
			$this->image_lib->resize();
			
			$data = array(
              'supplier'=>$mycheckOne,
			  'buyer'=>$mycheckTwo,
			  'title'=>$this->input->post('company'),
              'cname'=>$this->input->post('cname'),
			  'material'=>$material,
			  'email'=>$this->input->post('email'),
			  'phone'=>$this->input->post('phone'),
			  'fax'=>$this->input->post('fax'),
			  'mobile'=>$this->input->post('mobile'),
              'web'=>$this->input->post('website'),
			  'country'=>$this->input->post('nation'),
			  'city'=>$this->input->post('city'),
			  'address'=>$this->input->post('address'),
			  'zip'=>$this->input->post('zip'),
              'content'=>$this->input->post('content'),
			  'image'=>$filepath,
			  'date'=>$date,
			  'userid'=>$userid,
              'status'=>0
			  );
			
		}	
    	$this->db->insert(TBL_CLA,$data);
    	
		$log_type = 'trader';
		$password = do_hash($this->input->post('password'));
		$dataOne = array(
              'password'=>$this->security->xss_clean($password),
			  'fname'=>$this->security->xss_clean($this->input->post('fname')),
              'email'=>$this->security->xss_clean($this->input->post('email')),
			  'log_type'=>$log_type,
			  'userid'=>$userid,
			  'status'=>0,
			  'date'=>$date,
			  'active'=>1
			  );
		$this->db->insert(TBL_USE,$dataOne);
		
		$this->session->set_userdata('fname', $this->input->post('fname'));
		redirect(base_url().'register/activate');
		}
		}
		if($this->input->post('Login'))
		{

//---------------------------------FORM VALIDATION STARTS HERE---------------------------------
$this->form_validation->set_error_delimiters(’’, ‘’);
$this->form_validation->set_rules(‘fname’, ‘Full name’,‘required’);
$this->form_validation->set_rules(‘email’, ‘Email’, ‘required|valid_email|is_unique[tbl_user.email]’);
$this->form_validation->set_rules(‘password’, ‘password’, ‘trim|required|min_length[6]|matches[cpassword]’);
$this->form_validation->set_rules(‘cpassword’, ‘Password confirmation’, ‘required’);
$this->form_validation->set_rules(‘captchaText’, ‘captcha text’, ‘required’);
//-----------------------------------FORM VALIDATION ENDS HERE--------------------------------------

//------------------------------------CAPTCHA CHECK------------------------------------------
if($this->input->post(‘captchaText’))
{
$expiration = time()-7200; // Two hour limit
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

		// Then see if a captcha exists:
			$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
			$binds = array($_POST['captchaText'], $this->input->ip_address(), $expiration);
			$query = $this->db->query($sql, $binds);
			$row = $query->row();

			if ($row->count == 0)
			{
			$captchaError =  "You must submit the word that appears in the image";
			}
		}

//--------------------------------------CAPTCHA CHECK ENDS HERE----------------------------

//----------------------------------FORM VALIDATION RETURN ERRORS---------------------------
if ($this->form_validation->run() == FALSE || $captchaError!=’’)
{
$data[‘captcha’] = $this->getCaptcha();
$data[‘captchaError’] = $captchaError;
$data[‘pageTitle’]=‘Registration | Error’;
$this->load->view(‘register-blogger’,$data);
}
//-----------------------------------------------END---------------------------------------

//---------------------------------------INSERT DATA INTO DATABASE-----------------------
else
{

		$date = date('Y-m-d');
		$log_type = 'blogger';
		$password = do_hash($this->input->post('password'));
		$dataOne = array(
              'password'=>$this->security->xss_clean($password),
			  'fname'=>$this->security->xss_clean($this->input->post('fname')),
              'email'=>$this->security->xss_clean($this->input->post('email')),
			  'log_type'=>$log_type,
			  'userid'=>$userid,
			  'status'=>0,
			  'date'=>$date,
			  'active'=>0
			  );
		$this->db->insert(TBL_USE,$dataOne);
		$data['link'] = 'http://www.arabrecycling.org/activate/created/'.$userid;
		$data['name'] = $this->input->post('fname');
		$message = $this->load->view('includes/activate',$data, TRUE);
		$subject = 'Account Activation';
		$fromTest = 'The Arab Recycling Initiative';
		$this->userRegEmail('[email protected]',$this->input->post('email'),$message,$subject,$fromTest);
		$this->session->set_userdata('fname', $this->input->post('fname'));
		redirect(base_url().'register/activate');
		}
		}
	
   
}

//-------------------------------------------------------CAPTCHA CREATION STARTS HERE------------------------
public function getCaptcha(){

		$this->load->library('common');
		$this->common = new common();
	
		$this->load->helper('captcha');
		$vals = array(
		'word' => $this->common->GetRandomCaptchaText(8),
		'img_path' => './captcha/',
	 	'img_url' => base_url().'captcha/',
		'font_path' => base_url().'system/fonts/Candice.ttf',
		'img_width' => '150',
		'img_height' => 30,
		'expiration' => 7200
		);

		$cap = create_captcha($vals);
	
		$data = array(
		'captcha_time' => $cap['time'],
		'ip_address' => $this->input->ip_address(),
		'word' => $cap['word']
		);

		$query = $this->db->insert_string('captcha', $data);
		$this->db->query($query);

		return $cap['image'];
}

//--------------------------------------------------------CAPTCHA CREATION ENDS HERE------------------------------------------------
//--------------------------------------------------------CONFIGURING EMAIL------------------------------------------------
public function userRegEmail($from,$to,$message,$subject,$fromTest){
$email_config[‘protocol’] = ‘mail’;
$email_config[‘mailtype’] = ‘html’;
$this->email->initialize($email_config);

		 $this->email->from($from, $fromTest);
		 $this->email->to($to);         
		 $this->email->subject($subject);
		 $this->email->message($message); 
		 $this->email->send();
}

//--------------------------------------------------------EMAIL CONFIGURATION ENDS HERE------------------------------------------------

}

Since you did not show the form that calls this function, I will assume that you have a radio-button that selects the user type. From your validation, I assume this is “Buyer” or “Supplier”. But, you have code:
‘supplier’=>$mycheckOne,
‘buyer’=>$mycheckTwo,
‘title’=>$this->input->post(‘company’),
which indicates you have a database field named ‘supplier’ and ‘buyer’???

Well, normally, if you have a radio-button that switches between two options, you just set the value of each to different values. Such as 1=Buyer, 2=Supplier. Then, you store this value, not caring which was selected as one entry into the database named something like buyer_supplier or owner_type or whatever. Then, whenever they log in, you load that value and do a simple compare to view different things depending on the number 1 or 2.

For some reason, you do this code before saving this value:
$mycheck = $this->input->post(‘mycheck’);
$mycheckOne = ‘’;
$mycheckTwo = ‘’;
if(!empty($mycheck[0])){$mycheckOne = $mycheck[0];}
if(!empty($mycheck[1])){$mycheckTwo = $mycheck[1];}
I am assuming this is because you need the extra variables for some use that I do not see.
Or, you are using checkboxes which means you could check both of them and cause an error.

So, first, are you using checkboxes or radiobuttons? Next is there a reason to store them in two different fields wasting space and having to check two fields each time you load data? Not really clear on your design here.

Maybe that helps or makes it worst. Ask away…

Sponsor our Newsletter | Privacy Policy | Terms of Service