Shopping cart v1.1 Part 16: Registration page and customer log in page


Our customers uses this registration page to keep their information in our database. Once registered, this information will be displayed in a checkout page for a speedy process. This post covers the customer registration function, login and logout function.

Download Page


All the fields are required and rules are set in function registration.

function registration

This function sets the recaptcha as we did in the previous post, then sets rules and fields. If the validation is false outputs errors and load the same page.
Otherwise check if the customer’s email is in our database by using MCustomers’s checkCustomer function. If it returns TRUE, then set a flash message and redirect to login page.
If it returns FALSE, then add the customer to our database and set a success message and redirect to login page.

Be aware that due to $rules['city'] = ‘trim|required|alpha_dash’; a customer has to enter ‘NewYork’, ‘New-York’ or ‘New_York’ instead of ‘New York. If you don’t like this rule, you need to take out ‘alpha_dash’

function registration(){
	/* If you are using recaptcha, don't forget to configure modules/recaptcha/config/recaptcha.php
	 * Add your own key
	 * */
		$captcha_result = '';
		$data['cap_img'] = $this->_generate_captcha();

	if ($this->input->post('email')){

	 	$data['title'] = lang('webshop_shop_name')." | "."Registration";

		// set rules
		$rules['email'] = 'trim|required|matches[emailconf]|valid_email';
		$rules['emailconf'] = 'trim|required|valid_email';
		$rules['password'] = 'trim|required';
		$rules['customer_first_name'] = 'trim|required|min_length[3]|max_length[20]';
		$rules['customer_last_name'] = 'trim|required|min_length[3]|max_length[20]';
		$rules['phone_number'] = 'trim|required|min_length[8]|max_length[12]|numeric';
		$rules['address'] = 'trim|required';
		$rules['city'] = 'trim|required|alpha';
		$rules['post_code'] = 'trim|required|numeric';
		// if you want to use recaptcha, set modules/recaptcha/config and uncomment the following
		$rules['recaptcha_response_field'] = 'trim|required|valid_captcha';

		$this->validation->set_rules($rules);

		// set fields. This will be used for error messages
		// for example instead of customer_first_name, First Name will be used in errors
		$fields['email']	= lang('webshop_email');
		$fields['emailconf']	= lang('webshop_email_confirm');
		$fields['password']	= lang('webshop_pass_word');
		$fields['customer_first_name']	= lang('webshop_first_name');
		$fields['customer_last_name']	= lang('webshop_last_name');
		$fields['phone_number']	= lang('webshop_mobile_tel');
		$fields['address']	= lang('webshop_shipping_address');
		$fields['city']	= lang('webshop_city');
		$fields['post_code']	= lang('webshop_post_code');
		$fields['recaptcha_response_field']	= 'Recaptcha';

		$this->validation->set_fields($fields);

		// run validation
		if ($this->validation->run() == FALSE)
			{
				// if false outputs errors
				$this->validation->output_errors();
				// and take them to registration page to show errors
				$data['page'] = $this->config->item('backendpro_template_shop') . 'registration';
				$data['module'] = lang('webshop_folder');
				$this->load->view($this->_container,$data);
			}
			else
			{
				$e = $this->input->post('email');
				// otherwise check if the customer's email is in the database
				$numrow = $this->MCustomers->checkCustomer($e);
				if ($numrow == TRUE){
					// you have registered before, set the message and redirect to login page.
					flashMsg('info', lang('webshop_registed_before'));
					// $this->session->set_flashdata('msg', lang('webshop_registed_before'));
					redirect( lang('webshop_folder').'/login','refresh');
				}
			// a customer is new, so create the new customer, set message and redirect to login page.
			$this->MCustomers->addCustomer();
			flashMsg('success', lang('webshop_thank_registration'));
			// $this->session->set_flashdata('msg', lang('webshop_thank_registration'));
			redirect( lang('webshop_folder').'/login');
			}
	}// end of if($this->input->post('email'))

	$data['title'] = lang('webshop_shop_name')." | ". "Registration";
	$data['page'] = $this->config->item('backendpro_template_shop') . 'registration';
	$data['module'] = lang('webshop_folder');
	$this->load->view($this->_container,$data);

  }

