I try to do login page with session but face a problem.
First i created a model called giris. the model giris has a function girisKontrol
function girisKontrol($username, $password) {
$sha1_password = sha1($password);
$query = "SELECT id FROM pasaj_register WHERE username = '".$username."' and password = '".$sha1_password."'";
$result = $this->db->query($query, array($username, $sha1_password));
if ($result->num_rows() == 1)
return $result->row(0)->id;
else
return false;
}
and in a controller called giris
wrote below code,
public function main_page() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$userID = $this->giris->girisKontrol($username,$password);
if (!$userID) {
$this->session->set_flashdata('login error', TRUE);
redirect('giris/giris');
} else {
$this->session->set_userdata(array(
'logged_in' => TRUE,
'userID' => $userID));
redirect('welcome_message');
}
}
however when form is processed. i take below error,
What's the reason ?
You probably haven't loaded the giris model.
Put something like this inside the constructor of the controller or at the top of the function call of the controller to load the model.
$this->load->model('giris');
You probably forgot to load your model:
Invoke
$this->load->model('giris');
prior to
$userID = $this->giris->girisKontrol($username,$password);
the problem is that your class and your model has the same name...change your class to something like girisVO, report back
Related
I want to change the password for logged in users,but it always give me an error:[Message: Trying to get property 'password' of non-object] in the controller. How to solve this error???
Please help,Thanks much!
This is my controller:
if($this->form_validation->run()){
$cur_password = md5($this->input->post('cpassword')); // md5-encrypt the password
$new_password = md5($this->input->post('npassword'));
$conf_password = md5($this->input->post('copassword'));
//$this->load->model('queries');
$this->load->library('session');
$user_id = $this->session->userdata('user_id');//multiple users id login
$passwd = $this->queries->getCurPassword($user_id);
!ERROR FOUND HERE!->if($passwd->password == $cur_password){
if($new_password == $conf_password){
if($this->queries->updatePassword($new_password,$user_id)){
echo 'Password Updated Successfully!';
echo '<br/><label>Back to homepage</label><br/>';
}else{
echo 'Failed To Update Password!!';
}
}else{
echo 'New Password and Confirm Password is not matching!!';
}
}else{
echo 'Sorry! Current Password is not matching!!';
}
}else{
echo validation_errors();
}
This is my model:
<?php
class Queries extends CI_Model{
function login() {
$query = $this->db->select('*')
->from('users')
->where('id');
return $query->row();
}
function index() {
$userid = $this->login()->id; //id of the user which is currently logged IN
$this->session->set_userdata('user_id', $user_id);
}
public function getCurPassword($user_id){
$query = $this->db->where(['id'=>$user_id])
->get('users');
if($query->num_rows() > 0 ){
return $query->row();
}
}
public function updatePassword($new_password,$user_id){
$data = array(
//'password' => md5($this->input->post("$new_password"))
'password' => ($new_password)
//'password'=> password_hash(["$new_password"],PASSWORD_BCRYPT)
);
return $this->db->where('id',$user_id)
->update('users',$data);
}
}
Thanks!
You have error in your syntax error says that you are trying to get property of non-object means $passwd may be an array
if($passwd['password'] == $cur_password)
And in case you have null user_id
Place these two lines in your controller function above if($this->form_validation->run()){ line
$userid = $this->queries->login()->id; //id of the user which is currently logged IN
$this->session->set_userdata('user_id', $userid);
and in your login function in model
function login() {
$query = $this->db->select('*')
->from('users')
->where('username',$this->session->userdata('username'));
return $query->row();
}
Hope it helps!
This happens when the user_id not found.
Notice that the getCurPassword function will return the user if found (when checking if num_rows > 0) but if didn't found it returns NULL.
When this happens the $passwd var is null so you cann't access $passwd->password.
You can solve it by changing the if statement to:
if($passwd && $passwd->password == $cur_password){
Edited Try to retrieve your user name as and then call with it the getCurPassword function:
$user_name = $this->session->userdata('username');
$passwd = $this->queries->getCurPassword($user_name );
And in the controller change getCurPassword function as:
public function getCurPassword($user_name){
$query = $this->db->select('*')
->from('users')
->where('username', $user_name);
if($query->num_rows() > 0 ){
return $query->row();
}
}
Notice that I assume you have "username" column in your DB
I'm trying to make a login using sessions in codeigniter at the time the username and password match, but I can't get it. I'm doing this:
Controller:
public function __construct()
{
parent::__construct();
$this->load->model('main_select');
$this->load->helper('url');
$this->load->library('session');
}
...code when username and password match:
if($pass === $user){
$this->session->set_userdata(array(
'user_id' => $login['id_user'],
));//we create the session 'user_id'
}
here it is supposed that we created a session called 'user_id'
in the view it doesn't work, I have this:
if( !$this->session->userdata('id_user') ){
//see this content
//first content
}else{
//see this other
//second content
}
but I always see the same content('second content').
trying to destroy it (but not working):
public function logout()
{
//session_unset();
// destroy the session
//session_destroy();
$this->session->unset_userdata('id_user');
header("Location: ".base_url() );
}
what am I doing wrong? thanks
EDIT1:
$password = md5( $this->input->post('inputpassword') );
$login = $this->login_select->get_username($username);
//si no coincide
if( $login['password'] !== $password ) {}
Note : Always use database to handle user logins. (Code is related to database login check)
in your database create table with user and add this 2 fields.
username
password
Add some user logins to it
Then in your code
public function __construct()
{
parent::__construct();
$this->load->model('main_select');
$this->load->helper('url');
$this->load->library('session');
}
// logging
public function loging()
{
$user = mysql_real_escape_string($_POST['username']);
$pass = md5(mysql_real_escape_string($_POST['password']));
$validate = $this->main_select->validate_user($user,$pass);
if(empty($validate) || $validate>1)
{
//Not a valid user
//redirect to login page
$this->load->view('loging');
}
else
{
//valid user
//set the session
$array = array('user_id' => '$user');
$this->session->set_userdata();
//redirect to normal page
$this->load->view('home_page');
}
}
//logout
public function logout()
{
$result= $this->session->sess_destroy();
if ((isset($result)))
{
header("Location: ".base_url() );
}
else
{
}
}
In Model
public function validate_user($user,$pass)
{
$query = $this->db->query("SELECT * FROM user WHERE username= '$user' AND password='$pass'");
$result = $query->result_array();
$count = count($result);
return $count;
}
modify this line of changes then your script will work as id_user need to set first to use in script.
if($pass === $login['password'] && $user===$login['username']){
$this->session->set_userdata(array(
'id_user' => $login['id_user'],
));//we create the session 'user_id'
}
here $login['password'] and $login['username'] are data come from tables and need to change fieldname pass or user according to your user table.
Hi i have created a session in Codeigniter i have set the session variable in my model. But after setting the session variable value when i call session variable in my view through Controller the session variable value becomes null. Any help???
Update
This is my Model where i set my session variable
function login ($username, $password)
{
$this->db->where('username' , $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if ($query->num_rows()>0)
{
foreach ($query->result() as $rows)
{
$data = array(
'user_name' => $rows->username,
'logged_in' => TRUE,
'validated' => true
);
}
$this->session->set_userdata($data);
//$user = $rows->username;
//$this->session->set_userdata('user_name', $user);
return true;
}
else
{
return false;
}
}
Here is my controller from where i redirect to the view
public function verification()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->site_model->login($username, $password);
//$result = $this->session->set_userdata('validated');
if ($result)
{
//$this->admin();
//$this->session->set_userdata('login_state', TRUE);
redirect ('site/index');
}
else
{
redirect ('site/login');
//$this->load->view('login');
}
}
i have called the session_start(); in controller under construct();
and this is how i access the session variable in my view
<?php if ($this->session->userdata('user_name') != "") { ?>
.....
First of All in CI you don't need to use session_start() anywhere. only autoload session in config/autoload.php file.
Remove that session_start().
Try to use flashdata. Flashdata is temporary session mostly used in your type of case where We need to redirect user to another page and display some success or error message.
Set flashdata:
$this->session->set_flashdata('item', 'value');
get flashdata:
$this->session->flashdata('item');
Here is the documentation link http://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata
use autoload applications/config/autoload.php, to add session liblary and remove session_start();
$autoload['libraries'] = array('session');
Model pass post data vithout varibles, you can do like that:
controller:
public function verification()
{
$result = $this->site_model->login();
if ($result){
redirect ('site/index');
} else {
redirect ('site/login');
}
}
Model:
public function login()
{
$this->db->where('username' , $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
if ($query->num_rows()>0)
{
$res = $query->row_array();
$data = array(
'user_name' => $res['username'],
'logged_in' => TRUE,
'validated' => true
);
$this->session->set_userdata($data);
return true;
} else {
return false;
}
}
set the session variable in controller..this works for me nice
model
function Login($email,$pass){
$this->db->where('username' ,$email);
$this->db->where('password',$pass);
$query=$this->db->get('users');
if ($query->num_rows == 1) {
return true;
}
return FALSE;
}
Controller :
function varification(){
$this->load->model('login_model');
$email=$this->input->post('email');
$pass=$this->input->post('pass');
$success=$this->login_model->Login($email,$pass);
if ($success) {
$data = array(
'user_name' => $rows->username,
'logged_in' => TRUE,
'validated' => true
);
$this->session->set_userdata($data);
redirect ('site/index');
} else{ // incorrect id or password
redirect ('site/login');
}
}
now if you echo <?php echo $this->session->userdata('user_name');?> it will show the session username. you can echo it anywhere even in the view.hope this will help you
You don't need to use the core php session_start();
Make Sure you Have loaded session library in autoload.php
as:
$autoload['libraries'] = array("session");
or you can load session library manually : as
$this->load->library('session');
For Full Documentation You can refer : Read Documentation
please use this code to set session in codeigniter
$this->session->set_userdata('user_session', $user_session_data);
I was having the same problem and it got resolved using this code.Hope it works for you too!.
Setting session variables in Model, is not a good idea for any CMV framework (like codeigniter). Edit your Model's login() function so it returns a nice set of infos ...
function login ($username, $password)
{
$this->db->where('username' , $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if ($query->num_rows()>0)
{
foreach ($query->result() as $rows)
{
$data = array(
'user_name' => $rows->username,
'logged_in' => TRUE,
'validated' => true
);
}
return $data;
}
else
{
return false;
}
}
... and handle the session from the Controller.
public function verification()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->site_model->login($username, $password);
if ($result == false)
{
redirect ('site/login');
//$this->load->view('login');
}
else
{
$this->session->set_userdata($result);
redirect ('site/index');
}
}
Also, in order to have session's data available to the view file, you need to pass them through your controller
$data['user_name'] = $this->session->userdata('user_name');
and then you can do whatever in your view file:
<?php if ($user_name != "") { ?>
Hello i think you need to change your model code
You are setting array in session and accessing direct user_name in session
I think below code will work for you,
function login ($username, $password)
{
$this->db->where('username' , $username);
$this->db->where('password', $password);
$query = $this->db->get('users')->row();
if (count($query) > 0)
{
$this->session->set_userdata('user_name', $query->username);
$this->session->set_userdata('logged_in', true);
$this->session->set_userdata('validated', true);
return true;
}
else
{
return false;
}
}
please follow below instructions.
make sure you have addedd session lib in autoload.php file so session will automatically loaded.
When you are using session in codigniter you don't have add session_start() at the start of the file because codigniter core system will handled this automatically.
It is good idea to set session variables in controller instead of model.
I hope this helps you in solving your problem.
thanks.
Here is my solution, hope will help.. :D
in your model.. i copy paste code bellow but i change a little..
function login ($username, $password)
{
$this->db->where('username' , $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if ($query->result())//if any record
{
$rows = $query->row();
$data = array(
'user_name' => $rows->username,
'logged_in' => TRUE,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
else
{
return false;
}
}
delete your session_start() in controller,, and then in your application/config/autoload.php
in this $autoload['libraries'] = array(); => add session like this
$autoload['libraries'] = array('session');
Thankyou.. :D
user default php session, like session_start() and $_SESSION, it is working for me. in php codeignatore
I'm new to codeigniter and php, few days only, so I need a little help.
I'm trying to put some data in my cookie from table so I can check where to redirect user after login. In table users there are two columns named Admin and Company with one or zero if user is or not, and then i wish to insert that information to cookie.
function conformation in user_controler is:
function conformation(){
$this->load->model('user');
$q = $this->user->confr();
if($q){
$data = array(
'username' => $this->input->post('username'),
'Admin' => $this->input->post($a = $this->user->getAdmin), // get 1/0 from users column Admin
'Company' => $this->input->post($c = $this->user->getComp),
'login' => true
);
if( $a == 1 ){ //is admin redirect to admin view
$this->session->set_userdata($data);
redirect('user_controler/useradm');
}
if($c == 1){ //if company redirect to company view
$this->session->set_userdata($data);
redirect('user_controler/usercomp');
}
$this->session->set_userdata($data);// if common user redirect to user view
redirect('user_controler/userpro');
}
else{ // if nothing above redirect to login page
redirect('user_controler/log');
}
}
And in user model:
function getAdmin{
$this->db->where('Admin', 1);
$a = $this->db->get('users');
}
function getComp{
$this->db->where('Company', 1);
$a = $this->db->get('users');
}
function conf(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$q = $this->db->get('users');
if($q->num_rows == 1 ){
return TRUE;
}
}
Also have site controller for checking login
class Site extends CI_Controller{
function __construct() {
parent::__construct();
$this->login();
}
function login(){
$login = $this->session->userdata('login');
if(!isset($login) || login != TRUE){
$this->log;
die();
}
}
}
Of course it's not working because i should probably check these column some other way but I don't know how. I Also have enabled table ci_session and it's work perfectly without Admin and Company.
Hello and welcome to Stackoverflow.
Here are my updates to the code (I have annotated my changes):
function conformation(){
$this->load->model('user');
if($this->user->confr()){ //$q wasn't needed, as you are only using this twice
$user = $this->input->post('username'); //I have added this as I will be referring to it a couple of times.
$data = array(
'username' => $user,
'Admin' => $this->user->getAdmin($user), // Your method was questioning the original form looking for data that it would never find - This will question your model.
'Company' => $this->user->getComp($user), //Same as above
'login' => true
);
$this->session->set_userdata($data); //It doesn't matter who the user is, we shall set the data to start with.
if($this->user->getAdmin($user)){ //is admin redirect to admin view
redirect('user_controler/useradm');
}
elseif($this->user->getComp($user)){ //if company redirect to company view
redirect('user_controler/usercomp');
}
else { //Redirect non-privileged users.
redirect('user_controler/userpro');
}
}
else{ // if nothing above redirect to login page
redirect('user_controler/log');
}
}
Users Model:
function getAdmin($user){
$this->db->where('username', $user); //Before you was just returning everyone who is an admin This instead finds the user
$a = $this->db->get('users');
foreach($a as $u) {
if($u["Admin"]==1) { return true; } //This finds if the user is a admin or not, and the function will now return a value (true)
}
}
function getComp($user) {
$this->db->where('username', $user);
$a = $this->db->get('users');
foreach($a as $u) {
if($u["Company"]==1) { return true; }
}
} //Edited similar to the function above
function conf(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$q = $this->db->get('users');
if($q->num_rows == 1 ){
return TRUE;
}
}
Lastly your login function:
function login(){
$login = $this->session->userdata('login');
if(!isset($login) || $login != TRUE){ //You weren't referring to your $login variable
$this->log;
die();
}
}
Hopefully this helps with your problems, let me know if you need any amendments.
Trying to put together a simple login authentication. Been at this for quite sometime, and I can't find where I'm going wrong. Pretty new to Codeigniter and OOP PHP. I know there are authentication libraries out there, but I'm not interested in using those.
Model:
function login($username, $password){
if ($this->db->table_exists($username)){
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get($username);
if($query->num_rows >= 1)
{
return true;
$data = array(
'username' => $this->input->post('username'),
'login' => true
);
$this->session->set_userdata($data);
}
}
}
Controller
function __construct(){
parent::__construct();
$this->logincheck();
}
public function logincheck(){
if ($this->session->userdata('login')){
redirect('/members');
}
}
If I just echo from the controller: $this->session->all_userdata(); I get an empty array. So the problem seems to be that the $data array in the model isn't being stored in the session.
This code is part of your problem. You're asking the database driver if a table with the same name as the username exists, and also trying to get the username and password from the same table.
if ($this->db->table_exists($username)){
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get($username);
Change the $username in $this->db->table_exists($username); and $this->db->get($username); to whatever table your users are in.
I am guessing your login function is the issue:
if ($this->db->table_exists($username)){
This just doesn't look right... If your user name was admin, there there would need to be an admin table, etc... I am posting a quick example I recently wrote that seems to get the job done:
public function doLogin(){
$this->db->select('user_pass,user_id');
$query = $this->db->get_where('users', array('user_name' => $this->input->post('login')));
if($query->num_rows<1 || $query->row('user_pass')!=md5($this->input->post('password')))
return false;
$this->session->set_userdata(array('loggedin'=>true,'user_id'=>$query->row('user_id')));
return true;
}