form_validation returning false : codeigniter - php

Can anyone help me why my if code ($ this-> form_validation-> run () == FALSE) always returning FALSE even all fields are valid.
I have already loaded form_validation, url etc. the application is already working on a web server but when I deploy the application in localhost I hang on this error (from_validation always return false).
Here is my form:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$email = array(
'name' => 'email',
'id' => 'email',
'class'=> 'input',
'value' => set_value('email'),
'maxlength'=> 80,
'size' => 27,
);
$password = array(
'name' => 'password',
'id' => 'password',
'class' => 'input',
'value' => set_value('password'),
'size' => 27,
);
?>
<div class="row-fluid">
<div class="span8">
<b>Bienvenu!</b>
<p>Identifier-vous pour Accèder à Votre Compte</p>
<p>et Profiter de Nos Services en Ligne </p>
<?php $connect='users/login'; ?>
</div>
</div>
<div class="error"><strong><?=$this->session->flashdata('flashSuccess')?></strong></div>
<div class="login">
<?php $attributes = array('name' => 'loginf', 'autocomplete' => 'off' ); ?>
<?php echo form_open($connect,$attributes );?>
<div class="row-fluid">
<div class="span3">
<?php echo form_label('Adresse Email', $email['id']); ?>
</div>
<div class="span5">
<?php echo form_input($email); ?>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<?php echo form_label('Mot de passe', $password['id']); ?>
</div>
<div class="span5">
<?php echo form_password($password); ?>
</div>
</div>
<div class="row-fluid">
<div class="span4">
<input class="btn" name="submit" type="submit" id="submit" value="Connexion" />
</div>
</div>
</div>
<?php echo form_close()?>
And my controller where the form_validation always returns false:
<?php
function login(){
$this->load->model('User_m');
$auto=FALSE;
$email=strtolower($this->input->post('email'));
$found="#bmc.com";
$res=strpos($email,$found) ;
if($res > 0){
$role=$this->User_model->getRole(strtolower($this->input->post('email')));
$tiers=$this->User_model->getTiers(strtolower($this->input->post('email')));
if($role=="VEND" && $tiers==""){
$auto=TRUE;
$this->form_validation->set_rules('email', 'Email Address', 'xss_clean|trim|required|valid_email');
}else{
$this->form_validation->set_rules('email', 'Email Address', 'xss_clean|trim|required|valid_email|callback_conten');
}
}else{
$this->form_validation->set_rules('email', 'Email Address', 'xss_clean|trim|required|valid_email|callback_conten');
}
$this->form_validation->set_rules('password', 'Password', 'xss_clean|trim|required|min_length[6]|max_length[20]|callback_password_check');
$this->_email = $this->input->post('email');
$s_pass=$this->security->xss_clean($this->input->post('password'));
$this->_password = sha1($this->_salt .$s_pass);
if($this->form_validation->run() == FALSE){
$this->tmpl_login['success']=NULL;
$this->tmpl_login['auth']='style="display:block"';
$this->tmpl_login['p_o']='style="display:none"';
$this->layout->view_menu_top('menul');
$this->layout->view('compte/login',$this->tmpl_login);
}else{
$this->auth->login('email');
$dv=$this->find_date_visit();
if($auto==FALSE){$this->_SendDynamiqueMessage($this->_email);}
$this->session->set_userdata('name',$this->find_name_user($this->_email));
$this->session->set_userdata('quality',$this->find_quality_user($this->_email));
$this->session->set_userdata('date_visite',$dv);
$this->session->set_userdata('email',$this->_email);
$user_agent = get_browser(null, true);
$this->db->query("insert into us_connexion values ('". $this->_email."',sysdate,'". $user_agent['platform']."','". $user_agent['parent']."')");
if(!$dv){
$this->session->set_userdata('date_visite','premier visite');
redirect('users/password');
}
redirect('users/index');
}
}
?>
PS: I used the codeigniter 2.0.2 version.
Any help please!
Thinks.

Related

CodeIgniter login page load and show login page again on local server but working on live server

