Forgot password function not working in CodeIgniter - php

Good day! I'm trying to make a forgot password function in the CodeIgniter framework but I'm getting 2 errors when i try to send the e-mail.
Some database info (I'm using phpMyAdmin):
Db name: kadokado
Db table name: users
Db email column: email
Db password column: wachtwoord
My controller file (Auth.php) :
<?php
class Auth extends CI_Controller{
public function forgot()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('forgot');
$this->load->view('templates/footer');
}else{
$email = $this->input->post('email');
$clean = $this->security->xss_clean($email);
$userInfo = $this->user_model->getUserInfoByEmail($clean);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'We hebben dit email adres niet kunnen vinden');
redirect(site_url().'auth/login');
}
if($userInfo->status != $this->status[1]){ //if status is not approved
$this->session->set_flashdata('flash_message', 'Your account is not in approved status');
redirect(site_url().'auth/login');
}
//build token
$token = $this->user_model->insertToken($userInfo->id);
$qstring = $this->base64url_encode($token);
$url = site_url() . 'auth/reset_password/token/' . $qstring;
$link = '' . $url . '';
$message = '';
$message .= '<strong>A password reset has been requested for this email account</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
echo $message; //send this through mail
exit;
}
}
public function reset_password()
{
$token = $this->base64url_decode($this->uri->segment(4));
$cleanToken = $this->security->xss_clean($token);
$user_info = $this->user_model->isTokenValid($cleanToken); //either false or array();
if(!$user_info){
$this->session->set_flashdata('flash_message', 'Token is invalid or expired');
redirect(site_url().'auth/login');
}
$data = array(
'voornaam'=> $user_info->voornaam,
'email'=>$user_info->email,
'token'=>base64_encode($token)
);
$this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[wachtwoord]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('reset_password', $data);
$this->load->view('templates/footer');
}else{
$this->load->library('wachtwoord');
$post = $this->input->post(NULL, TRUE);
$cleanPost = $this->security->xss_clean($post);
$hashed = $this->password->create_hash($cleanPost['wachtwoord']);
$cleanPost['wachtwoord'] = $hashed;
$cleanPost['user_id'] = $user_info->id;
unset($cleanPost['passconf']);
if(!$this->user_model->updatePassword($cleanPost)){
$this->session->set_flashdata('flash_message', 'Er is iets foutgegaan');
}else{
$this->session->set_flashdata('flash_message', 'Uw wachtwoord is geupdate, u kunt nu inloggen');
}
redirect(site_url().'auth/login');
}
}
}
My model file (User_Model.php) :
<?php
class user_model extends CI_model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function insertToken($user_id)
{
$token = substr(sha1(rand()), 0, 30);
$date = date('Y-m-d');
$string = array(
'token'=> $token,
'user_id'=>$user_id,
'created'=>$date
);
$query = $this->db->insert_string('tokens',$string);
$this->db->query($query);
return $token . $user_id;
}
public function isTokenValid($token)
{
$tkn = substr($token,0,30);
$uid = substr($token,30);
$q = $this->db->get_where('tokens', array(
'tokens.token' => $tkn,
'tokens.user_id' => $uid), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
$created = $row->created;
$createdTS = strtotime($created);
$today = date('Y-m-d');
$todayTS = strtotime($today);
if($createdTS != $todayTS){
return false;
}
$user_info = $this->getUserInfo($row->user_id);
return $user_info;
}else{
return false;
}
}
}
?>
My view file (reset_password.php) :
<div class="col-lg-4 col-lg-offset-4">
<h2>Reset your password</h2>
<h5>Hello <span><?php echo $firstName; ?></span>, Voer uw wachtwoord 2x in aub</h5>
<?php
$fattr = array('class' => 'form-signin');
echo form_open(site_url().'auth/reset_password/token/'.$token, $fattr); ?>
<div class="form-group">
<?php echo form_password(array('name'=>'wachtwoord', 'id'=> 'wachtwoord', 'placeholder'=>'Wachtwoord', 'class'=>'form-control', 'value' => set_value('wachtwoord'))); ?>
<?php echo form_error('password') ?>
</div>
<div class="form-group">
<?php echo form_password(array('name'=>'passconf', 'id'=> 'passconf', 'placeholder'=>'Confirm Password', 'class'=>'form-control', 'value'=> set_value('passconf'))); ?>
<?php echo form_error('passconf') ?>
</div>
<?php echo form_hidden('user_id', $user_id);?>
<?php echo form_submit(array('value'=>'Reset Password', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
<?php echo form_close(); ?>
</div>
And these are the errors I'm getting:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$user_model
Filename: controllers/Auth.php
Line Number: 123
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 123
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
2nd error:
A PHP Error was encountered
Severity: Error
Message: Call to a member function getUserInfoByEmail() on a non-object
Filename: controllers/Auth.php
Line Number: 123
Backtrace:
I have absolutely no clue what I'm doing wrong and I hope someone can help me.
Thanks!

Load user model in auth controller. You can load it in constructor or in the function.
class Auth extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('user_model'); // load user model
}
public function forgot(){
// your code
}
In Function
class Auth extends CI_Controller{
public function forgot(){
$this->load->model('user_model'); // load user model
// your code
}
Not tested

You need to make sure that the user_model class is loaded from the controller. Like so:
class Auth extends CI_Controller {
function __construct() {
$this->load->model('user_model');
}
}
And be sure that you have the spelling/capitalization correct in the model class.
class User_Model extends CI_Model {
// rest of code
}

#frodo again.
First Error : in your controller code, you need to initialize model first than only you can use the model property.
public function forgot(){
// Changes required
$this->load->model('user_model');
$userInfo = $this->user_model->getUserInfoByEmail($clean);
}
Second Error :
if($userInfo->status != $this->status[1]){
$this->session->set_flashdata('flash_message', 'Your account is not in approved status');
redirect(site_url().'auth/login');
}
How you get the value of $this->status[1] variable. You can simply use if($userInfo->status != true).
Please change this code and let me know if you have any error.

Related

Codeigniter foreach loop error

I am getting a foreach loop error in codeigniter, it was working fine, but once I moved the project to a new hosting I get an error for the loop. Both servers are using PHP 7.2
Here is the first error:
Message: array_slice() expects parameter 1 to be array, null given
And here is the second error:
Message: Invalid argument supplied for foreach()
The backtrace says the error is in the controller, I have checked many times and tried many methods but still the same.
Using is_array PHP function wont help, as it only hide the error messages, but the functions wont work as before.
Controller:
public function login(){
$data['title'] = 'Sign In';
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header', $data);
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
} else {
// Get username
$username = $this->input->post('email');
// Get and encrypt the password
$password = $this->input->post('password');
// Login user
$user_id = $this->user_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'id' => $user_id,
'email' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
// Set message
$this->session->set_flashdata('user_loggedin', 'Login Success');
redirect('users/account');
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login Faild');
redirect('users/login');
}
}
}
public function account($id = NULL){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['users'] = $this->user_model->get_users($this->session->userdata('id'));
$data['title'] = 'Account';
$this->load->view('templates/user_header', $data);
$this->load->view('users/account', $data);
$this->load->view('templates/user_footer');
}
Loop code:
<?php foreach (array_slice($users, 0, 1) as $user): ?>
<div><p><?php echo $user['first_name'] ?></p></div>
<?php endforeach; ?>
Model:
public function get_users($id){
$this->db->join('user_details', 'user_details.user_details_id = users.id');
$this->db->join('user_orders', 'user_orders.user_id = users.id');
$query = $this->db->get_where('users', array('id' => $id));
return $query->row_array();
}
Rewrite the below code:
<?php foreach (array_slice($users, 0, 1) as $user): ?>
<div><p><?php echo $user['first_name'] ?></p></div>
<?php endforeach; ?>
as:
<div><p><?php echo $users['first_name'] ?></p></div>
Because:
public function get_users($id){
$this->db->join('user_details', 'user_details.user_details_id = users.id');
$this->db->join('user_orders', 'user_orders.user_id = users.id');
$query = $this->db->get_where('users', array('id' => $id));
return $query->row_array();
}
This code will not return an multi-dimension array... you have used row_array() which will be a single array.

