Codeigniter registration

0
23

<!– Start: MAIN CONTENT –>


<?php $this->load->view('segments/top'); ?>
<!-- Start: MAIN CONTENT -->
<div class="content">
<!-- Start: slider -->
<div class="container">
<div class="page-header">
<h1 style="color:#22a5f0">Sign Up</h1>
</div>
<div class="row">
<div class="span6 offset3">
<h4 class="widget-header">Before continuing we need you to <a href="<?php echo base_url("user/login");?>" style="color:#FFF">login</a> or sign up: </h4>
<div class="widget-body">
<div class="center-align">
<form class="form-horizontal form-signin-signup" method="post" action="<?php echo base_url("user/signup"); ?>">
<input type="text" name="first_name" placeholder="First Name" value="<?=$this->input->post('first_name')?>">
<input type="text" name="last_name" placeholder="Last Name" value="<?=$this->input->post('last_name')?>">
<input type="text" name="username" placeholder="User Name" value="<?=$this->input->post('username')?>">
<input type="text" name="email" placeholder="Email" value="<?=$this->input->post('email')?>">
<input type="password" name="password" placeholder="Password">
<input type="password" name="confirm_password" placeholder="Confirm Password">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%"> </td>
<td width="80%" align="left">
<input name="sms_alert" type="checkbox" style="margin:10px 0px 15px 0px; padding:0px;" <? if($this->input->post('sms_alert')){ echo "CHECKED";}?> />&nbsp;&nbsp;Send me SMS bid alerts
</td>
</tr>
</table>


<!--<select style="height: 37px; margin-bottom: 13px; width:73%;"><option>City</option></select>-->
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td width="15%"> </td>
<td width="80%" align="left">
I want to : &nbsp;&nbsp;
<input type="radio" id="work" name="user_type" value="freelancer" style="margin-bottom:7px" <? if($this->input->post('user_type')=='freelancer'){ echo "CHECKED";}?> /> Work &nbsp;
<input type="radio" id="hire" name="user_type" value="business" style="margin-bottom:7px" <? if($this->input->post('user_type')=='business'){ echo "CHECKED";}?> /> Hire
</td>
</tr>
</table>
<select name="freelancer_type" id="freelancer_type" style="height: 37px; margin-bottom: 13px; width:73%;">
<option value="">Select User Type</option>
<option value="individual" <? if($this->input->post('freelancer_type')=='individual'){ echo "SELECTED";}?>>Individual</option>
<option value="company" <? if($this->input->post('freelancer_type')=='company'){ echo "SELECTED";}?>>Company</option>
</select>

<div class="remember-me">

<div class="pull-right" style="font-size:11px; text-align:left;"> By signing up you confirm to agree to our <a href="<?php echo base_url('page/terms'); ?>" target="_blank" style="font-size:11px; text-align:left;">Terms and Conditions</a> and <a href="<?php echo base_url('page/privacy'); ?>" target="_blank" style="font-size:11px; text-align:left;">Privacy Policy.</a> </div>
<div class="clearfix"></div>
</div>
<input type="submit" value="Sign up" class="btn-primary btn-large">
</form>
<h4><i class="icon-thumbs-up"></i> Alternatively, login using your Facebook account:</h4>
<ul class="signin-with-list">

<li>
<a class="btn-facebook">
<i class="icon-facebook icon-large" style="margin-top:5px;"></i>
Login with Facebook
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>

<!-- End: PRODUCT LIST -->
</div>
<!-- End: MAIN CONTENT -->
<div style="height:30px;"></div>

<?php $this->load->view('segments/bottom'); ?>

<script>
$(function(){
if($('#work').is(':checked')){
$('#freelancer_type').attr('disabled',false);
$('#freelancer_type').show();
} else {
$('#freelancer_type').attr('disabled',true);
$('#freelancer_type').hide();
}

$('#work').click(function(){
$('#freelancer_type').attr('disabled',false);
$('#freelancer_type').show();
});

$('#hire').click(function(){
$('#freelancer_type').attr('disabled',true);
$('#freelancer_type').hide();
});

});
</script>

