I am in the process of creating an activation email for my registration process. I need to pass the variables containing the activation code and email address from my model back to my controller where I will send the email.
My registration form currently writes all signup data to the database and creates and activation code.
How can you pass the variables 'activation_code' and 'email' from my model back to my controller for use in my email.
Model:
...
$new_member_insert_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email_address' => $email,
'password' => hashPassword($salt, $password, $hash),
'activation_code' => activationCode($email, $hash.$salt),
'hash' => $hash
);
$insert = $this->db->insert('members', $new_member_insert_data);
return $insert;
}
Controller
$this->load->model('members_model');
if($this->members_model->create_member())//return variables here somehow
{
//get activation code + email variables
//send activation email
$this->session->set_flashdata('success', 'success');
redirect('home', 'location');
}
else
{
$viewdata['main_content'] = $category;
$this->load->view('includes/template', $viewdata);
}
You could simply return the array of values on success, or FALSE on failure:
$insert = $this->db->insert('members', $new_member_insert_data);
if ($insert) {
return $new_member_insert_data; // Same data
}
else {
return FALSE;
}
...but it doesn't really make sense, as you must be passing something to the model's method in order for it to be useful (or have access to some of these values beforehand, your given code is incomplete). Also, it can be confusing returning different data types and usually a bad idea.
Despite not having access to your full code here, I think the safest and most accurate way may be running another query after the INSERT runs. For instance, looking up the user by email_address (assuming it's supposed to be unique).
Related
Hello im using fullcalendar.io for my website, for the framework i use ci3, im new with ci.
So the problem is, can i just load the database fullcalendar.io just for user who logged in?
im planning to use email to connect it since i used email to login to my website..
here is my user database example :
here is my events on fullcalendar.io database :
here is my load function in controllers
function load()
{
$event_data = $this->fullcalendar_model->fetch_all_event();
foreach ($event_data->result_array() as $row) {
$data[] = array(
'id' => $row['id'],
'title' => $row['title'],
'start' => $row['start_event'],
'end' => $row['end_event']
);
}
echo json_encode($data);
}
and here is my fetch_all_event() code
function fetch_all_event()
{
$this->db->order_by('id');
return $this->db->get_where('events');
}
should i adding something or what? can anyone help please im just starting with ci, thank u
So the easiest way is you just have to store ur session data when u log in, store it at the page u login at.
here is mine :
if ($user) {
// jika usernya ada
if ($user['is_active'] == 1) {
if (password_verify($password, $user['password'])) {
$data = [
'email' => $user['email'],
'role_id' => $user['role_id']
];
$this->session->set_userdata($data);
and u can call that email in ur load model, here is my fetch_all_event function code on my model :
function fetch_all_event()
{
$email = $this->session->userdata('email');
$this->db->order_by('id');
return $this->db->get_where('events', ['email' => $email]);
}
this will solve that problem, kept that in mind ur email at database shouldnt do as primary or unique key.
I have an issue regarding session (userdata/flashdata) on my code.
Modal
public function loginCheck(){
$email = $this->input->post('email');
//encrypt password
$this->load->library("hashing");
$password = $this->hashing->incrypt($this->input->post('password'));
$this->params = array('email' => $email, 'password' => $password);
$user = $this->findById();
if(count($user)>0){
$data = array(
'email' => $user->email,
'isLoggedIn' => 1,
'user_id' => $user->id,
'user_type' => $user->user_type
);
$this->session->set_userdata($data);
return true;
}
return false;
}
And my Controller
public function login(){
$this->load->model('model_users');
//if posted login
$submit = $this->input->post('submit');
if($submit){
$this->load->model('model_users');
$rules = $this->model_users->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == true){
// user credential from model
if($this->model_users->loginCheck()== true){
redirect("admin/site/index");
} else{
$this->session->set_flashdata('message', 'Oops! Invalid email and/ or password.');
redirect("admin/site/login");
}
}else{
$this->session->set_flashdata('message', 'Oops! Invalid email and/ or password.');
redirect("admin/site/login");
}
}
$this->loadPartialView("admin/login");
}
The session is not being set on CI 3.0. The function set_userdata() is not functioning well.
The manual session initializing is also having trouble.
Please try load Session Library into Controller and Modal before using it.
$this->load->library('session');
i dont think if this is a good answer, but have you call your session in view?
as i know ,
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
it can be found in sesseion documentation
so in your view in div tag or other, its like
<?php echo $this->session->flashdata('message');?>
First you can add library of session.
There are two way of add library.
1) You can add in controller method.
2) autoload in autoload.php file.
I have successfully created a registration and login system.
I have used useremail and password in the login form and I want to display the username and other properties related to that logged in user.
What are the simplest and best practices to get the userid after a login in codeigniter?
As good coder create session at login time and use session at website wide.
public function login($username, $password) {
$user = $this->db
->select("username, name, phone")
->where(
[
'username' => $username,
'password' => md5($password)
]
)
->get("table_name")
->row();
if ($user) {
$logindata = [
'userid' => $user->username,
'name' => $user->name,
'phone' => $user->phone
];
$this->session
->set_userdata($logindata);
return true;
}
else {
return false;
}
}
Then after you can use anywhere in website
echo $this->session->userid;
I hope it will help you in general way.
Best practice would be that when you check the login details in your model after success you should create a session saving userid of that user and use that anywhere in your application for fetching respective data against that user. Take a look at following psuedo code for login user.
public function userAuthentication($username,$pass)
{
$this->db->select('id');
$this->db->where('username',$username);
$this->db->where('password',md5($pass));
$result = $this->db->get('users')->result();
if(!empty($result)){
$this->session->set_userdata('userlogged_in', $result[0]->id);
return true;
}else{
return false;
}
}
You can place this function in your model and apply it in your controller. I hope this helps.
You may create a session and set user id after successful login.
The model:
function validate()
{
$this->db->where('username',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('memberships');
if($query->num_rows() == 1)
{
return TRUE;
}
}
function validate_admin()
{
$this->db->where('adminname',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('admin');
if($query->num_rows() == 1)
{
return TRUE;
}
}
The controller
function validate_credentials()
{
$this->load->model('membership_model');
if($this->membership_model->validate())
{
$this->db->where('username',$this->input->post('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() > 0){
foreach ($get_profile_info->result() as $row)
{
$info = array('firstname' => $row->firstname,
'lastname' => $row->lastname,
'email_address' => $row->email_address
);
}
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'firstname' => $info['firstname'],
'lastname' => $info['lastname'],
'email_address' => $info['email_address'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('/site/main_menu');
}}
else if($this->membership_model->validate_admin())
{
echo "admin";
}
else
{
redirect('login');
}
}
The if else if statement is not working correctly. The program test the first condition and if it returns false skips the second condition even if that is TRUE and execute the else statement. I'm not sure what is going worng here.
Refactor your one controller method into different methods - one for Members and one for Admin to start. And because you are calling separate database tables would suggest having a separate model for each.
Because you are interacting with a database table getting the profile information should happen in a model (not the controller).
This is a personal preference but i would set the session data in a model as well. Also there might be issues with your foreach and then getting the value $info['first name'].
Validate the form data first before sending to your database. Its safer and its better for your users - if they forget to put in the password you want to show them the form again with a helpful message. http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
and remember when in doubt -- echo it out.
I am at the tail end of signing in a created user to an account. I've commented out my flow and everything seems to make since, however I am missing a step or two because now the post data password is not being hashed.
CONTROLLER:
function validate_credentials()
{
// WHEN THE VIEW IS LOADED THIS FUNCTION IS CALLED AND LOADS MODEL AS WELL AS DEFINES THE SALT VARIABLE AND LOADS THE ENCRYPTING HELPER LIBRARY
$this->load->model('user_model', 'um');
$login = $this->input->post('submit');
$salt = $this->_salt();
$this->load->library('encrypt');
//IF THE SUBMIT BUTTON IS TRIGGERED THE POST DATA IS SENT TO THE VALIDATE FUNCTION IN THE MODEL VIA VARIABLES CREATED
if($login)
{
$data = array(
'email' => $this->input->post('email'),
'password' => $this->encrypt->sha1($user->salt. $this->encrypt->sha1($this->input->post('password')))
);
$user = $this->um->validate($data);
}
// IF ITS A REAL USER OPEN THE GATE AND LET THEM IN
if($user)
{
$this->session->set_userdata($data);
redirect('account/dashboard');
}
else
{
$this->index();
}
}
MODEL:
function validate($data)
{
$this->output->enable_profiler(TRUE);
// TAKING THE DATA FROM THE MODEL AND CHECKING IT AGAINST THE STORED INFO IN THE DB
$query = $this->db->where($data)->get('users', 1);
if($query->row())
{
return $query->row();
}
}
thanks in advance
$user->salt should just be $salt.