codeigniter: login using email but I need username in session - codeigniter

After I created a registration form with the fields: user, email, pass, cpass, I saved the user field in a session, so when I submit the form it should redirect me to the homepage displaying the message: hello + $this->session->userdata('user').
In my login form I have only the fields: email, pass and I want after submitting the form to be redirected aswell to my homepage displaying the same message but I don't know how to save the username in the session.How can I do that?

suppose you have a function in your model claass for checking user credentials.like-
function check_user($email, $pass) {
//select db column that you will need for future task
$this->db->select('id, user, email, pass);
$this->db->from('user_table');
$this->db->where('email', $email);
$this->db->where('pass', $pass);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
after returning in your controller function just put them in the session.like-
public function do_login(){
$result = $this->login_model->check_user($email, $pass);
//put them in the session
foreach($result as $row){
$this->session->set_userdata('user_id', $row->id);
$this->session->set_userdata('user', $row->user);
$this->session->set_userdata('email', $row->email);
//put more information as your need
}
}
please modify the code as your situation.

Related

CodeIgniter - Load View after Update

After I have updated a customer (which works fine)
public function update($customer_acc) {
if($this->session->userdata('logged_in'))
{
$id= $this->input->post('did');
$data = array(
'customer_name' => $this->input->post('dname')
);
$this->load->model('update_model');
$this->update_model->update_customer($id,$data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
How can i then load back the view customer function. What i need is after the update has been completed so then go back to viewing the customer.
public function view($customer_acc) {
if($this->session->userdata('logged_in'))
{
$this->load->model('display_single_customer');
$customers = $this->display_single_customer->view_single_customer($customer_acc);
$data['customer_acc'] = $customers['customer_acc'];
$data['customer_name'] = $customers['customer_name'];
$this->load->view('customers_single_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
you just exit to another method in your controller to show the new page. for passing the id you can either pass it directly
if ( some condition ) {
$id = $this->input->post('did', TRUE);
// blah blah blah
// success -- now go to show customer method
$this->showCustomer($id) ; }
function showCustomer($id){
// get the customer to display using the $id that was passed
$customers = $this->display_single_customer->view_single_customer($id);
OR you can declare the id with $this-> and then it is available to any method in the controller
$this->id = $this->input->post('did', TRUE);
// blah blah
$this->showCustomer() ; }
function showCustomer(){
// get the customer to display using $this->id
$customers = $this->display_single_customer->view_single_customer($this->id);
// etc etc
I think i may have sussed it I can use a redirect using the $id which is the customer account number
redirect("/customers/view/$id");
Is this the correct way, It works but is it best practice ?

Laravel 4 - Display a different view depending on a value in user database

I'm trying to display a profile View depending if a user has activated their account - i.e. active = 1 (or 0 for inactive)
If the user has activated their account show a View with users details of if they haven't display a different View saying they need to activate their account.
This is the code I am having trouble with:
public function user($username) {
// Get the /username from URL and check the database
$user = User::where('username', '=', $username);
// If /username does exist
if($user->count()->('active', '=', 1)) {
// Grab user info from database
$user = $user->first();
// return the active user view
return View::make('profile.active-user')->with('user', $user);
} else {
// Grab user info from database
$user = $user->first();
// return the inactive view
return View::make('profile.inactive-user')->with('user', $user);
}
return App::abort(404); // else throw a 404
}
Any help/logic would be really appreciated.
Thanks, Jack.
You can simplify it a bit by combining the eloquent query and 404. Also you already have your user object after the query so no need for extra eloquent stuff:
public function user($username) {
//get user from db, if no result throw not found (404)
$user = User::where('username', '=', $username)->firstOrFail();
if($user->active) {
// return the active user view
return View::make('profile.active-user')->with('user', $user);
} else {
// return the inactive view
return View::make('profile.inactive-user')->with('user', $user);
}
}

sending email with codeigniter

I have this MODEL and I get the email which I want to send
class Cliente_Model extends CI_Model{
public function getInfo($id){
$this->db->select('*');
$this->db->from('pendientes');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
return $row['email'];
}
}
else
{
return FALSE;
}
}
}
CONTROLLER
$this->load->model('cliente_model', 'client');
$clientInfo = $this->client->getInfo($id);
$this->email->from('myemail#gmail.com', 'Demo');
$this->email->to($clientInfo);
$this->email->subject('Email Test');
$this->email->message('your user is '.$clientInfo.' and your password is '.$clave);
$this->email->send();
and I need some help here, I can get the email and it can send it perfectly but in the message I need to send the password also and I don't know how I can get it from the model.
thanks in advance!
Instead of returning '$row['email]' from your model, you should return the whole '$row' which will enable you to get other data from the model and not only the email field.
Not sure, but you might need to use it like this in the controller:
$this->email->message('your user is '.$clientInfo->email.' and your password is '.$clientInfo->passwordField);
Good luck.

codeigniter ion auth getting user id

I need to get the user id of the person logged in, so that i can run a query for that user. Im using ion auth, should i use a session or call it from the db, should i use a helper or not? anyway, heres what im trying to do:
example: how many work orders does logged in user have?
query would be: select * from work_orders WHERE status = "1" AND userid = "%THE LOGGED IN USER"
here is my controller, which doesnt have the "get user id" code:
public function index()
{
$this->load->view('templates/header');
$data['total_open_wo'] = $this->Home_model->total_open_wo();
$data['result'] = $this->Home_model->index();
$this->load->view('home', $data);
$this->load->view('templates/footer');
}
here is my model:
public function total_open_wo() {
$this->db->where('status', "1");
$this->db->where('tid', "NEED_THE_USER_ID");
$num = $this->db->count_all_results('work_orders');
return ($num);
}
Provided the user is logged in you can get the user ID by using the get_user_id() function in ion auth. On a related note in your model you need a $this->db->from() call to set the table to fetch the number of rows from.
public function total_open_wo() {
$userId = $this->ion_auth->get_user_id();
$this->db->where('status', '1');
$this->db->where('tid', $userId);
$this->db->from('work_orders');
$num = $this->db->count_all_results();
return $num;
}
Use the function $this->ion_auth->logged_in() to make sure that the user is logged in before calling the model function.
you can retrieve the logged in user details using user() method.
For example to get user id
echo $this->ion_auth->user()->row()->id;

Codeigniter login (auth) only with username (without password)

Is there a way to make authentication into Codeigniter, only with username, not using a password?
Thank you in advance!
Of course. Hope you're not trying to store anything secure though. The terse Datamapper ORM solution would be something like the following. Adapt to Active Record if you need to.
The CodeIgniter
class Login extends CI_Controller{
function try_username(){
$p = $this->input->post();
if($p){
$u = new User();
$u->where('username', $p['username'])->get();
if($u->exists()){
$data = array("loggedin" => true "username" => $p['username']);
$this->session->set_userdata($data);
redirect('logged_in_controller');
}
}
}
}
Now your HTML:
<form action='login/try_username' method='post'>
<input type='text' name='username'/>
<input type='submit'/>
</form>
Well of course if you do not require security you could easily set your session based upon a single username. This is just as much possible in Codeigniter as in just PHP or other framework/language.
controller example:
function authenticate()
{
// check postdata
if ($this->input->post('username')):
if (!$this->auth_model->login($this->input->post('username',true))):
$this->session->set_flashdata('message', 'Bad username!');
else:
// go to user page or such...
endif;
endif;
redirect('login'); // take user back to login page
}
In above example I assume you have a model (auth_model) that checks database for username and sets login session for you. If user is not logged in then you get an error message and is sent back to login page...
you need only username = abc#gmail.com
public function login_ulogin($identity, $remember=FALSE)
{
$this->trigger_events('pre_login');
$this->trigger_events('extra_where');
$query = $this->db->select('username,group_id, email, id, password, active, last_login')
->where('username', $identity)
->from('users')
->get();
if ($query->num_rows() == 1)
{
$user = $query->row();
$password = TRUE;//$this->hash_password_db($user->id, $password);
if ($password === TRUE)
{
$this->set_session($user);
$this->update_last_login($user->id);
$this->clear_login_attempts($identity);
if ($remember && $this->config->item('remember_users', 'ion_auth'))
{
$this->remember_user($user->id);
}
$this->trigger_events(array('post_login', 'post_login_successful'));
$this->set_message('login_successful');
return TRUE;
}
}
//Hash something anyway, just to take up time
//$this->hash_password($password);
$this->increase_login_attempts($identity);
$this->trigger_events('post_login_unsuccessful');
$this->set_error('login_unsuccessful');
return FALSE;
}

Resources