Why set_userdata() doesn't work? - php

I'm Using codeigniter framework and i'm having a problem with a session.. I was trying to create login page, but there was a problem when I used set_userdata().
It doesn't work. When I checked, there was no session created.
Is there any wrong with my code?? Any one has same problem with it??
function __construct(){
$this->CI=&get_instance();
$auth=$this->CI->config->item('auth');
$this->CI->load->helper('cookie');
$this->CI->load->library('session');
$this->CI->load->model('Auth_users_model');
}
function login($username,$password){
$result=$this->CI->Auth_users_model->get_login_info($username);
if($result){
$password=md5($password);
if($password==$result->password){
$user_ses=array('user_id_hcp'=>$result->id,'user_access_hcp'=>$result->access, 'user_username_hcp'=>$username);
$this->CI->session->set_userdata($user_ses);
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
}

Instead of $this->CI->session->set_userdata($user_ses);
try using simply
$this->session->set_userdata($user_ses);

Related

Redirect after a DB update

So, I'm using this php function to update a database in a site.
public function ativo($id, $ativo) {
if ($ativo == 0) {
$data['ATIVO'] = 1;
}
if ($ativo == 1) {
$data['ATIVO'] = 0;
}
$this->db->where('CLIENTE.idCLIENTE', $id);
$this->db->update('CLIENTE', $data);
header("Location: -imagine-a-web-address-here");
}
The problem is, I keep getting a blank page after the db update, so the header() isn't really helping me.
Is there any problem with the code or other function to redirect to the address? Thx for the help.
What is the error you got?
Try this;
header('location:'.$_SERVER['HTTP_REFERER']); or redirect($_SERVER['HTTP_REFERER']);

comparing hash password laravel 4

I'm trying to make a custom login function in Laravel 4 but I don't know how to compare password with hash password that has been stored.
This is my controller
public function login($email,$password)
{
$user=UserMobile::where('email','=',$email)->count();
if($user==0)
{
return Response::json("Invalid");
}
else
{
$user=UserMobile::where('email','=',$email)->first();
if(Hash::check($password,$user->password))
{
return Response::json($user);
}
else
{
return Response::json("Error");
}
}
}
When I try to check it in postman, it keeps returning the return Response::json("Error");. Can somebody help please? thanks in advance

how to get the error message when query is update or not codeigniter

how to get the error message when query is update or not codeigniter
I am using codeigniter library please help
I don't care about the affect row's
$ci = &get_instance();
$ci->db->where('activity_id',$activity_id);
$ci->db->update('activity',$activity_data);
if(error){
return false / or error message ;
}else{
return true;
}
You can simply know, whether your data is entered in database or not.
Model
$row = $this->db->affected_rows()
if($row ){
return true;
}else{
return false;
}
Then in controller check condition and then you can set the flash data and show in the view.
$this->session->set_flashdata('err_message', 'Data not updated successfully.');
remember that you need to load the session library before use it.
View
<?php
if($this->session->flashdata('err_message')){
?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('err_message'); ?>
</div>
and you can then return the $row that may be have value or empty. Hope this will help you.
You can add it into variable first
try this
$result = $ci->db->update('activity',$activity_data);
if($result){
return true;
}else{
return $this->db->_error_message();
}
try this
if (!(bool)$ci->db->where('activity_id',$activity_id)->update('activity',$activity_data))
{
//your error
}

YII scenario: how to?

What If I have an input field for example:
UserEmail:
UserPhoneNumber:
UserOldPasword:
UserNewPassword:
UserRetypePassword:
And on my action update I will only require UserNewPassword if UserOldPassword is not empty else the only thing that needs to be updated is either UserEmail or UserPhoneNumber. How will I implement this rule?only on update? Or should I create a custom validator on my model?
so on my afterFind() I have this code to avoid the output of the hashed password:
public function afterFind()
{
//reset the password to null because we don't want the hash to be shown.
$this->old_password = $this->password;
$this->password = null;
parent::afterFind();
}
and I created a custom validation. it does validates, the problem is even if I submitted the form with empty UserNewPassword I still get an error and $this->password field is now returning a hashed value from my database
Please see my code and correct my mistakes:
public function checkForEmpty($attribute,$params){
if(!empty($this->$attribute)){
if(empty($params['oldPassword'])){
$this->addError('oldPassword','We require your old password to proceed changing password');
}
if(empty($params['retypePassword'])){
$this->addError('retype_password','To assure that you type the right password please retype your password');
}
if(!empty($params['oldPassword']) && !empty($params['retypePassword'])){
$this->compareOldPass($params['oldPassword'],$this->old_password);
}
}
}
Thanks in advance...
you can do a beforeSave() in your model. I think it's the best solution because of your complicated logic, and not being a global issue of your app.
UPDATE:
public function beforeSave()
{
if(parent::beforeSave())
{
//implement you logic here
//or check it is a new record
if($this->isNewRecord)
{
// do stuff here if new
}
//or you can return false if it doesn't meet your logic
return true;
}
else
return false;
}

Little Active record views updating

I need little Active record help. I am trying to update page views which i store in database. Each time post() function runs, add +1 . Here is my code:
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
Please help!
Correct your code like this for example:
public function post($id) {
if(!isset($id) || (int) $id<=0){
header('Location: /blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
}
Mark the header('Location: /blog') - you should put here your real server path to the blog.
If you use a custom redirection function, like your redirect() then check if the is an output before calling her. Header redirection only works if no other output is sent to the browser before invoking it.
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1',FALSE);
$this->db->update('entry');
FALSE turns off Active record escaping,by default it ignores +1.

Categories