view registration

module/webshop/views/shop/registration.php

 	<div class="gallerimain">
    	<h1><?php echo $title; ?></h1><br />
	<?php print displayStatus();?>
		<h2><?php echo $this->lang->line('webshop_regist_plz_here'); ?></h2><br />
		<h2><?php echo sprintf( $this->lang->line('genral_login_msg'), anchor( $this->lang->line('webshop_folder').'/login', $this->lang->line('genral_login') ) );?></h2>
		<br />

		<?php
		if ($this->session->flashdata('msg')|| $this->session->flashdata('error')){
			echo "<div class='status_box'>";
			echo $this->session->flashdata('msg');
			echo $this->session->flashdata('error');
			echo "</div>";
		}
		?>
		<?php echo validation_errors('<div class="message error">','</div>'); ?>

		<?php echo form_open($this->lang->line('webshop_folder')."/registration",array('class' => 'expose')); ?>

		<h3>*<?php echo $this->lang->line('webshop_email'); ?></h3>
		<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="40" />

		<h3>*<?php echo $this->lang->line('webshop_email_confirm'); ?></h3>
		<input type="text" name="emailconf" value="<?php echo set_value('emailconf'); ?>" size="40" />

		<h3>*<?php echo $this->lang->line('webshop_pass_word'); ?></h3>
		<input type="password" name="password" value="" size="20" />

		<h3>*<?php echo $this->lang->line('webshop_first_name'); ?></h3>
		<input type="text" name="customer_first_name" value="<?php echo set_value('customer_first_name'); ?>" size="30" />

		<h3>*<?php echo $this->lang->line('webshop_last_name'); ?></h3>
		<input type="text" name="customer_last_name" value="<?php echo set_value('customer_last_name'); ?>" size="30" />

		<h3>*<?php echo $this->lang->line('webshop_mobile_tel'); ?></h3>
		<input type="text" name="phone_number" value="<?php echo set_value('phone_number'); ?>" size="15" />

		<h3>*<?php echo $this->lang->line('webshop_shipping_address'); ?></h3>
		<input type="text" name="address" value="<?php echo set_value('address'); ?>" size="50" />

		<h3>*<?php echo $this->lang->line('webshop_post_code'); ?></h3>
		<input type="text" name="post_code" value="<?php echo set_value('post_code'); ?>" size="8" />

		<h3>*<?php echo $this->lang->line('webshop_city'); ?></h3>
		<input type="text" name="city" value="<?php echo set_value('city'); ?>" size="20" />

		<h3>*Are you human?</h3><?php echo "<p>$cap_img</p>" ;?>

		<br />
		<input type="submit" name="submit" value="<?php echo $this->lang->line('webshop_register'); ?>" />

		<?php echo form_close(); ?>

</div>

login function


A cutomer will be redirected to a login page.
If email is filled, verify the customer by using MCustomers->verifyCustomer function.
This verifyCustomer function as you see below, check if the customer exist in our database. If data exists, set the PHP session, customer_id, first name, last name and other details which the customer entered in the registration page previously.

Then in the controller, it checks if the PHP session ‘customer_id’ exists. If so, set a flash message and redirect and display it. If not, set an error message and redirect.

modules/customers/models/mcustomers.php

function verifyCustomer($e,$pw){
		$this->db->where('email',db_clean($e,50));
		$this->db->where('password', db_clean(dohash($pw),16));
		$this->db->limit(1);
		$Q = $this->db->get('omc_customer');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			$_SESSION['customer_id'] = $row['customer_id'];
			$_SESSION['customer_first_name'] = $row['customer_first_name'];
			$_SESSION['customer_last_name'] = $row['customer_last_name'];
			$_SESSION['phone_number'] = $row['phone_number'];
			$_SESSION['email'] = $row['email'];
			$_SESSION['address'] = $row['address'];
			$_SESSION['city'] = $row['city'];
			$_SESSION['post_code'] = $row['post_code'];
		}else{
			// $_SESSION['customer_id'] = 0; // this will eliminate error
		}
	}

