Creating Social Networking site with CodeIgniter The registration

This is the tutorial of creating the registration form for our social networking site, so lets start coding.

The Database:

create a new database called mysocial because our site name gone to be this.

Next for the creation of registration we are need of only one database and that is “USER” database

the user database contains 11 fields here is a pic of that

user_structure

The Dependencies:

For the creation of the registration we are depend upon some of the core libraries and helper class of the CodeIgniter Framework and they are:

Libraries: database,session,form_validation

Helper: url,cookie

The Model functions:

public function getMonthString()
 {
 $months = array();
 $months[1] = "Jan";
 $months[2] = "Feb";
 $months[3] = "Mar";
 $months[4] = "Apr";
 $months[5] = "May";
 $months[6] = "Jun";
 $months[7] = "Jul";
 $months[8] = "Aug";
 $months[9] = "Sep";
 $months[10] = "Oct";
 $months[11] = "Nov";
 $months[12] = "Dec";

 return $months;
 }

public function getDays()
 {
 $days = array();
 for($i=1; $i<=31; $i++)
 {
 $days[] = $i;
 }
 return $days;
 }

public function getYears()
 {
 $years = array();
 for($i=date('Y',time()); $i>=1900; $i--)
 {
 $years[] = $i;
 }
 return $years;
 }

public function check_email($email)
 {
 $this->db->select('id')->from('user')->where("email = '$email'")->limit(1);
 $q = $this->db->get();
 if($q->num_rows() > 0)
 {
 return true;
 }
 else
 {
 return false;
 }
 }

private function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
 {
 $str = '';
 $count = strlen($charset);
 while ($length--) {
 $str .= $charset[mt_rand(0, $count-1)];
 }
 return $str;
 }

public function signup($fname,$email,$password,$sex,$month,$day,$year)
 {
 $key = $this->randString(20);
 $message = 'Welcome to the mysocial site click on the following link to activate your account';
 $message .= "<a href='".site_url('activate/'.$key)."'>".site_url('activate/'.$key)."</a>";
 if($this->db->insert('user',array('fname'=>$fname,'email'=>$email,'password'=>md5($password),'sex'=>$sex,'birthday'=>mktime(0,0,0,$month,$day,$year),'registered'=>time(),'key'=>$key)))
 {
 mail($email,'Registraton Confirmation',$message);
 return true;
 }
 else
 {
 return false;
 }
 }
 }

The getMonthString() function returns an array containing months  this function is used in registration for date of birth.

The getDays() function returns an array containing days from 1 to 31 this used for date of birth.

The getYears() function returns an array containing years from 1900-till year(2009) this is also used for date of birth.

next, the check_email() checks whether this email is already registered it takes email as its argument  if registered it returns true and if not it returns false this function is used in form validation process.

the randString() generates the confirmation link that is sent to user when he has finished the registration process it takes two argument the the first argument is the length of the confirmation link and next the characterset.

and at last the signup($fname,$email,$password,$sex,$month,$day,$year) register the user in our database

it takes seven parameters the fullname of the user,the email,the password,the sex,the month,the day and the year

if the user is registered successfully it send a confirmation links to the user’s email and returns true else it will return false.

Thats all the model functions needed for the registration process now lets move on to the controller functions

The Index Controller Functions:

First create a new controller named as “index” and add the following function to it.

Class Index extends Controller
{

 public function __Construct(){
 parent::Controller();
 }

 public function Index()
 {
 $this->common->index_login();
 $month = $this->common->getMonthString();
 $day = $this->common->getDays();
 $year = $this->common->getYears();
 $data = array('months'=>$month,'days'=>$day,'years'=>$year);
 $this->load->view('index_view',$data);
 }
 }

we first create a construct function an called the parent construct function next we created an Index function and pass the model functions to the view file.

NOTE: i’ve used the word common for the model file because my model file name is common.

The Index View:

the css:

