This is the controller code for signing up:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index(){
$data = array(
'title' => 'SignUp Page'
);
$this->load->view('header', $data);
$this->load->view('signup');
}
public function signup_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('salutation', 'Salutation', 'required');
$this->form_validation->set_rules('fName', 'First Name', 'required|trim|alpha|xss_clean|strip_tags');
$this->form_validation->set_rules('lName', 'Last Name', 'required|trim|alpha|xss_clean|strip_tags');
if($this->form_validation->run()){
$month = $this->input->post('months');
$day = $this->input->post('days');
$year = $this->input->post('years');
$birthday = date("d-m-Y",mktime(0,0,0,$month,$day,$year));
}
$this->form_validation->set_rules('address', 'Address', 'required|trim|xss_clean|strip_tags|min_length[10]');
$this->form_validation->set_rules('contact', 'Contact No', 'required|trim|xss_clean|strip_tags|numeric|min_length[8]|max_length[8]');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]|xss_clean|strip_tags');
$this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|xss_clean|strip_tags|min_length[3]|max_length[20],is_unique[users.username');
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|xss_clean|strip_tags|alpha_numeric');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]|xss_clean|strip_tags|alpha_numeric');
if ($this->form_validation->run()){
$key = md5(uniqid());
$this->load->library('email', array('mailtype'=>'html'));
$this->load->model('model_users');
$this->email->from('admin#snt.website', "SWAP");
$this->email->to($this->input->post('email'));
$this->email->subject("Confirm your account.");
$message = "<p>Thank you for signing up!</p>";
$message .= "<p><a href='".base_url()."main/register_user/$key'>Click Here</a> to confirm your account</p>";
$this->email->message($message);
if ($this->model_users->add_temp_users($key, $birthday)){
if ($this->email->send()){
echo "The email has been sent!";
} else echo "Could not send the mail";
} else echo "problem adding to database";
} else {
$this->index;
}
}
}
I have checked through my codes and have no idea why does this error keep showing up:
If i am to remove all the validations it will work perfectly fine.Can someone help me with this problem please?
I think you wanted to write:
$this->index();
//^^ So you actually call the function index and not the property
instead of:
$this->index;
Related
I have a registration system with CodeIgniter but currently I have no control on email and password. A user can register without putting email or a password.
I have an index() and a register_user function() in my Signup controller but the redirection is not working on success
At the moment I have the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->load->view('home-view');
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
function register_user(){
$this->load->library('custom_u_id');
$data = array('user_id' => $this->custom_u_id->construct_id('USR'),
'name' => $_POST['name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
);
$this->load->model('signup_model');
$user_details = $this->signup_model->register_user($data);
if (!empty($user_details)){
$user_data = array
(
'user_id' => $user_details['user_id'],
'email' => $user_details['email'],
'name' => $user_details['name'],
'user_type' => $user_details['user_type'],
);
$this->session->set_userdata('sessiondata',$user_data);
if (intval($user_details['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
} else{
redirect('login');
}
}// end of function login
}
Do I need to put the form_validation in my register_user function ? I've tried but the check doesn't work anymore...
I also have in my view the <?php validation_errors();?> function and the <?php form_open(base_url().'signup');?>
looking by your code, i think you want to put register_user() inside validation TRUE since the query is in that method.
so try to change your code to this :
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->register_user();
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
and be sure your form action to like this :
<form action="<?=site_url('/signup/index');?>">
# table name is users#
## model name is user_model##
### controller name is get_password ###
issue - no change on the password , remain as old
> model(user_model)
public function updatePassword($email,$data)
{
$data1=array('password'=>$data);
$this->db->where('email','$email');
$this->db->update('users','password');
$success = $this->db->affected_rows();
if(!$success){
error_log('Unable to updatePassword');
return false;
}
return true;
}
> controller(get_password)
public function index($rs=FALSE)
{
$this->load->database();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->library('session');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('passconf', 'password Confirmation', 'required|matches[password]');
if ($this->form_validation->run() == FALSE)
{
echo form_open();
$this->load->view('users/fpre');
}
else
{
$data = array(
'password' => md5($this->input->post('password')),
);
$email =array(
'email' =>$this->input->post('email')
);
$this->user_model->updatePassword($data,$email);
echo "Congratulations!";
}
}
it shows no error but the password is not updated remain same at users table..i can't find the problem is, please help me to find it out ..
Controller (get_password):
public function index() {
$this->load->database();
$this->load->helper(array('form', 'url'));
$this->load->library(array('form_validation', 'session'));
$this->load->model('user_model');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Current password', 'required');
$this->form_validation->set_rules('newpassword', 'password', 'required');
$this->form_validation->set_rules('newpassconf', 'password Confirmation', 'required|matches[newpassword]');
$email = $this->input->post('email');
$success = false;
$msg = '';
if ($this->form_validation->run() !== FALSE) {
$password = md5($this->input->post('password'));
if ($this->user_model->checkPassword($email, $password)){
$newpassword = md5($this->input->post('newpassword'));
if ($this->user_model->updatePassword($email, $newpassword)){
$success = true;
}else{
$msg = 'Unable to updatePassword';
}
}else{
$msg = 'Incorrect password';
}
}
if ($success){
echo 'Congratulations!';
}else{
$this->load->view('users/fpre', array(
'email'=>$email,
'msg'=>$msg
));
}
}
Model (user_model):
public function checkPassword($email, $password) {
$users = $this->db->get_where('users', array('email'=>$email))->row();
return $users->password === $password;
}
public function updatePassword($email, $newpassword) {
$data = array('password' => $newpassword);
$this->db->where('email', $email)
->update('users', $data);
$success = $this->db->affected_rows();
if (!$success) {
error_log('Unable to updatePassword');
}
return $success;
}
View (users/fpre):
if ($msg){
echo 'Message: '.$msg;
}
echo form_open();
echo form_input('email', $email);
echo form_password('password');
echo form_password('newpassword');
echo form_password('newpassconf');
echo form_submit('', 'Enviar');
echo form_close();
Changes to compare:
Your model function shows the parameters are expected to be email and then password, but your controller is passing them through be other way around.
$this->user_model->updatePassword($data,$email);
Should be:
$this->user_model->updatePassword($email,$data);
I also believe the data needs to be passed differently. The where() function expects either where(field_name, value) or where(array(field_name => value)). Looking at your code, you seem to be mixing both of those.
Using set() should help with this too, so instead of
$data1=array('password'=>$data);
$this->db->where('email','$email');
$this->db->update('users','password');
Use:
$this->db->set($data);
$this->db->where($email);
$this->db->update('users');
Note: code untested.
I believe this line $this->db->update('users','password'); should be $this->db->update('users', $data);.
Right now you are not passing the password to the update function. You are passing the string "password".
I am working in Codeigniter registration and login forms. I have created them all is working. now what i need to auto logged in user when it successful registered on the site.
Add user code:
public function add_user() {
$data = array(
'username' => $this->input->post('user_name'),
'email' => $this->input->post('email_address'),
'password' => md5($this->input->post('password'))
);
$this->db->insert('user', $data);
}
Registration and validation:
public function registration() {
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->index();
} else {
$this->user_model->add_user();
$this->thank();
}
}
In else part,
else {
$id = $this->user_model->add_user();
// set session here like how you will set on login
$data["user_id"] = $id;
...// other required data for session
$this->session->set_userdata($data);
$this->thank();
}
In model return last insert id after inserting the data
$this->db->insert('user', $data);
return $this->db->insert_id();// <--- this line
I am trying to register a user with Ion Auth, but the register() function does not seem to report any error messages. How does one register with Ion Auth? I've already read various tutorials like this one and this one (and the documentation), but they do not seem to be working.
function register()
{
// do not allow registration if logged in
if ($this->ion_auth->logged_in())
{
redirect('/');
}
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check');
$this->form_validation->set_rules('email2', 'Email Confirmation', 'trim|required|valid_email|matches[email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
if ($this->form_validation->run() == FALSE)
{
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->load->view('header');
$this->load->view('register', $this->data);
$this->load->view('footer');
}
else
{
$username = $this->input->post('email');
$password = $this->input->post('password');
$email = $username;
$additional_data = array(
'first_name' => $this->input->post('name'),
'last_name' => '',
);
if (!$this->ion_auth->email_check($email))
{
$group_name = 'users';
$uId = $this->ion_auth->register($username, $password, $email, $additional_data, $group_name);
if ($uId == FALSE)
{
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect("home/register", 'refresh');
}
else
{
redirect('/'); // registration success
}
}
}
}
function email_check($str)
{
if ($this->ion_auth->email_check($str))
{
$this->form_validation->set_message('email_check', 'This email is already registered.');
return FALSE;
}
return TRUE;
}
I have create a form with 2 file upload fields and basically I want to make them required fields. I created a call back function but just can't seem to make it work properly. It is using the callback and posts the error if some other fields are left blank but sends the form whether files are attached or not.
I'm pretty new to Codeigniter. Any help would be very much appreciated!
Controller:
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//Upload errors array
$up_errors = array();
$this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
$this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
$this->form_validation->set_rules('dob', 'Date of Birth', 'required');
$this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
//$this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
$this->form_validation->set_rules('address_city', 'City', 'required|alpha');
$this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
//$this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
$this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
$this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
$this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
$this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
$this->form_validation->set_rules('end_date', 'Course End Date', 'required');
//Custom callback
$this->form_validation->set_rules('file', 'Attachment', 'callback_handle_upload');
//Check if file attached
//if (isset($_FILES['file']))
//{
//}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('home');
}
else
{
//Display Success page
$this->load->view('formsuccess');
//Array helper
$this->load->helper('array');
//Set form data array
$fields = $this->input->post('first_name')."\n".
$this->input->post('last_name')."\n".
$this->input->post('dob')."\n".
$this->input->post('nationality')."\n".
$this->input->post('gender')."\n".
$this->input->post('address_l1')."\n".
$this->input->post('address_l2')."\n".
$this->input->post('address_city')."\n".
$this->input->post('address_postcode')."\n".
$this->input->post('address_country')."\n".
$this->input->post('e_address')."\n".
$this->input->post('h_tel')."\n".
$this->input->post('mobile')."\n".
$this->input->post('university')."\n".
$this->input->post('campus')."\n".
$this->input->post('course')."\n".
$this->input->post('end_date');
$msg = serialize($fields);
//Upload files to server
$this->load->library('upload');
//Send Email
$this->load->library('email');
//Set config
$config['upload_path'] = './attachments'; //if the files does not exist it'll be created
$config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
$config['max_size'] = '4000'; //size in kilobytes
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files
//Attach the 2 files to email
foreach($_FILES as $key => $value)
{
//var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
$file = $uploaded['success'][$key]['full_path'];
$this->email->attach($file);
//unlink($file);
}
//var_dump($msg); //FOR TESTING
$this->email->from($this->input->post('e_address'),$this->input->post('first_name') . $this->input->post('last_name'));
$this->email->to('tom#shu.ac.uk');
$this->email->subject('NON-SHU STUDENT REQUEST');
$this->email->message($msg);
$this->email->send();
//echo $this->email->print_debugger();
}
}
function alpha_dash_space($str_in)
{
if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
$this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
return FALSE;
} else {
return TRUE;
}
}
function handle_upload()
{
if (count($_FILES['file']['name'] < 2)
{
// throw an error because nothing was uploaded
$this->form_validation->set_message('handle_upload', "You must attach both files!");
return false;
}
else
{
return TRUE;
}
}
}
?>
check the following form validation extension which can help you to validate files, images with CI form validation library copy the code and create MY_Form_validation.php file in application library and paste code in that file and save
form file validation
code to copy