When I login admin panel locally it redirects and load login page again, not open dashboard of my admin panel but when I am moving my file to live server admin panel works perfectly.
My Login page Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="login-box-body" class="text-center" style="margin:0 auto;padding:30px;background:#f0f0f0;border-radius:10px 10px 0 0;">
<div style="margin:0 auto;" class="text-center">
<img src="<?php echo base_url('upload/images/logo.png'); ?>" style="width:300px;" class="text-center img-responsive;" title="<?php echo $title_lg; ?>" />
</div>
</div>
<div class="login-box-body">
<p class="login-box-msg"><?php echo lang('auth_sign_session'); ?></p>
<div class="text-danger"><?php echo $message;?></div>
<?php echo form_open('auth/login');?>
<div class="form-group has-feedback">
<?php echo form_input($identity);?>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<?php echo form_input($password);?>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<!-- <label>
<?php /*?> <?php echo form_checkbox('remember', '1', FALSE, 'id="remember"'); ?><?php echo lang('auth_remember_me'); ?><?php */?>
</label>-->
</div>
</div>
<div class="col-xs-4">
<?php /*?><?php echo form_submit('submit', lang('auth_login'), array('class' => 'btn btn-primary btn-block btn-flat'));?><?php */?>
<input type="submit" name="submit" value="Login" class="btn btn-primary btn-block btn-flat" style="background-color: #2f76bb;border-color: #2f76bb;">
</div>
</div>
<?php echo form_close();?>
above code is my login page code
My Authentication Code
class Auth extends MY_Controller {
function __construct()
{
parent::__construct();
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
$this->lang->load('auth');
}
function index()
{
if ( ! $this->ion_auth->logged_in())
{
redirect('auth/login', 'refresh');
}
else
{
redirect('/', 'refresh');
}
}
function login()
{
if ( ! $this->ion_auth->logged_in())
{
/* Load */
$this->load->config('admin/dp_config');
$this->load->config('common/dp_config');
/* Valid form */
$this->form_validation->set_rules('identity', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
/* Data */
$this->data['title'] = $this->config->item('title');
$this->data['title_lg'] = $this->config->item('title_lg');
$this->data['auth_social_network'] = $this->config->item('auth_social_network');
$this->data['forgot_password'] = $this->config->item('forgot_password');
$this->data['new_membership'] = $this->config->item('new_membership');
if ( $this->form_validation->run() == TRUE)
{
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
{
if ( ! $this->ion_auth->is_admin())
{
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/', 'refresh');
}
else
{
/* Data */
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
/* Load Template */
redirect('admin', 'refresh');
}
}
else
{
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/login', 'refresh');
}
}
else
{
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['identity'] = array(
'name' => 'identity',
'id' => 'identity',
'type' => 'email',
'value' => $this->form_validation->set_value('identity'),
'class' => 'form-control',
'placeholder' => lang('auth_your_email')
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'class' => 'form-control',
'placeholder' => lang('auth_your_password')
);
if($this->session->flashdata('message'))
{
$this->data['forgot_password'] = TRUE;
}
/* Load Template */
$this->template->auth_render('auth/login', $this->data);
}
}
else
{
redirect('/', 'refresh');
}
}
This is my Authentication Code.
Thanks For advance
Please ensure that the following are updated:
The PHP version (on the local server)
The Codeigniter version
The Ion_Auth library
Hope it helps :)

Codeigniter form validation rules order

I've set this rules on the controler
public function checkLogin()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules(
'username',
'nombre de usuario',
'required',
array('required' => 'Debes introducir un %s.')
);
$this->form_validation->set_rules(
'pass',
'contraseña',
'required',
array('required' => 'Debes introducir una %s.')
);
$this->form_validation->set_rules(
'bad_login',
'login',
'callback_checkCredentials'
);
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('login/login_view');
$this->load->view('footer');
} else {
$this->session->set_userdata('logged',true);
$this->session->set_userdata('user',$this->input->post('username'));
redirect('main');
}
}
public function checkCredentials(){
if($this->input->post('username') == $this->user && $this->input->post('pass') == $this->pass){
return true;
}else{
$this->form_validation->set_message('checkCredentials', 'El usuario/contraseña introducido no es correcto');
return false;
}
}
and the view
<div class="container">
<div class="col-md-4">
<?php $this->load->helper('form'); ?>
<?php echo form_open('checkLogin'); ?>
<?php echo form_error('bad_login'); ?>
<?php echo form_error('username'); ?>
<div class="form-group">
<?php echo form_label('Usuario:'); ?>
<?php echo form_input(array('id' => 'username', 'name' => 'username', 'class'=>'form-control', 'placeholder'=>"Nombre de usuario")); ?>
</div>
<?php echo form_error('pass'); ?>
<div class="form-group">
<?php echo form_label('Contraseña:'); ?>
<?php echo form_password(array('id' => 'pass', 'name' => 'pass', 'class'=>'form-control', 'placeholder'=>"Contraseña")); ?>
</div>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Enviar', 'class'=>'btn btn-primary')); ?>
<?php echo form_close(); ?>
</div>
</div>
It's a login form with and input to the user and another to the password , but if the pass or the username are empty there is no need to check if pass and the username are correct(and show the message error), I'm not sure how to do that
You can simply do this:
$username = trim($this->input->post('usename'));
$password = trim($this->input->post('password'));
if(!empty($username) && !empty($password))
{
// do you validation
}
else
{
// maybe show error message
}

Codeigniter form validation does not show error and redirects to blank page