<---controller->
<?php
class User extends CI_Controller{

function __construct(){
parent::__construct();

$this->load->helper('url');
$this->load->model('user_model');
}






public function index()
{

}

function login(){
if(!$this->session->userdata('user_logged_in')){

$this->data['page_title'] = 'E-sourcing - Login';

$this->load->helper('form');

if(!$_POST) {
$this->load->view('users/login_v',$this->data);
} else {
//print_r($_POST); die;

$this->load->library('form_validation');
$this->form_validation->set_error_delimiters( '', '<br>' );

$this->form_validation->set_rules('email', '<strong>Email</strong>', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', '<strong>Password</strong>', 'trim|required|xss_clean');

if($this->form_validation->run() == FALSE) {
$this->data['error']=validation_errors();
$this->load->view('users/login_v',$this->data);
} else {
$this->load->model('login_model');

$user = $this->login_model->user_login($_POST['email'], $_POST['password']);
if($user) { //print_r($user); die;
if($user->status==1){
$sess_array = array(
'user_id' => $user->user_id,
'username' => $user->username,
'email' => $user->email,
'user_type' => $user->user_type,
'status' => $user->status,
);
$this->session->set_userdata('user_logged_in', $sess_array);

if(isset($_POST['remember']) && $_POST['remember']=='on') {
$this->load->helper('cookie');
$this->input->set_cookie("email",$_POST['email'],time()+(86400*7));
$this->input->set_cookie("password",$_POST['password'],time()+(86400*7));
$this->input->set_cookie("rememberme",$_POST['remember'],time()+(86400*7));

}

if($user->user_type == "freelancer"){
redirect('freelancers','refresh');
} else {
redirect('clients','refresh');
}
} else {
$this->data['error'] = "This User is Blocked! Please Contact our Support system.";
$this->load->view('users/login_v',$this->data);
} //else 4
} else {
$this->data['error'] = "Invalid Email or Password";
$this->load->view('users/login_v',$this->data);
} //else 3
} // else 2
}//esle 1
} else {

$chek_sess=$this->session->userdata('user_logged_in');
if($chek_sess['user_type'] == "freelancer"){
redirect('freelancers','refresh');
} else {
redirect('clients','refresh');
}
//redirect('user/profile');
}
} //login function


function signup(){
if(!$this->session->userdata('user_logged_in')){
$this->data['page_title'] = 'E-sourcing - Signup';
if(!$_POST) {
$this->load->view('users/signup_v',$this->data);
} else {
//print_r($_POST); die;

$this->load->library('form_validation');
$this->form_validation->set_error_delimiters( '', '<br>' );

$this->form_validation->set_rules('first_name', '<strong>First Name</strong>', 'trim|required|xss_clean');
$this->form_validation->set_rules('last_name', '<strong>Last Name</strong>', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', '<strong>User Name</strong>', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', '<strong>Email</strong>', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', '<strong>Password</strong>', 'trim|required|xss_clean');
$this->form_validation->set_rules('user_type', '<strong>I Want To</strong>', 'trim|required|xss_clean');
if(isset($_POST['user_type']) && $_POST['user_type']=='freelancer'){
$this->form_validation->set_rules('freelancer_type', '<strong>User Type</strong>', 'trim|required|xss_clean');
}

if($_POST['password']!=$_POST['confirm_password']){
$this->data['error']="The Entered Password Missmathed!";
$this->load->view('users/signup_v',$this->data);
}
else {
if($this->form_validation->run() == FALSE) {
$this->data['error']=validation_errors();
$this->load->view('users/signup_v',$this->data);
} else {
if($this->check_user($_POST['email'],$_POST['username'])==TRUE) {
$data['ip_address'] = $this->get_client_ip();
$data['first_name'] = $_POST['first_name'];
$data['last_name'] = $_POST['last_name'];
$data['username'] = $_POST['username'];
$data['email'] = $_POST['email'];
$data['password'] = $_POST['password'];
$data['user_type'] = $_POST['user_type'];
if(isset($_POST['sms_alert']) && $_POST['sms_alert']=='on'){ $data['sms_alert'] =1; } else { $data['sms_alert']=0; }
if($_POST['user_type']=='freelancer'){ $data['freelancer_type']= $_POST['freelancer_type']; }
$data['activation_code'] = md5(time());
$data['created_on'] = time();
$data['status'] = 0;

$last_id = $this->user_model->signup($data);
if($last_id){
$this->send_email($last_id);
$this->session->set_flashdata('success', 'Your account succesfully created.<br>Please check your email and follow the steps to verify account.');
redirect('user/signup','refresh');
} else {
$this->data['error']="Error in Creating Account! Try Again.";
$this->load->view('users/signup_v',$this->data);
}
} else {
$this->data['error']="This Email used by another person! Please use other email address.";
$this->load->view('users/signup_v',$this->data);
}
}
}
}
}
//else
//{
//redirect('user/profile','refresh');
//}
}


public function logout(){
$this->session->sess_destroy();
redirect('home','refresh');
}

function check_user($email='', $username=''){
$chk = $this->user_model->check_exist_user($email, $username);
if($chk=='exists') {
return FALSE;
} else {
return TRUE;
}
}

function get_client_ip() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

function send_email($userId){
$this->load->model('login_model');
$admin = $this->login_model->get_admin();
$user = $this->user_model->get_user($userId);

$config1 = Array(
'mailtype' => 'html',
'charset' => 'utf-8',
);
$this->load->library('email',$config1);

$this->email->from($admin['email'], 'E-Sourcing');
$this->email->to($user->email);
$this->email->cc($admin['email']);
$this->email->reply_to('no-reply@email.com', 'No-Reply');

$this->email->subject('Verify Your Email Address.');

$message = "
<br />Welcome to E-Sourcing!<br />To finish your registration and get hired, there is one more quick step:
<br />To continue, click below and verify your email address:<br />
<br /><a href='".base_url('user/confirm')."/".$user->user_id."/".$user->activation_code."' target='_blank'>Click here</a> to verify your email address<br />
<br />Or you can also copy and paste this link to your browser:</br />
<br />".base_url('user/confirm')."/".$user->user_id."/".$user->activation_code."
<br />Thanks for joining E-Sourcing!
<br /><br />
Sincerely,<br />
E-Sourcing<br />
".base_url();

$this->email->message($message);

@$this->email->send();
$this->email->clear();
return $this->email->print_debugger();
}

function confirm(){ //http://local.foo.com/e_sourcing/user/confirm/22/bae809a21d19e89e59c6f1268b763367
$uid = $this->uri->segment(3);
$activation_code = $this->uri->segment(4); //echo $activation_code; die;

$user = $this->user_model->get_user($uid); //print_r($user); die;
if($user!="Not found"){
if($user && $user->activation_code==$activation_code) {
$this->user_model->activate_account($uid);
$this->session->set_flashdata('success', 'Congratulation!<br>Your account is verified.');
} else {
$this->session->set_flashdata('error', 'Problem in account verification. Please try again later or contact our support system.');
}
} else {
$this->session->set_flashdata('error', 'Error!<br>This user is not exists.');
}
redirect('user/login','refresh');

}
function forget()
{

$this->data['page_title'] = 'E-sourcing - Recover Password';
$this->load->library(array('form_validation'));
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if(!$_POST)
{
$this->load->view('users/forgetpassword',$this->data);
}
else
{
$this->form_validation->set_rules('email','Email','required');
if($this->form_validation->run()==false){
$this->data['error']=validation_errors();

$this->load->view('users/forgetpassword',$this->data);
}

else
{
$email=$this->input->post('email');
$result=$this->user_model->check_user_email($email);

if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array('id' => $row->user_id,'email' => $row->email,'password' => $row->password);
}
$this->load->model('login_model');
$admin = $this->login_model->get_admin();

//print_r($sess_array);die;

//email section
$this->load->library('email');
$this->email->set_newline("\r\n");

$this->email->from($admin['email'], 'E-Sourcing');
$this->email->to($sess_array['email']);
$this->email->subject('Recover Your Password');
$this->email->message('Account Login Info
Your Password is '.$sess_array['password'].' and email address '.$sess_array['email']);

$result = $this->email->send();
//echo $this->email->print_debugger();
$this->session->set_flashdata('success', 'Your Account Login Detail Had Been Sent Successfully To Your Email');
redirect('user/login', 'refresh');
return TRUE;
}
else
{

$this->data['error']="Invalid Email";
$this->load->view('users/forgetpassword', $this->data);
}
}
}
}
}
?>
<--model->
<?php

class User_Model extends CI_Model{

function __construct() {
// Call the Model constructor
parent::__construct();
$this->load->database();
}

function get_user($id){
$this->db->select("*");
$this -> db -> from('users');
$this -> db -> where('user_id',$id);

$query = $this->db->get();
if ($query->num_rows()>0) { return $query->row(); }
else { return 'Not found'; }
}

function get_users_by_type($type){
return $this->db->get_where('users',array('user_type'=>$type,'status'=>1))->result_array();
}

function check_email($email,$type){
$this->db->select("email");
$this -> db -> from('users');
$this -> db -> where('email',$email);
$this -> db -> where('user_type',$type);

$query = $this->db->get();
if($query->num_rows()>0){
return 'exists';
}else{
return 'available';
}
}

public function edit_user($id){
$query = $this->db->get_where('users',array('user_id' => $id));
return $query->row_array();
}

function signup($array){
$this->db->insert('users',$array);
return $this->db->insert_id();
}

function check_exist_user($email, $username=''){
$this->db->select('username');
$this->db->from('users');
$this->db->where('username',$username);

$query = $this->db->get();

$this->db->select('email');
$this->db->from('users');
$this->db->where('email',$email);

$query1 = $this->db->get();

if($query->num_rows()>0 || $query1->num_rows()>0){
return 'exists';
}else{
return 'available';
}
}

function activate_account($uid){
$data['activation_code'] = NULL;
$data['status'] = 1;
$this->db->update('users',$data,array('user_id'=>$uid));
}



function check_user_email($email){

$this -> db -> select('user_id,email,password');
$this -> db -> from('users');
$this -> db -> where('email', $email);
$this -> db -> limit(1);

$query = $this -> db -> get();

if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}

}
public function view_user($id){

$data = $this->db->get_where('users',array('user_id'=>$id));
return $data->row_array();

}




}

?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here