CodeIgniter - Login is not working

I'm currently working with admin page. I logged in my username and password correctly but it says "404 Page Not Found - The page you requested was not found."
This is the controller:
public function login()
{
$header = array("title" => "Welcome - ");
$this->load->view('includes/header', $header);
$this->load->view('accounts/login');
$this->load->view('includes/footer');
}
public function login_submit()
{
$data = array(
'username' => $this->input->post('username'),
'password' => sha1($this->input->post('password'))
);
$this->form_validation->set_rules('username', 'Username', 'required|is_unique[accounts.username]|min_length[6]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
$accountDetails = $this->accounts_model->fetch('accounts', $data);
if (!$accountDetails) {
echo "<script>alert('Account does not exist!'); window.location.href = '".base_url()."accounts/login';</script>";
} else {
$accountDetails = $accountDetails[0];
if ($accountDetails->status == 1) {
$header = array("title" => "Account - ");
$this->load->view('includes/header', $header);
$this->load->view('admin/home', $accountDetails);
$this->load->view('includes/footer');
} else {
echo "<script>alert('You account is blocked!'); window.location.href = '".base_url()."accounts/login';</script>";
}
}
}
I hope this code will help you..Works fine
plz set your form like this:
<form action='<?php echo base_url();?>index.php/accounts/login_submit' method='post' name='process'>
accounts Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Pedram shabani
* Description: accounts controller class
*/
class accounts extends CI_Controller{
function __construct(){
parent::__construct();
}
public function login($msg = NULL)
{
$data['msg'] = $msg;
$this->load->helper('url');
$header = array("title" => "Welcome - ");
$this->load->view('includes/header', $header);
$this->load->view('accounts/login',$data);
$this->load->view('includes/footer');
}
public function login_submit()
{
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
$data = array(
'username' => $this->input->post('username'),
'password' => sha1($this->input->post('password'))
);
$this->load->model('accounts_model');
$accountDetails = $this->accounts_model->fetch($data);
if(! $accountDetails){
// If user did not validate, then show them login page again
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->login($msg);
}else{
$this->load->helper('url');
$header = array("title" => "Welcome - ");
$this->load->view('includes/header', $header);
$this->load->view('accounts/login',$data);
$this->load->view('includes/footer');
}
}
}
?>
accounts_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class accounts_model extends CI_Model{
function __construct(){
parent::__construct();
}
public function fetch ($data){
// die(var_dump($data));
$this->db->select('*');
$this->db->where('username', $data['username']);
$this->db->where('password', $data['password']);
$query = $this->db->get('users');
$num = $query->num_rows();
if($query->num_rows == 1)
{
$row = $query->row();
$data = array(
'userid' => $row->userid,
'fname' => $row->fname,
'lname' => $row->lname,
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
return false;
}
}
?>
and at the end.plz set default controller
$route['default_controller'] = 'login';

passing user information to view in codeigniter

I am trying to get users details from the database and put it in session so that it can be used in the view. I have tried all I can but I keep getting error. Undefined Variable Email.
MODEL:
function login($username, $password)
{
$this->db->select('authTbl.id, authTbl.password, authTbl.username, authTbl.email, authTbl.mobile');
$this->db->from('users as authTbl');
$this->db->where('authTbl.username', $username);
$this->db->where('authTbl.isDeleted', 0);
$query = $this->db->get();
$user = $query->result();
if(!empty($user)){
if(verifyHashedPassword($password, $user[0]->password)){
return $user;
} else {
return array();
}
} else {
return array();
}
}
CONTROLLER:
function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
$data['title'] = 'Login';
if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
{
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
else
{
redirect('posts');
}
}
/**
* This function used to logged in user
*/
public function login()
{
$this->load->library('form_validation');
$data['title'] = 'Login';
$this->form_validation->set_rules('username', 'Username', 'required|max_length[128]|trim');
//$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]|');
if($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
else
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->user_model->login($username, $password);
if(count($result) > 0)
{
foreach ($result as $res)
{
$sessionArray = array('id'=>$res->id,
'username'=>$res->username,
'email'=>$res->email,
'mobile'=>$res->mobile,
'isLoggedIn' => TRUE
);
$this->session->set_userdata($sessionArray);
$this->session->set_flashdata('user_login', 'Welcome');
redirect('posts');
}
}
else
{
$this->session->set_flashdata('login_fail', 'Email or password mismatch');
redirect('users/login');
}
}
}
VIEW:
<?php echo $email; ?>
You are creating the session for user information
Try to fetch the session data :
echo $this->session->userdata('email');
or trying to pass the data in views $data
$email will not work because writing $email means ordinary variable but in your case you have to write like :
<?php echo $this->session->userdata('email'); ?>
If You are using flashdata, then You can get it using below code:
Controller
if (empty($this->session->userdata('UserID'))) {
$this->session->set_flashdata('flash_data', 'You don\'t have access!');
$this->load->view("initialHeader");
$this->load->view("login");
redirect('Login');
}
View
<?php if(!empty($this->session->flashdata('flash_data'))) {
?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('flash_data'); ?>
</div>
<?php } ?>
If You are using tmp data as a session, please refer below code:
Controller
<?php
if (!empty($data)) {
$this->session->set_userdata('permission_error_msg', $data);
$this->session->mark_as_temp('permission_error_msg', 10); // For 10 Seconds
$this->load->view('dashboard', array($title, $data));
} ?>
View
<?php if ($this->session->tempdata('permission_error_msg')) { ?>
<b class="login_error_msg"><?php
if (!empty($this->session->tempdata('permission_error_msg'))) {
echo $this->session->tempdata('permission_error_msg');
}
?></b>
<?php } ?>
Thank You.

I made a CodeIgniter forgot password function but its not working

Good day! I'm making a forgot password function on the CodeIgniter PHP framework but it doesn't really work. When I click on the send button no email is send.
Some db info:
db name: kadokado
db table: users
db email column: email
db password column: wachtwoord
db id column: user_id
My controller (Auth.php) :
public function forgot()
{
$this->load->model('User_model'); // load user model
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('forgot');
$this->load->view('templates/footer');
}else{
$email = $this->input->post('email');
$clean = $this->security->xss_clean($email);
$userInfo = $this->User_model->getUserInfoByEmail($clean);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'We hebben dit email adres niet kunnen vinden');
redirect(site_url().'auth/login');
}
if($userInfo->status != true){ //if status is not approved
$this->session->set_flashdata('flash_message', 'Your account is not in approved status');
redirect(site_url().'auth/login');
}
//build token
$token = $this->User_model->insertToken($userInfo->id);
$qstring = $this->base64url_encode($token);
$url = site_url() . 'auth/reset_password/token/' . $qstring;
$link = '' . $url . '';
$message = '';
$message .= '<strong>A password reset has been requested for this email account</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
echo $message; //send this through mail
exit;
}
}
public function reset_password()
{
$token = $this->base64url_decode($this->uri->segment(4));
$cleanToken = $this->security->xss_clean($token);
$user_info = $this->User_model->isTokenValid($cleanToken); //either false or array();
if(!$user_info){
$this->session->set_flashdata('flash_message', 'Token is invalid or expired');
redirect(site_url().'auth/login');
}
$data = array(
'voornaam'=> $user_info->voornaam,
'email'=>$user_info->email,
'token'=>base64_encode($token)
);
$this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[wachtwoord]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('reset_password', $data);
$this->load->view('templates/footer');
}else{
$this->load->library('wachtwoord');
$post = $this->input->post(NULL, TRUE);
$cleanPost = $this->security->xss_clean($post);
$hashed = $this->password->create_hash($cleanPost['wachtwoord']);
$cleanPost['wachtwoord'] = $hashed;
$cleanPost['user_id'] = $user_info->id;
unset($cleanPost['passconf']);
if(!$this->User_model->updatePassword($cleanPost)){
$this->session->set_flashdata('flash_message', 'Er is iets foutgegaan');
}else{
$this->session->set_flashdata('flash_message', 'Uw wachtwoord is geupdate, u kunt nu inloggen');
}
redirect(site_url().'auth/login');
}
}
My model (User_model.php) :
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function insertToken($user_id)
{
$token = substr(sha1(rand()), 0, 30);
$date = date('Y-m-d');
$string = array(
'token'=> $token,
'user_id'=>$user_id,
'created'=>$date
);
$query = $this->db->insert_string('tokens',$string);
$this->db->query($query);
return $token . $user_id;
}
public function isTokenValid($token)
{
$tkn = substr($token,0,30);
$uid = substr($token,30);
$q = $this->db->get_where('tokens', array(
'tokens.token' => $tkn,
'tokens.user_id' => $uid), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
$created = $row->created;
$createdTS = strtotime($created);
$today = date('Y-m-d');
$todayTS = strtotime($today);
if($createdTS != $todayTS){
return false;
}
$user_info = $this->getUserInfo($row->user_id);
return $user_info;
}else{
return false;
}
}
}
?>
My view (reset_password.php) :
<?php include_once ('templates/header.php'); ?>
<?php include_once ('templates/sidebar2.php'); ?>
<div class="col-lg-4 col-lg-offset-4">
<h2>Reset your password</h2>
<h5>Hello <span><?php echo $voornaam; ?></span>, Voer uw wachtwoord 2x in aub</h5>
<?php
$fattr = array('class' => 'form-signin');
echo form_open(site_url().'auth/reset_password/token/'.$token, $fattr); ?>
<div class="form-group">
<?php echo form_password(array('name'=>'wachtwoord', 'id'=> 'wachtwoord', 'placeholder'=>'Wachtwoord', 'class'=>'form-control', 'value' => set_value('wachtwoord'))); ?>
<?php echo form_error('password') ?>
</div>
<div class="form-group">
<?php echo form_password(array('name'=>'passconf', 'id'=> 'passconf', 'placeholder'=>'Confirm Password', 'class'=>'form-control', 'value'=> set_value('passconf'))); ?>
<?php echo form_error('passconf') ?>
</div>
<?php echo form_hidden('user_id', $user_id);?>
<?php echo form_submit(array('value'=>'Reset Password', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
<?php echo form_close(); ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
I hope someone can help me out!

Using multiple controllers in Codeigniter : cannot send session cache limiter

When I use only 1 controller in my application, user, everything works fine. When I try to use 2 or 3 to divide the code and create some structure my application always give the following error :
A PHP Error was encountered
Severity: Warning
Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php:99)
Filename: Session/Session.php
Line Number: 140
Backtrace:
File: /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php
Line: 11
Function: __construct
File: /Applications/MAMP/htdocs/BT_dashboard/index.php
Line: 292
Function: require_once
Googled it and tried many things but can't find an answer. I'm using codeigniter 3. My User controller looks like :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* User class.
*
* #extends CI_Controller
*/
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
$this->load->model('project_model');
}
public function createProject() {
if ($_SESSION['userlevel']) {
if ($_SESSION['userlevel'] < 3) {
$userid = $this->uri->segment(3);
} else {
$userid = $this->input->post('userproject');
}
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('project_name', 'project name', 'trim|required|min_length[2]|callback_is_project_name_unique[' . $this->input->post('project_name') . ']');
$this->form_validation->set_rules('project_address', 'project address', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_description', 'project description', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_city', 'project city', 'trim|required|min_length[2]');
if ($this->form_validation->run() == FALSE) {
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
if ($_SESSION['userlevel'] < 3) {
$this->load->view('dashboard_add_project', $data);
} else {
$data['userslist'] = $this->user_model->get_users_list();
$this->load->view('dashboard_add_project_admin', $data);
}
$this->load->view('wrapper', $data);
} else {
$Address = urlencode($this->input->post('project_address'));
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" . $Address . "&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status == "OK") {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = "$Lat,$Lon";
}
//pass validation
$data = array(
'project_name' => $this->input->post('project_name'),
'project_address' => $this->input->post('project_address'),
'project_description' => $this->input->post('project_description'),
'project_city' => $this->input->post('project_city'),
'project_finished' => $this->input->post('project_finished'),
'lat' => $Lat,
'lng' => $Lon,
);
//$this->db->insert('tbl_user', $data);
if ($this->user_model->create_project($data, $userid, $this->input->post('project_name'))) {
if ($_SESSION['userlevel'] > 1) {
$data['projectlist'] = $this->user_model->get_project_list();
$data['uncompleted_projects'] = $this->user_model->get_uncompleted_projects();
$data['completed_projects'] = $this->user_model->get_completed_projects();
} else {
$data['projectlist'] = $this->user_model->get_project_list_userid($userid);
}
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
$this->load->view('dashboard_projects', $data);
$this->load->view('wrapper', $data);
} else {
$data->error = 'There was a problem creating your new employee. Please try again.';
$data['userdata'] = $this->session->userdata;
// send error to the view
$this->load->view('header', $data);
$this->load->view('dashboard_add_project', $data);
$this->load->view('wrapper', $data);
}
}
}
}
My second controller, the project controller. This is the controller I want to use know to paste the createproject code so this gives more structure. But when I paste it here and adapt my views it gives the error. here's my project_controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* File Name: employee.php
*/
class Project extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
}
public function createProject() {
//same as usercontroller
My User/login code in User controller :
public function login() {
$loggedin = $this->session->userdata('logged_in');
if (!$loggedin) {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('user_mail', 'user mail', 'required');
$this->form_validation->set_rules('user_password', 'user password', 'required');
if ($this->form_validation->run() == false) {
$data = new stdClass();
$data->error = 'Check your user and password';
$this->load->view('dashboard_login', $data);
} else {
$usermail = $this->input->post('user_mail');
$password = $this->input->post('user_password');
if ($this->user_model->resolve_user_login($usermail, $password)) {
$user_id = $this->user_model->get_user_id_from_mail($usermail);
$user = $this->user_model->get_user($user_id);
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = (string) $user->user_name;
$_SESSION['logged_in'] = (bool) true;
$_SESSION['user_gsm'] = (string) $user->user_gsm;
$_SESSION['user_address'] = (string) $user->user_address;
$_SESSION['user_city'] = (string) $user->user_city;
$_SESSION['userlevel'] = $this->user_model->get_user_level((int) $user->user_id);
$_SESSION['user_mail'] = $usermail;
$data['userdata'] = $this->session->userdata;
if ($_SESSION['userlevel'] == "3") {
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
}
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
} else {
$data = new stdClass();
// login failed
$data->error = 'Wrong username or password.';
// send error to the view
$this->load->view('dashboard_login', $data);
}
}
} else {
$data['userdata'] = $this->session->userdata;
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
}
}
In config/autoload I've got following:
$autoload['libraries'] = array('database','session');
So, what is on line 99 in your Project.php controller?

Categories