Given below is controller and view but its not validated and only redirects to about:blank page.
I Have made some changes but nothing happens.
controller:
<?php
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library(array('form_validation','session')); // load form lidation libaray & session library
$this->load->helper(array('url','html','form')); // load url,html,form helpers optional
}
public function index(){
// set validation rules
$this->form_validation->set_rules('name', 'Name', 'required|min_length[4]|max_length[10]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Phone Number', 'required|numeric|max_length[15]');
$this->form_validation->set_rules('subject', 'Subject', 'required|max_length[10]|alpha');
$this->form_validation->set_rules('message', 'Message', 'required|min_length[12]|max_length[100]');
// hold error messages in div
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// check for validation
if ($this->form_validation->run() == FALSE) {
$this->load->view('viewform');
}else{
$this->session->set_flashdata('item', 'form submitted successfully');
redirect('Home');
}
}
}
?>
view:
<?php if(validation_errors()) { ?>
<div class="alert alert-warning">
<?php echo validation_errors(); ?>
</div>
<?php } ?>
<?php if($this->session->flashdata('item')) { ?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('item'); ?>
</div>
<?php } ?>
<?php echo form_open(); ?>
<div class="form-group">
<?php echo form_label('Your Name','name'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "name", "placeholder"=>"Enter Name","value" => set_value('name'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Email address','EmailAddress'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "email", "placeholder"=>"Enter email","value" => set_value('email'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Phone Number','number'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "number", "placeholder"=>"Enter Phone Number","value" => set_value('number'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Subject','subject'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "subject", "placeholder"=>"Enter Subject","value" => set_value('subject'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Message','message'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "message", "placeholder"=>"Enter Message","value" => set_value('message'))); ?>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<?php echo form_close(); ?>
so when any error is there the page is redirected to about:blank even if all fields are proper or improper how to fix this?

Yii update case shows empty screen

I have a form for editing a user record. Its updates the record for the first time but when i press the update button again, it shows me an empty screen and it unsets the picture as well. I cannot figure out what I am doign wrong
Below is my code
public function actionEditProfile($affusername = NULL) {
$this->layout = 'layout_home';
if ($this->VarifyUser($affusername) && Yii::app()->user->id) {
$model = new Users('edit'); //apply rules if user comes directly to signup page
// uncomment the following code to enable ajax-based validation
if (isset($_POST['ajax']) && $_POST['ajax'] === 'editprofile-form') {
CActiveForm::validate($model);
Yii::app()->end();
}
if (isset($_POST['Users'])) {
$the_image = CUploadedFile::getInstance($model, 'image');
if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {
$model->image = $the_image;
}
if (is_object($the_image)) {
$file_name = Yii::app()->user->id . '.' . $model->image->extensionName;
$move = $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/myproject/images/users/' . $file_name);
$_POST['Users']['image'] = $file_name;
}
$model->attributes = $_POST['Users'];
if ($model->validate()) {
// form inputs are valid, do something here
if ($model->updateByPk(Yii::app()->user->id, $_POST['Users'])) {
//$this->setFlashSuccess("Unable to register user, please try again");
$this->setFlashSuccess('Updated successfully, now redirecting.');
//$this->redirect($this->base_url);
} else {
$this->setFlashError("Unable to update user, please try again");
return false;
}
} else {
print_r($model->getErrors());
echo 'validation fail';
exit;
}
}
$user_data = Users::model()->getUsersByUserName($affusername); //getting user information
//print_r($user_data);
//$model=new Users('nonfb-create'); //apply rules if user comes directly to signup page
$this->render('editprofile', array('model' => $model, 'data' => $user_data,));
} else {
$this->redirect($this->base_url);
}
}
My view is
<div class="adminform_wrapp">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'editprofile-form',
'enableAjaxValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
),
'enableClientValidation' => true,
'focus' => array($model, 'first_name'),
'htmlOptions' => array(
'enctype' => 'multipart/form-data'
)
));
echo $form->errorSummary($model);
?>
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_First_Name">First Name: <span class="required">*</span></label>
<?php echo $form->textField($model, 'first_name', array('value' => $data['first_name'])); ?>
<?php $form->error($model, 'first_name'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Last_Name">Last Name: <span class="required">*</span></label>
<?php echo $form->textField($model, 'last_name', array('value' => $data['last_name'])); ?>
<?php $form->error($model, 'last_name'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Email">Email: <span class="required">*</span></label>
<?php echo $form->textField($model, 'email', array('value' => $data['email'])); ?>
<?php $form->error($model, 'email'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Phone_No">Phone No: <span class="required">*</span></label>
<?php echo $form->textField($model, 'phone_no', array('value' => $data['phone_no'])); ?>
<?php $form->error($model, 'phone_no'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label>Username:</label>
<span class="profile-username"><?php echo $data['username']; ?></span> </div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label>Edit Profile picture:</label>
<span class="image-placeholder">
<img src="<?php echo $this->theme_baseurl . '/images/users/' . $data['image']; ?>" style="width:96px; height:96px;"/>
</span>
<div id='file_browse_wrapper'>
<?php
//echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image', array('id' => 'file_browse'));
echo $form->error($model, 'image');
?>
</div>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<?php echo $form->labelEx($model, 'Address', array('class' => 'fieldname')); ?>
<?php echo $form->textField($model, 'address', array('value' => $data['address'])); ?>
<?php $form->error($model, 'address'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Country">Country: <span class="required">*</span></label>
<select class="error" onchange="print_state('Users_state', this.selectedIndex);" id="Users_country" name ="Users[country]"></select>
<?php $form->error($model, 'country'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_State">State: <span class="required">*</span></label>
<select name ="Users[state]" id ="Users_state"></select>
<script language="javascript">print_state("Users_state", '', "<?php echo $data['state'] ?>");</script>
<?php $form->error($model, 'state'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_City">City: <span class="required">*</span></label>
<?php echo $form->textField($model, 'city', array('value' => $data['city'])); ?>
<?php $form->error($model, 'city'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Zipcode">Zipcode: <span class="required">*</span></label>
<?php echo $form->textField($model, 'zipcode', array('value' => $data['zipcode'])); ?>
<?php $form->error($model, 'zipcode'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<input type="submit" class="adminupdate_btn" value="Update">
<input type="reset" class="admincancel_btn" value="Cancel">
</div>
<!--adminform_row-->
<?php $this->endWidget(); ?>
</div>
An empty screen is shown usually when there is a syntactic error.
You can see the error in the apache log (usually at /var/log/apache2/error.log in linux).
As darkheir said, you should see that log file.
It was a syntax error. Also it was updating the complete row in the database where update form was sending only a few fields. I fixed by manually setting the attributes. Below is my code
if (isset($_POST['Users'])) {
//echo "<pre>";
//print_r($model->image);
// print_r($_POST);
// print_r($_FILES);
//echo "</pre>";
$the_image = CUploadedFile::getInstance($model, 'image');
if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {
$model->image = $the_image;
}
//echo "<pre>";
//print_r($_POST);
//echo "</pre>";
$first_name = $_POST['Users']['first_name'];
$last_name = $_POST['Users']['last_name'];
$email = $_POST['Users']['email'];
$phone_no = $_POST['Users']['phone_no'];
$address = $_POST['Users']['address'];
$country = $_POST['Users']['country'];
$state = $_POST['Users']['state'];
$city = $_POST['Users']['city'];
$zipcode = $_POST['Users']['zipcode'];
if (is_object($the_image)) {
$file_name = Yii::app()->user->id . '.' . $model->image->extensionName;
$model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/karmora/images/users/' . $file_name);
$_POST['Users']['image'] = $file_name;
//$model->first_name = $_POST['Users']['first_name'];
$attributes = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'phone_no' => $phone_no,
'image' => $file_name,
'address' => $address,
'country' => $country,
'state' => $state,
'city' => $city,
'zipcode' => $zipcode
);
} else {
$attributes = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'phone_no' => $phone_no,
'address' => $address,
'country' => $country,
'state' => $state,
'city' => $city,
'zipcode' => $zipcode);
}
$model->updateByPk(Yii::app()->user->id, $attributes);
}

Cannot post radio value in Codeigniter

view:
<?php
$attr_form = array('class' => 'form-horizontal');
echo form_open('login/create', $attr_form); ?>
<div class="form-group">
<label class="col-sm-4 control-label">Gender</label>
<div class="col-sm-4">
<?php echo form_radio('gender', 'M'); ?>
<?php echo form_radio('gender', 'F'); ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<?php
$data_submit = array('class'=> 'btn btn-info', 'name' => 'submit', 'value'=> 'Create Account');
echo form_submit($data_submit);?>
</div>
</div>
controllers:
function create ()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('gender', 'Gender', 'required');
if($this->form_validation->run() == FALSE)
{
$this->signup();
}
else
{
$this->load->model('parents_model');
if($query = $this->parents_model->create_member())
{
$data['main_content'] = 'successful';
$this->load->view('include/template', $data);
}
else
{
$this->signup();
}
}
}
model:
function create_member ()
{
$new_parents = array(
'gender' => $this->input->post('gender')
);
$insert = $this->db->insert('parents', $new_parents);
return $insert;
}
I just simplified the other form inputs.
Can anyone figure out whats wrong with my code? It cannot go to successful page.
Or do I use wrong form_radio code ?
I have tried many times! Please help me....
You have a syntax error.
$this->input->post->('gender')
should be:
$this->input->post('gender')
try this
$gender = $this->input->post('gender')['M'] ? 'M':'F';
$new_parents = array('gender' => $gender);
After I restarted my localhost, everything works again.
Cause I have created a column in the table without restarting!

Categories