Fatal error: Call to undefined method on specific line - php

Codeigniter throw the next error when trying to render the register page. It looks like the method Register::render() is not defined on line 21 but i don't understand the problem at all
The Register.php file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Register extends MY_Controller
{
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First name','trim|required');
$this->form_validation->set_rules('last_name', 'Last name','trim|required');
$this->form_validation->set_rules('username','Username','trim|required|is_unique[users.username]');
$this->form_validation->set_rules('email','Email','trim|valid_email|required');
$this->form_validation->set_rules('password','Password','trim|min_length[8]|max_length[20]|required');
$this->form_validation->set_rules('confirm_password','Confirm password','trim|matches[password]|required');
if($this->form_validation->run()===FALSE)
{
$this->load->helper('form');
$this->render('register/index_view');
}
else
{
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $first_name,
'last_name' => $last_name
);
$this->load->library('ion_auth');
if($this->ion_auth->register($username,$password,$email,$additional_data))
{
$_SESSION['auth_message'] = 'The account has been created. You may now login.';
$this->session->mark_as_flash('auth_message');
redirect('user/login');
}
else
{
$_SESSION['auth_message'] = $this->ion_auth->errors();
$this->session->mark_as_flash('auth_message');
redirect('register');
}
}
}
}

You should use:
$this->load->view('register/index_view');
When I started with codeigniter I had a similar issue. Render() is used by some libraries and, like you, I followed a tutorial, but there was nothing about that library. After a while I saw some comments in that article and I found out about render(). If you want to use this, try to find some library that has it implemented.

Related

PHP login using Ion Auth and Facebook login works locally on MAMP but not Web Server

