getting memory allocation error in codeigniter HMVC - php

Description: I created simple login page with session and mysql database using PHP,MySQL Database,CodeIgniter(HMVC) and XAMPP Server in local system.
Problem: I am getting following error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in C:\xampp\htdocs\ci\application\third_party\MX\Modules.php on line 114.
Question: Please tell me the solution how to resolve this above error.
View:
*login2.php*
<?php echo form_open(/* base_url(). */'user/user/login'); ?>
<?php
if ($this->session->flashdata('login_error')) {
echo 'you have entered an incorrect username or password';
}
echo validation_errors();
?>
<label>UserName</label>
<div>
<?php echo form_input(array('id' => 'username', 'name' => 'username', 'placeholder' => 'Enter ur name')); ?>
</div>
<label>Password</label>
<div>
<?php echo form_password(array('id' => 'password', 'name' => 'password', 'placeholder' => 'Enter ur password')); ?>
</div>
<div>
<?php echo form_submit(array('name' => 'submit'), 'Login'); ?>
</div>
<?php echo form_close(); ?>
Controller:
*user.php*
<?php
class User extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->login();
}
function main_page() {
echo 'You are logged in';
}
function login() {
$this->form_validation->set_rules('username', 'Username', 'required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|trim|max_length[200]|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login2');
} else { // process their input and login the user
extract($_POST);
$user_id = $this->user->login($username, $password);
if ($user_id) {
//login failed error...
$this->session->set_flashdata('login_error', TRUE);
redirect('user/login');
} else {
//logem in
$this->session->set_userdata(array(
'logged_in' => TRUE,
'user_id' => $user_id
));
redirect('user/main_page');
}
}
}
}
?>
Model:
*user.php*
<?php
Class User extends CI_Model {
function login($username, $password) {
$this->db->select('id, username, password');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', MD5($password));
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
}
?>

Related

Use form_validation correctly in CodeIgniter

I have a registration system with CodeIgniter but currently I have no control on email and password. A user can register without putting email or a password.
I have an index() and a register_user function() in my Signup controller but the redirection is not working on success
At the moment I have the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->load->view('home-view');
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
function register_user(){
$this->load->library('custom_u_id');
$data = array('user_id' => $this->custom_u_id->construct_id('USR'),
'name' => $_POST['name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
);
$this->load->model('signup_model');
$user_details = $this->signup_model->register_user($data);
if (!empty($user_details)){
$user_data = array
(
'user_id' => $user_details['user_id'],
'email' => $user_details['email'],
'name' => $user_details['name'],
'user_type' => $user_details['user_type'],
);
$this->session->set_userdata('sessiondata',$user_data);
if (intval($user_details['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
} else{
redirect('login');
}
}// end of function login
}
Do I need to put the form_validation in my register_user function ? I've tried but the check doesn't work anymore...
I also have in my view the <?php validation_errors();?> function and the <?php form_open(base_url().'signup');?>
looking by your code, i think you want to put register_user() inside validation TRUE since the query is in that method.
so try to change your code to this :
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->register_user();
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
and be sure your form action to like this :
<form action="<?=site_url('/signup/index');?>">

How to show user profile data in codeigniter with session?

I am new in CI and I have make login system but I am trying to show user profile data in username and email are show on user profile view page. I am to much try but I cant solve this issue. Here is my code.
Can one tell me about how to show user profile data in profile view page? I have too much trying but no solution is available for showing a user profile data in codeigniter.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class UserLogin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper(array('form', 'url'));
$this->load->model('login_model');
}
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->view('UserLoginPage');
}
public function userLoginProcess()
{
// $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// echo "login reached";
$this->form_validation->set_rules('username', 'Username', 'required|alpha|trim');
$this->form_validation->set_rules('password', 'Password','required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
$this->session->set_flashdata("<p class='text-danger'>","</p>");
if ($this->form_validation->run() ){
//username aur password araha hay
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('login_model');
//$loginObj session ko check variable haa..!
$loginObj = $this->login_model->login_valid($username,$password);
if($loginObj){
// print_r($loginObj->password);
$this->session->set_userdata('userSessionObj', $loginObj);
//print_r($loginObj);
$this->load->view('userDashboard');
}
else{
// echo "<script>alert('UserName And Passowrd Are Wrong....!!!! ');</script>";
// $this->session->set_flashdata('error', 'Invalid Username and Password');
$this->load->view('UserLoginPage');
// echo "<script language=\"javascript\">alert('Username And Password Are Worng');</script>";
$this->session->set_flashdata('error','<p class="text-danger"> you entered invalid username and password');
} // end of else
} // end of public function
else
{
$this->load->view('userLoginPage');
} // end of else
} //end of function
//logout function Start
public function logout()
{
$this->session->sess_destroy();
$this->session->unset_userdata('username','password');
return redirect("userLogin");
}
//logout function End
public function register()
{
$this->load->view('registered');
} //end of register function
public function preRegister()
{
$this->form_validation->set_rules('username', 'Username', 'required|alpha|trim|min_length[5]|max_length[12]|is_unique[user.username]');
$this->form_validation->set_rules('email', 'email','required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password', 'Password','required');
$this->form_validation->set_rules('confirmpassword', 'ConfirmPassword','required|matches[password]');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
if($this->form_validation->run())
{
$store = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'dateOfbirth' => $this->input->post('dateOfbirth'),
'password' => $this->input->post('password'),
'confirmpassword'=> $this->input->post('confirmpassword'),
'gender' => $this->input->post('gender')
);
$this->login_model->insert_mod($store);
redirect('UserLogin/index');
} // end of if form_validation
else
{
$this->load->view('registered');
} // end of else
} //end of preRegister function
public function employess() // employes of add ka page view kr rha haaaa
{
$this->load->view('userDashboard');
} // employes of add ka page view kr rha haaaa
public function proEmployess()
{
// $this->load->view('addEmployess');
// $this->load->view('addEmployess');
$employessData = array(
// 'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'department' => $this->input->post('department')
);
$this->login_model->employess_add($employessData);
redirect('UserLogin/employess');
}
public function myProfile(){
// $myProfile = $this->session->userdata();
$this->load->view('headerDashboard.php');
$myProfiledata ['profiles'] = $this->login_model->profileVeiw();
$this->load->view('myProfileView', $myProfiledata);
$this->load->view('footerDashboard.php');
}
// public function myProfile(){
//
// $this->load->view('headerDashboard');
// //$myProfiledata ['datas'] = $this->login_model->veiw_Employess();
// //$employessRecord['datas'] = $this->login_model->veiw_Employess();
// $this->load->view('myProfileView');
// $this->load->view('footerDashboard');
//
// }
}
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model{
public function login_valid($username ,$password)
{
$q = $this->db->where(['username'=>$username, 'password'=>$password])
->get('user');
if( $q->num_rows() )
{
//return pori row ho rhe ha jo database sa arhe haaa
return $q->row();
// return $q->row()->id;
// return TRUE;
}
else
{
return FALSE;
}
}
public function insert_mod($store)
{
$this->db->insert('user', $store);
}
public function employess_add($employessData)
{
$this->db->insert('employessRecord',$employessData);
}
public function view_employess(){
$query = $this->db->get('employessrecord');
return $query;
}public function profileVeiw(){
$queries = $this->db->get('user');
//$queries = $this->session->userdata();
// print_r($queries);
return $queries;
}
}
<div class="col-xl-6">
<div class="card" >
<div class="card-header">
<h4>Profile Details</h4>
</div><br>
<?php foreach($profiles->result() as $profile):?>
<div class="col-md-8">
<label for="name">Username:</label>
<?php echo $profile->username;?>
</div><br><br>
<div class="col-md-8">
<label for="address">Email:</label>
<?php echo $profile->email;?>
</div><br><br>
<div class="col-md-8">
<label for="address">Passowrd:</label>
<?php echo $profile->password;?>
</div><br><br>
<div class="col-md-8">
<label for="address">Gender:</label>
<?php echo $profile->gender;?>
</div><br><br>
<?php endforeach; ?>
</div>
</div>
There is opening tag php tag
so you received the data from form and inserted it in database $this->login_model->insert_mod($store) .
when you call myProfile function you get data in $myProfiledata ['profiles'] = $this->login_model->profileVeiw(); here and passed it to view. if this is correct then first try to print $myProfiledata before passing to the view if everything ok there then try to print $profiles on the "myProfileView" view.
$this->session->userdata('userSessionObj');
here you can check the session data

How to compare form input data to database data in a custom codeigniter form validator?

Edit: Some naming had been mixed up in my attempts to solve it myself. I've fixed the callback etc naming and the same error persists.
I am attempting to create a login page for my codeigniter website. I already have a registration page that correctly inputs usernames and passwords in to a "users" table. I am having issues understanding the syntax of creating the functions needed for custom form validators.
My error is "Unable to access an error message corresponding to your field name" for the password and username custom validators.
Here is the relevant part of the controller "login_ctrl"
class login_ctrl extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_mdl');
}
function index() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_userCorrect');
//Validating Password Field
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_passwordCorrect');
//variables to pass form input
$username = $this->input->post('username');
$password = $this->input->post('password');
//reload login page if validation fails
if ($this->form_validation->run() == FALSE) {
$this->load->view('login');
} else {
//functions for custom validation
function userCorrect($username) {
$this->load->library('form_validation');
//the loads the model that contains the function to compare input to database data
$userExists = $this->login_mdl->userExists($username);
if ($userExists) {
$this->form_validation->set_message(
'userCorrect', 'correct user.'
);
return true;
} else {
$this->form_validation->set_message(
'userCorrect', 'not a valid user name.'
);
return false;
}
}
function passwordCorrect($password) {
$this->load->library('form_validation');
$passwordExists = $this->login_mdl->passwordCorrect($password);
if ($passwordExists) {
$this->form_validation->set_message('passwordCorrect', 'correct password.');
return true;
} else {
$this->form_validation->set_message('passwordCorrect', 'invalid password.');
return false;
}
}
This is the corresponding view "login"
<?php echo form_open('login_ctrl'); ?>
<h1>Login</h1><hr/>
<?php if (isset($message)) { ?>
<?php } ?>
<?php echo form_label('User Name :'); ?> <?php echo form_error('username'); ?><br />
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?><br />
<?php echo form_label('Password :'); ?> <?php echo form_error('password'); ?><br />
<?php echo form_input(array('id' => 'password', 'name' => 'password')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
Finally, this is the corresponding model "login_mdl" (I think the issue might be in this guy).
<?php
class login_mdl extends CI_Model{
function __construct() {
parent::__construct();
}
function userExists($username) {
$this->db->select('id');
$this->db->where('username', $username);
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
function passwordCorrect($password) {
$this->db->select('password');
$this->db->where('username', $username);
$query = $this->db->get('users');
$result = $query->row();
return $result->password;
if ('password' == $password) {
return true;
} else {
return false;
}
}
}
?>
I think my issue is related to the db calls and if statements but I've been reading documentation and failing at fixing this for hours so a new pair of eyes would be greatly appreciated.
You need to make your fieldname on your costume rules function is same as your function callback. So, it’s should be like this :
$this->form_validation->set_message(
'userCorrect', 'correct user.'
Do same thing on your other function.

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.

Add data to the database using codeigniter

I am currently trying to add data to the database using codeigniter. I have already set up a registration page using the active method and attempted to use the same method for the add news form but was unsuccessful.
When I click submit it is saying page cannot be found and the url shows the controller function name. This is the same when i purposely leave any fields blank. I have checked my database and no records have been added and no php log errors.
Here is my snippets of code:
View:
<?php echo form_open('add/add_article'); ?>
<?php echo form_input('title', set_value('title', 'Title')); ?><br />
<?php echo form_textarea('content', set_value('content', 'Content')); ?><br />
<?php echo form_input('author', set_value('author', 'Author')); ?>
<?php echo form_submit('submit', 'Add Article'); ?>
<?php echo validation_errors('<p class="error">' );?>
<?php echo form_close(); ?>
Controller:
class Add extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('admin/add');
}
public function add_article() {
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('content', 'Content', 'trim|required');
$this->form_validation->set_rules('author', 'Author', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->index();
}else{
$this->load->model('news_model');
if($query = $this->news_model->addArticle()) {
$this->load->view('news');
}else {
$this->load->view('news');
}
}
}
}
Model:
public function __construct() {
parent::__construct();
}
function addArticle() {
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username'));
$insert = $this->db->insert('news', $data);
return $insert;
}
}
If it's the server that's throwing the page not found it's almost certainly a URL issue as opposed to a CI/PHP issue.
Is your base url defined properly in the config file? Is your .htaccess configured properly (an old configuration could be routing /add requests away from CI)?
Try adding the following action to the Add controller, and navigating to it directly at http://[base]/add/thetest
public function thetest() {
echo 'Controller accessed';
die;
}
If it still says page not found it's not your code, it's your config (either server config or CI).
Instead of insert use update in your model like:
$insert = $this->db->update('news', $data);
return $insert;
And I think that this part of your code in controller is wrong too (wrong if statement and no data send to model):
if($query = $this->news_model->addArticle()) {
$this->load->view('news');
}else {
$this->load->view('news');
}
try this:
$data =array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'author' => $this->input->post('author'),
'username' => $this->input->post('username')
);
$query = $this->news_model->addArticle($data);
if($query)
{
// query ok
$this->load->view('news');
}
else {
// no query
$this->load->view('news');
}

Categories