*{margin: 0px;padding: 0px;}
.infoWrapper{margin-top:6px;clear:both;margin-bottom: 12px}
.infoTitle{float:left;width:90px;color:#808080}
.infoContent{padding-left:110px}
#footer{clear: both;}
#header{border: 1px solid silver;height:100px;background: #3B5998}
#l_pan{float: left;height:270px;width:500px;padding: 6px 3px;margin-left: 35px;}
.logo{float: left}
.login_div{float: right;margin-top: 25px;margin-right: 30px}
select{padding: 2px;}
body{background: #DEE3F0;font-family:"lucida grande",tahoma,verdana,arial,sans-serif;font-size:11px}
input[type="text"],input[type="password"]{padding:1px;}
label{color:#DEE3F0;font-weight: bold}
a{color:#DEE3F0;text-decoration: none;}
a:hover{text-decoration: underline;}
h1{color:#DEE3F0;margin-top:35px;margin-left:27px}

The HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
 <meta name="author" content="mubarak" />
 <title>mySocial</title>
</head>
<body>
<div id="wrapper">
 <div id="header">
 <div><h1>mySocial</h1></div>
 </div>
 <div id="l_pan">
 <div>
 <form action="<?=site_url('signup')?>" method="post">
 <div>
 <div>Full Name:</div>
 <div>
 <input type="text" size="35" name="reg_fname" id="reg_fname" />
 </div>
 </div>
 <div>
 <div>Email:</div>
 <div>
 <input type="text" size="35" name="reg_email" id="reg_email" />
 </div>
 </div>
 <div>
 <div>Password:</div>
 <div>
 <input type="password" size="35" name="reg_passwd" id="reg_passwd" />
 </div>
 </div>
 <div>
 <div>Sex:</div>
 <div>
 <select name="reg_sex">
 <option value="">Select Sex:</option>
 <option value="1">Male</option>
 <option value="2">Female</option>
 </select>
 </div>
 </div>
 <div>
 <div>Birthday:</div>
 <div>
 <select name="reg_month">
 <option value="">Month:</option>
 <?php
 foreach($months as $k=>$m)
 {
 echo '<option value="'.$k.'">'.$m.'</option>';
 }
 ?>
 </select>&nbsp;
 <select name="reg_day">
 <option value="">Day:</option>
 <?php
 foreach($days as $d)
 {
 echo '<option value="'.$d.'">'.$d.'</option>';
 }
 ?>
 </select>&nbsp;
 <select name="reg_year">
 <option value="">Year:</option>
 <?php
 foreach($years as $y)
 {
 echo '<option value="'.$y.'">'.$y.'</option>';
 }
 ?>
 </select>
 </div>
 <div>
 <div><input type="submit" name="signup" value="Sign Up" /></div>
 </div>
 </div>
 </form>
 </div>
 <div>
 <?echo validation_errors();?>
 </div>
 </div>
 <div id="r_pan">
 </div>
 <div id="footer">

 </div>
</div>

</body>
</html>

Thats all our index view is over now we have to make the form work note that i have  declared the form action to signup controller we haven’t created that yet, lets create!

The Signup Controller:

Class Signup extends Controller
{
 public function __Construct()
 {
 parent::Controller();
 }

 public function Index()
 {
 $rules = array(array('field'=>'reg_fname','label'=>'Full Name','rules'=>'required'),
 array('field'=>'reg_email','label'=>'Email','rules'=>'required|valid_email|callback_emailExists'),
 array('field'=>'reg_passwd','label'=>'Password','rules'=>'required|min_length[6]'),
 array('field'=>'reg_sex','label'=>'Sex','rules'=>'required'),
 array('field'=>'reg_month','label'=>'Month','rules'=>'required'),
 array('field'=>'reg_day','label'=>'Day','rules'=>'required'),
 array('field'=>'reg_year','label'=>'Year','rules'=>'required')
 );
 $this->form_validation->set_rules($rules);
 if($this->form_validation->run() == FALSE)
 {
 $month = $this->common->getMonthString();
 $day = $this->common->getDays();
 $year = $this->common->getYears();
 $data = array('months'=>$month,'days'=>$day,'years'=>$year);
 $this->load->view('index_view',$data);
 }
 else
 {
 $fname = $this->input->post('reg_fname');
 $email = $this->input->post('reg_email');
 $passwd = $this->input->post('reg_passwd');
 $sex = $this->input->post('reg_sex');
 $month = $this->input->post('reg_month');
 $day = $this->input->post('reg_day');
 $year = $this->input->post('reg_year');
 $this->common->signup($fname,$email,$passwd,$sex,$month,$day,$year);
 $this->load->view('registration_success',array('email'=>$email));
 }
 }
 public function emailExists($str)
 {
 if($this->common->check_email($str) == true)
 {
 $this->form_validation->set_message('emailExists', 'This '.$str.' email is already associated with another account, try loggin in or use another email');
 return FALSE;
 }
 else
 {
 return TRUE;
 }
 }
}

Explanation:

First we created the construct function and we called the parent construct function next we create an index function and there we validate our form inputs using form_validation library of CodeIgniter framework its so easy as you can see we are passing an two-dimensional array containing the field names of the form elements and their label names and the rules, to a function called set_rules next we call the function run() of form_validation this function does all the validation dirty jobs, and returns false if something invalid else it will return true, note that i’ve provided an callback function emailExists to the email rules  this function checks whether the email is already exists or not if exists it will return false by setting an error message else it will return true, next if everything is alright we pass the informations to the database and show the user that an email has been sent to confirm his registration we will create this part later.

One thought on “Creating Social Networking site with CodeIgniter The registration”

  1. This is good for beginners, but personally using form validation data which checks for the presence of post data is a much better habit to get into than creating functions with parameters (at least for sign-up and sign-in functionality).

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image