The problem I'm facing is that manual new user subscription works locally but does not work on remote web server. The only difference between the non-working one and the one that works is that I upgraded Ion Auth to version 2 and added Facebook-Ion Auth library for Facebook Login.
Again, it works perfect locally, but not on the web server. PHP versions have been tested and work fine.
Here's the controller it gets stuck on (shows a blank page or goes back to homepage).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends CI_Controller {
function __construct()
{
parent::__construct();
$this->CI =& get_instance();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->library('forms/signupform');
$this->form_validation->set_error_delimiters(
$this->config->item('error_start_delimiter', 'ion_auth'),
$this->config->item('error_end_delimiter', 'ion_auth')
);
}
/**
* #signup page of freelancer
*/
public function index()
{
$data['title'] = lang('title_registration');
$data['bodyclass'] = 'hold-transition register-page';
$data['js_bottom_files'] = array('plugins/iCheck/icheck.min', 'js/custom');
$data['cssfiles'] = array('plugins/iCheck/square/blue');
// POST SIGNUP FORM
if ('POST' == $this->input->server('REQUEST_METHOD') && $this->input->post('registersubmit') ) {
// Signup Action
$data['message_success'] = $this->signup();
}
// $user['checked'] = '';
// if ($this->input->post('agree') == 'yes') {
// $user['checked'] = 'checked';
// }
$data['form'] = $this->signupform->view($user);
//Render Or redirect according to User AccessLevel
if (!$this->ion_auth->logged_in()) {
$this->template->load('layout', 'home', $data);
} elseif ($this->ion_auth->is_admin()) {
redirect(site_url('admin/dashboard'), 'refresh');
} elseif ($this->ion_auth->is_members()) {
redirect(site_url('note/create'), 'refresh');
}
}
/**
* #Signup action for user
*/
public function signup()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
// $this->form_validation->set_rules('full_name', lang('label_full_name'), 'required');
$this->form_validation->set_rules('loginPassword', lang('label_signup_createpassword'), 'required|min_length['.$this->config->item('min_password_length', 'ion_auth').']|max_length['.$this->config->item('max_password_length', 'ion_auth').']');
// $this->form_validation->set_rules('confirmpassword', lang('label_signup_confirmpassword'), 'required');
// $this->form_validation->set_rules('agree', 'Agree', 'required');
if ($this->form_validation->run() == true) {
$email = $this->input->post('email');
$password = $this->input->post('loginPassword');
// $additional_data = array('first_name' => $this->input->post('full_name'), 'school' => $this->input->post('school_name'));
$group_ids = array( 'user_groups' => 2);
if ($this->ion_auth->register($email, $password, $email, $additional_data, $group_ids)) {
//check to see if we are creating the user
//$this->session->set_flashdata('message_success', $this->ion_auth->messages());
//redirect(site_url('/'), 'refresh');
if($this->ion_auth->login($email, $password)){
redirect(site_url('note/create'), 'refresh');
}
} else {
$this->session->set_flashdata('message_error', $this->ion_auth->errors());
redirect(site_url('/'), 'refresh');
}
}
}
}
/* End of file signup.php */
Potentially not the issue without more information but:
$group_ids = array( 'user_groups' => 2); should just be $group_ids = array('2')
See usage of the register function here.
and in the Ion auth config file $config['identity'] should be set as email (if you haven't done so already).

How to merge session data into a function using CodeIgniter?

I've asked this subject in another topic but that topic has gone cold and I've directed this topic to the core of the problem.
I've loaded the session library in the previous page and have no issue with the function working in that page.
But it is the next page where I'm having problems. I get the error "Unexpected T_VARIABLE".
I've read the topic on How to solve syntax errors. That topic suggests the line before is usually the problem line, usually by a missing semicolon or bracket.
This is the coding;
public function index()
{
$this->load->model('Code_model', 'code_model');
$this->session->email //This is the problem line
$email = $this->session->email
$code = $this->input->post('code');
if ($this->code_model->find_code($email, $code))
{
$this->load->view('username');
}
else
{
$this->load->view('codeincorrect');
}
}
I've tried putting a semicolon at the end. And tried adding - userdata('email');
And tried having a separate function containing the problem line with its own brackets. And tried deleting the problem line & the line below. When deleted $email cannot be found.
But nothing works.
Is there somebody who understands how sessions work and how they can be integrated into a function?
Update
This is the Controller coding of the previous page, which works good.
function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function index()
{
$this->load->model('Email_model', 'email_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'required|min_length[10]|max_length[40]|valid_email|is_unique[tbl_members.email_address]', array(
'required' => 'You have not entered an %s address.', 'min_length' => 'Your %s address must be a minimum of 10 characters.',
'max_length' => 'Your %s address must be a maximum of 40 characters.', 'valid_email' => 'You must enter a valid %s address.',
'is_unique' => 'That %s address already exists in our Database.'));
if ($this->form_validation->run() == FALSE) // The email address does not exist.
{
$this->load->view('email');
}
else
{
$email = $this->input->post('email');
$random_string = chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . chr(rand(65,90));
$code = $random_string;
$this->email_model->insert_email($email, $code);
}
}
2nd Update - This is the coding for the 2 Controllers and 2 Models
Email Controller
class Email extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function index()
{
$this->load->model('Email_model', 'email_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'required|min_length[10]|max_length[40]|valid_email|is_unique[tbl_members.email_address]', array(
'required' => 'You have not entered an %s address.', 'min_length' => 'Your %s address must be a minimum of 10 characters.',
'max_length' => 'Your %s address must be a maximum of 40 characters.', 'valid_email' => 'You must enter a valid %s address.',
'is_unique' => 'That %s address already exists in our Database.'));
if ($this->form_validation->run() == FALSE) // The email address does not exist.
{
$this->load->view('email');
}
else
{
$email = $this->input->post('email');
$random_string = chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . rand(1,9) . chr(rand(65,90)) . chr(rand(65,90));
$code = $random_string;
$this->email_model->insert_email($email, $code);
$this->load->library('email'); // Not sure if this works - testing from localhost
$this->email->from('<?php echo WEBSITE_NAME; ?>', '<?php echo WEBSITE_NAME; ?>'); // Not sure if this works - testing from localhost
$this->email->to('$email'); // Not sure if this works - testing from localhost
$this->email->subject('Code.'); // Not sure if this works - testing from localhost
$this->email->message('Select & Copy this code, then return to the website. - ','$code'); // Not sure if this works - testing from localhost
$this->email->send(); // Not sure if this works - testing from localhost
$this->load->view('code');
}
}
}
Email Model
class Email_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
public function insert_email($email, $code)
{
$data = array(
'email_address' => $email,
'pass_word' => $code
);
$this->db->insert('tbl_members', $data);
return $this->db->insert_id();
}
}
Code Controller
class Code extends CI_Controller
{
public function index()
{
$this->load->model('Code_model', 'code_model');
$this->session->email // Problem line - syntax error, unexpected '$email' (T_VARIABLE)
$email = $this->session->email
$code = $this->input->post('code');
if ($this->code_model->find_code($email, $code))
{
$this->load->view('username');
}
else
{
$this->load->view('codeincorrect');
}
}
}
Code Model
class Code_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
public function find_code($code,$email)
{
$this->db->select('user_id');
$this->db->where('email_address', $email);
$this->db->where('pass_word', $code);
$code = $this->db->get('tbl_members');
if ($code->result())
{
return $this->db->delete('pass_word', $code);
}
}
}
Make sure the sessions library is loaded. You can do this manually by saying:
$this->load->library('session');
But if you want it loaded at all times, go to your autoload.php file and make sure sessions is added in the autoload['libraries'] area.
Your code looks like the buggy. Please check followings.
In PHP statements are ended by semi-colon (;)
$this->session->email : returns the value of email element from session array and it should be assigned, like the next line as in your code $email = $this->session->email.
Check whether the email is set in the session or not print_r($this->session->all_userdata());
Best of luck!

