sending email with codeigniter - 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.

Related

Undefined variable: id when trying to pass data from URL in CodeIgniter

I am trying to pass variable.
I want to show all the sponsor ids from the id. i.e. if 203035051 passed in the URL then in the view i can see all ids for which 203035051 is sponsor.
Here is my anchor link:
<a href="<?=base_url('v3/member_income?id='.$id);?>">
Here is my Model: MemberIncome
public function display_records($id)
{
$this->db->select('*');
$this->db->from('users');
$this->db->where("id",$id);
$query = $this->db->get();
if ($query) {
return $query->row_array();
} else {
return false;
}
}
Here is my Controller
public function displaydata()
{
/*load Model*/
$this->load->model('MemberIncome');
$result['data']=$this->MemberIncome->display_records($id);
$this->load->view('customer/member_income', $result);
}
here is my route
$route['v3/member_income'] = 'IncomeData/displaydata';
Here the reason:
$route['v3/member_income'] = 'IncomeData/displaydata';
You need to send the ID through routes. Add the (:any) and $1 those are the parameters you need to send. Without it, the "displaydata" won't receive the ID.
$route['v3/member_income/(:any)'] = 'IncomeData/displaydata/$1';
In the anchor section, actually you don't need "?id=", just send it like this
<a href="<?= base_url('v3/member_income/'.$id); ?>">
Missing something in route to catch your ID. Try this way
$route['v3/member_income/(:any)'] = 'IncomeData/displaydata/$1';

codeigniter: login using email but I need username in session

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.

Codeigniter and Set Session in Model

I want to unset and set session in model function depend on passed id.
I have language field for every record and should change session considering that.
in sample word i have a link like this : http://www.mysite.com/report/2020
now this article by this id maybe saved by english language but current session is french.so I should change the session.
here is my code but it dos not work.
//function for checking language by id
public function lang($id)
{
$data = $this->db
->select('language')
->from('content')
->where('id',$id)
->get();
if ($data->num_rows > 0) {
return $data;
}else{
return false;
}
}
//function for reading data from db depend on id and language
public function report($id)
{
$cat=2;
$code=$id;
$langu= $this->lang($id)->row()->language;
if($langu==3){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','dr');
}
if($langu==2){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','pa');
}
if($langu==1){
$this->session->unset_userdata('lang');
$this->session->set_userdata('lang','en');
}
$language= $this->session->userdata('lang');
$lang=$language;
$data = $this->db
->select('*')
->from('content')
->where('category',$cat)
->where('language',$lang)
->where('id',$code)
->get();
if ($data->num_rows > 0) {
return $data;
}else{
return false;
}
}
the best way to session is:
you have to check the query in model, and call the model in controller, if its true set session data within controller....
Setting Session within the model is not a good idea for an MVC architecture.
Sometimes there is really need to set or get session data in model.
Use then Code Igniter instance like below:
$codeIgniter =& get_instance();
$codeIgniter->set_userdata('data', $data);
// or to read data:
$data = $codeIgniter->userdata('data');
Note that using $this to call set_userdata() or userdata() in model will not work correctly.

Redirect Loop error in CodeIgniter

I worked on this for a day. I get this same problem, but I don't understand.
<?php
class Users extends CI_Controller
{
function index()
{
redirect('Users/login');
}
function login()
{
$data['title'] = 'Selamat datang di Sistem Informasi Koperasi';
$data['main_content'] = 'login';
$this->load->view('Users/template', $data);
}
function logout()
{
$this->session->sess_destroy();
$this->session->set_flashdata('info_login', 'Anda sudah keluar dari sistem');
redirect('Users/login');
}
function validate()
{
//Load User Model
$this->load->model('Users_Model');
//Validate User
$query = $this->Users_Model->validate();
if($query != '') {
//Mengambil Roles dari Groups
$roles = $this->Users_Model->roles($query->group_id);
//$this->login_model->last_login($query);
$data = array(
'username' => $query->username,
'roles' => $roles,
'is_logged_in' => true
);
$this->session->set_userdata($data);
if($roles == 'administrators') {
redirect('Administrators/index');
} elseif($roles == 'managers') {
redirect('Managers/index');
}
else {
$this->session->set_flashdata('info_login', 'Mohon maaf anda belum terdaftar sebagai Group! Silahkan hubungi admin!');
redirect('Users/login');
}
} else {
$this->session->set_flashdata('info_login', 'Maaf,username dan password yang anda masukkan salah,silahkan coba kembali!');
redirect('Users/login');
}
}
}
In Chrome and Firefox I get this message. What should i do?
This webpage has a redirect loop The webpage at
http://localhost/simpks/index.php/Users/login has resulted in too
many redirects. Clearing your cookies for this site or allowing
third-party cookies may fix the problem. If not, it is possibly a
server configuration issue and not a problem with your computer. Here
are some suggestions: Reload this webpage later. Learn more about this
problem. Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many
redirects.
this is my view template.php
<?php
$this->load->view('includes/header',$main_content);
$this->load->view('Users/'.$main_content);
$this->load->view('includes/footer');
?>
this is my model Users_Model.php
<?php
class Users_Model extends CI_Model{
function validate(){
$this->db->where('username',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('Users');
if($query->num_rows == 1){
$row = $query->row();
return $row;
}
}
function roles($id){
$this->db->where('id',$id);
$query = $this->db->get('Groups');
if($query->num_rows == 1){
$row = $query->row();
return $row->name;
}
}
}
?>
use include instead loader if you call it in view.
ex : include 'includes/footer';
you don't have to put redirect('Users/login'); for session checking in your view class. Just erase it.
If you need redirect, put it in another page like users/test. If session is expired in users/test call redirect method in users/test controller. For better structure, i think you should minimize php function in view.
I am also facing that problem but both page's controllers i've redirects method so I add a refresh in redirect method, try it.
read at bottom of page CI redirect with refresh
<?php
class Users extends CI_Controller
{
function index()
{
redirect('another_controller/login');
}
}
Create another controller - another_controller.php
class another_controller extends CI_Controller
{
function login()
{
$this->load->view('home');
}
}

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