webshop/controllers/webshop.php

function login(){
		if ($this->input->post('email')){
			$e = $this->input->post('email');
			$pw = $this->input->post('password');
			$this->MCustomers->verifyCustomer($e,$pw);
			if (isset($_SESSION['customer_id'])){
				flashMsg('info',lang('login_logged_in'));
				redirect( lang('webshop_folder').'/login','refresh');
			}
			flashMsg('info',lang('login_email_pw_incorrect'));
			redirect( lang('webshop_folder').'/login','refresh');
		}
		$data['title'] = lang('webshop_shop_name')." | "."Customer Login";
		$data['page'] = $this->config->item('backendpro_template_shop') . 'customerlogin';
		$data['module'] = lang('webshop_folder');
		$this->load->view($this->_container,$data);
  }

cutomerlogin.php

   <div class="gallerimain">
      <h1><?php echo $title;?></h1>
      <br />
<?php
	if($this->data['customer_status']==1){
		echo "<h2>".lang('customer_login_enjoy_shopping')."</h2>";
		echo "<br /><h2>".anchor( lang('webshop_folder').'/checkout', lang('general_check_out') )."</h2><br />" ;

	}else{
		echo "<h2>".lang('customer_login_plz_login')."</h2><br />";
	}
?>

<?php
	if ($this->session->flashdata('msg')){
		echo "<div class='status_box'>";
		echo $this->session->flashdata('msg');
		echo "</div>";
	}
?>

<?php
	$udata = array('name'=>'email','id'=>'email','size'=>30);
	$pdata = array('name'=>'password','id'=>'password','size'=>16);

	echo form_open($this->lang->line('webshop_folder')."/login");
	echo "<p><label for='email'>". lang('orders_email'). "</label><br/>";
	echo form_input($udata) . "</p>";
	echo "<p><label for='password'>Password</label><br/>";
	echo form_password($pdata) . "</p>";
	echo form_submit('submit','login');
	echo form_close();
?>
</div>

We need to add lang(‘customer_login_enjoy_shopping’) etc to modules/webshop/language/english/webshop_lang.php

// modules/webshop/views/customerlogin.php
$lang['customer_login_enjoy_shopping'] = 'Enjoy your shopping!';
$lang['customer_login_plz_login'] = 'Please login. This will fill up your details at check out automatically.';

// you can add more here.

When a customer is logged in, a message is displayed and login status and ‘Log out’ link are displayed on the top-right column.

If you see the Shop_controller.php, you will see the code which controlling this function.
system/application/libraries/Shop_controller.php

// This part is used in all the pages so load it here
		// For customer login status
		if(isset($_SESSION['customer_first_name'])){
			$this->data['customer_status']=1;
			$this->data['loginstatus']=lang('general_hello').$_SESSION['customer_first_name'].". ".lang('general_logged_in')."<br />
			<a href=\"index.php/".$this->lang->line('webshop_folder')."/logout \">Log out</a>";
		}else{
			$this->data['customer_status']=0;
			$this->data['loginstatus']="You are not logged in. <a href=\"index.php/".$this->lang->line('webshop_folder')."/login \">".lang('general_login')."</a>
			<br /><a href=\"index.php/".$this->lang->line('webshop_folder')."/registration \">".lang('general_register')."</a>";
		}

logout function

function logout(){
		// this would remove all the variable in the session
		session_unset();

		//destroy the session
		session_destroy(); 

		redirect( lang('webshop_folder').'/index','refresh');
	 }

When a customer is logged out, the customer will be redirected to the index page.

1 comment to Shopping cart v1.1 Part 16: Registration page and customer log in page

  • Lukman Hakim

    I cant login and when I try to register. Image of recaptcha doesnt show.
    Any setting to do

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>