CodeIgniter: 404 Page Not Found ( In Hosting Server )

note : everything going well when I try in Localhost.
So I have a problem when I want to call my do_login controller in my login form.
this is my controller :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Do_login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('login_model', '', TRUE);
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'trim|required');
$this->form_validation->set_rules('password', 'password', 'trim|required|callback_check_database');
if($this->form_validation->run() == FALSE)
{
$this->load->view('admin/login_view');
}
else
{
redirect('home', 'refresh');
}
}
public function check_database($password)
{
$email = $this->input->post('email', TRUE);
$result = $this->login_model->check_login($email, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'user_id' => $row->user_id,
'email' => $row->email
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Email / Password salah');
return FALSE;
}
}
}
?>
this is my view :
<?php
$attributes = array('class' => 'form-signin', 'id' => 'myform');
echo form_open('do_login', $attributes);
?>
When I try it in Localhost, everything going well and smooth.
But when I try in my web server, everytime I submit the login form, I directed into 404.
Thanks for your help :)
Check your file names Because it happens with me that different case file name was worked on localhost but not on server.
So check it once.

how to send data from controller to model with CodeIgniter

i'm newbie with CI and i'm using CI v.2.2.1, i want send data from my controller to model but i have some trouble here. The error said my model is undefined property. here's my code :
Controller :
users.php
public function postUser(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$address = $this->input->post('address');
$level = $this->input->post('level');
$status = $this->input->post('status');
$this->load->model('model_crud');
$query = $this->model_crud->insert('Users',$_POST);
echo "$query";
}
Model :
model_crud.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_crud extends CI_Model {
public function __construct(){
parent::__construct();
}
public function insert($table,$data){
$query = $this->db->insert($table,$data);
return $query;
}
}
is there any configuration to use model or my code is wrong?
can anyone help me ? Thx
1st thing, you're not providing enough information about the error.
Is the model loaded anywhere/anyhow? From the controller you load your model this way:
$this->load->model('model_crud');
Or you can preload it, by modifying your autolad.php config file
in postUser() - you're getting your post data, but don't really use it. Instead, you're passing the whole global $_POST, which may be dirty and unsafe. I'd recommend using CodeIgniter's XSS filtering when forming a data array from POST:
$data = array (
'username' => $this->input->post('username', TRUE); //TRUE identifies you're passing your data through XSS filter,
//all other elements
);
finally:
$query = $this->model_crud->insert('Users',$data);
You can do it by sending an array to your model with table columns name as key :
Controller :
$data = array(
'username' => $this->input->post('username') ,
'password ' => $this->input->post('password') ,
...
);
// 'TABLE COLUMN NAME' => "VALUE"
$query = $this->model_crud->insert('Users',$data);
echo "$query";

Unable to redirect url with callback in codeigniter

I just create login class that check the valid user, after that user enter in his/her home section. But now i need to change the destination of the user's home conditionally, Unable to
attach switch case in url redirection.
Note: I think varibale not properly passed in function.Plz help me
My code as Follow:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('userp','',TRUE);
}
function index($dest)
{
$this->load->view('header');
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$data['msg'] = "";
$this->load->view('login',$data);
}
$this->load->view('footer');
}
function check_database()
{
//Field validation succeeded. Validate against database
$email = $this->input->post('email');
//query the database
$password= $this->input->post('password');
$result = $this->userp->login($email,$password);
if($result!==FALSE)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' =>$row->email,
'fname' =>$row->first_name,
'lname' =>$row->last_name,
'pic'=>$row->pic
);
$this->session->set_userdata('logged_in',$sess_array);
}
switch($dest){
case 'NULL':{
redirect('home','refresh');
break;
}
case 'part1':{
redirect('subhome','refresh');
break;
}
case 'part2':{
redirect('subhome1','refresh');
break;
}
default :{
redirect('mainhome','refresh');
break;
}
}
return TRUE;
}
else
{
$data['msg'] = "Something Wrong Username/Password";
$this->load->view('login',$data);
//return false;
}
}
};
?>
The problem is that you are passing $dest to the index() function but you are attempting to use it in your check_database() function. So either you want to move your switch into the index function or you want to pass $dest to check_database() like
function check_database($dest)
{
//You can use $dest in here
}
If you want to pass an extra parameter to your callback function you need to put it in brackets like
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database[parameter]');
or
$this->form_validation->set_rules("password", "Password", "trim|required|xss_clean|callback_check_database[$parameter]");

Categories