codeigniter login validation not validating rules - validation

am using codeigniter 3.0.4 .here this Here the action send to the controler(first.php)not validating anything it will turn to else statement and display login page again(as set as in redirect('welcome/login_page');). my register function in the same controller working good. what is the exact problem ..please help me to find it.thanks.
..............the controller(first.php).....
public function signup_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|valid_email|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
if($this->form_validation->run() ){
$user_id= $this->model_users->get_id($this->input->post('email'));
$user_status = $this->model_users->get_status($this->input->post('email'));
$data = array (
'user_id' => $user_id,
'email' => $this->input->post('email'),
'user_status' =>$user_status,
'is_logged_in' => 1
);
$this->session->set_userdata($data);
}
if ($this->session->userdata('user_status')=='0') {
redirect('main/admins');
}
elseif ($this->session->userdata('user_status')=='1') {
redirect('main/members');
}else{
redirect('welcome/login_page');
}
}
public function validate_credentials(){
$this->load->model('model_users');
if($this->model_users->can_log_in() ){
return TRUE;
} else {
$this->form_validation->set_message('validate_credentials', 'Invalid useername(email-id) or password. !');
return FALSE;
}
}
....the view(login.php)......
echo form_open("first/signup_validation");
echo'<div class="row">';
echo'<div class="form-group col-md-6 col-xs-12 col-sm-6">';
echo'<label for="email">Email<span></span></label>';
echo'<input type="text" class="form-control" id="email">';
echo'</div>';
echo'<div class="form-group col-md-6 col-xs-12 col-sm-6">';
echo'<label for="password">Password<span></span></label>';
echo'<input type="password" class="form-control" id="password">';
echo'</div>';
echo'</div>';
$data2 = array(
'class' => 'btn btn-default',
);
echo form_submit($data2, 'Login');
echo form_close('');
?>

echo '<input type="text" class="form-control" id="email">';
Your input elements don't contain a name attribute. The name is what's used as the identifier in the validation rule.
$this->form_validation->set_rules('email'... // <- 'email' is the name attribute
Do this...
echo'<input type="text" class="form-control" id="email" name="email">';

Related

delete if password is correct

i need to create a condition that clears the record but with a password.
if the password is correct execute the delete();
controller:
public function eliminar($id){
$registros = \App\Models\Registro::findOrFail($id);
$registros->delete();
return redirect('sistema')->with('mensaje', 'Registro Borrado con exito');
}
public function borrar($id){
// return $request->all();
$data = [
'category_name' => 'datatable',
'page_name' => 'multiple_tables',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
'fechax' => Carbon::now(),
'borrar' => \App\Models\Registro::findOrFail($id),
'password' => 'PASSCODE',
];
return view('borrar')->with($data);
}
blade.php:
<h1>Do you want to delete the record?</h1>
<form action="{{ route('eliminar', $borrar) }}" class="d-inline" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger btn-sm">DELETE</button>
<div class="form-group col-md-6">
<label for="telefono">Password</label>
<input name="password" type="password" class="form-control" id="telefono" required>
</div>
</form>
the password is obtained statically
How can I make it delete without the password is identical?
help please
If the password is saved statically, inside in a variable, the following should do the job for you.
routes/web.php
Route::delete('/path/here', 'SomeController#destroy');
SomeController.php
public function destroy($id)
{
$model = YourModel::find($id);
if (! $model) {
session()->flash('error_message', 'Model not found with the given id: ', . $id);
return back();
}
// $password is the password that you have saved somewhere
if (request()->password_field_value == $password) {
$model->delete();
session()->flash('success_message', 'Model deleted successfully.');
return back();
}
session()->flash('error_message', 'Invalid password. Try again');
return back();
}

Undefined Index:email Error In Laravel 5.6

I want to make login functionality for my website. But unfortunately it is giving undefined Index:email in my AdminController:
public function login(Request $request)
{
if($request->isMethod('post'))
{
$data = $request->input();
if (Auth::attempt(['email' => $data['email'], 'password' => $data['password'],'admin' => '1'])) {
echo "Success";
//console.log("Successfull");
die;
}
else
{
echo "Failed";
//console.log("Failed");
die;
}
}
return view('admin.admin_login');
}
In Blade:
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text bg-success text-white" id="basic-addon1"><i class="ti-user"></i></span>
</div>
<input type="email" name="email" class="form-control form-control-lg" placeholder="Email" aria-label="Email" aria-describedby="basic-addon1" required="">
</div>
change $data['email'] to $request->email.Because $request contain object not an array
You can do the following
public function login(Request $request)
{
if($request->isMethod('post'))
{
if (Auth::attempt(['email' =>$request->email, 'password' => $request->password,'admin' => '1'])) {
echo "Success";
//console.log("Successfull");
die;
}
else
{
echo "Failed";
//console.log("Failed");
die;
}
}
return view('admin.admin_login');
}
even i dont see password field in your blade template

how to form popup model validation in codeigniter without using javascript

how to form popup model validation in codeigniter without using javascript and jquery
public function login() {
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');
if ($this->form_validation->run() && $this->Login_model->loginn($email, $password)) {
$this->welcome();
} else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
$this->index();
}
}
view page, how to form popup model validation in codeigniter without using javascript and jquery
<form id="register-form" onsubmit ="return validateForm()" action="<?php echo base_url(); ?>Index.php/Login_cntrl/login" method="POST" >
<div class="field-wrap">
<label class="view-label">Email Address</label>
<input type="email" placeholder="Email Address" name="email" id="email" class="input-control" value=""/>
</div>
<div class="field-wrap">
<input type="password" placeholder="Password" name="password" id="password" value="" />
<a href="javascript:void(0)" class="btn btn-link btn-nobg" id="btn-show-forgot" >Forgot ?</a>
</div>
<div class="field-wrap">
<button type="submit" class="btn btn-submit" name="ulogin" id="ulogin" value="ulogin" >Login</button>
</div>
<div class="field-wrap">
NEW User? Sign up
</div>
</form>
model code,how to form popup model validation in codeigniter without using javascript and jquery
public function loginn($email, $password) {
// $this->db->where('email', $email);
$where="(email='$email' or mobile_no='$email') and password='$password'";
$this->db->where($where);
$query = $this->db->get('customer_registration');
$count = $query->num_rows(); //counting result from query
if ($count === 0) {
// $this->db->where('email', $email);
$where="(email='$email' or mobile_no='$email') and password='$password'";
$this->db->where($where);
// $this->db->where('password', $password);
$query = $this->db->get('supplier_registration');
}
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//add all data to session
$newdata = array(
'id' => $row->id,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'email' => $row->email,
'password' => $row->password,
'mobile_number' => $row->mobile_number,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
Below shown code is working properly
public function loginn($email, $password) {
// $this->db->where('email', $email);
$where = "(email='$email' or mobile_no='$email') and password='$password'";
$this->db->where($where);
$query = $this->db->get('customer_registration');
$count = $query->num_rows(); //counting result from query
$tablename = "customer";
if ($count === 0) {
// $this->db->where('email', $email);
$where = "(email='$email' or mobile_no='$email') and password='$password'";
$this->db->where($where);
// $this->db->where('password', $password);
$query = $this->db->get('supplier_registration');
$tablename = "supplier";
}
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//add all data to session
$newdata = array(
'id' => $row->id,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'email' => $row->email,
'password' => $row->password,
'mobile_number' => $row->mobile_number,
'log_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return $tablename;
}
return $tablenam = " '";
}

CodeIgniter insert into two tables

This is my model and i have problem when i go create "pjesma", i choose "izvodjac" in the form, and how to implement this with codeigniter3, i have to insert values into "izvodi_pjesmu", from where do i get "pjesma_id" that i am just inserting, i hope its clear, if you see the EER diagram below you will understand i hope. Thank you.
diagram
controller method:
public function kreiraj()
{
if (isset($_POST['kreiraj']))
{
$data1 = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$data2 = array(
'izvodjac_izvodjac_id' => $this->input->post('izvodjaci')
);
$this->form_validation->set_rules('izvodjaci','Izvođači','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Morate odabrati izvođača');
$this->form_validation->set_rules('naslov', 'Naslov', 'required', array('required' => 'Naslov je obavezan'));
$this->form_validation->set_rules('tablatura', 'Tablatura', 'required', array('required' => 'Tablatura je obavezna'));
if ($this->form_validation->run() != FALSE)
{
$this->pjesma_model->kreiraj($data1, $data2);
redirect('pjesma', 'location');
}
}
$data['izvodjaci'] = $this->izvodjac_model->svi_izvodjaci();
$this->load->view('zaglavlje');
$this->load->view('pjesma_kreiraj', $data);
$this->load->view('podnozje');
}
model method:
public function kreiraj($data1, $data2)
{
$this->db->insert('izvodi_pjesmu', $data2);
$this->db->insert('pjesma', $data1);
}
view:
<h1>Kreiranje pjesme</h1>
<hr>
<div class="row">
<div class="col"></div>
<div class="col-9">
<?php if (validation_errors()): ?>
<div class="alert alert-info">
<?php echo validation_errors(); ?>
</div>
<?php endif; ?>
<?php echo form_open('pjesma/kreiraj'); ?>
<div class="form-group">
<label for="izvodjaci">Izvođač:</label>
<select class="form-control" name="izvodjaci" id="izvodjaci">
<option value="0">odabir izvođača</option>
<?php foreach($izvodjaci as $izvodjac): ?>
<option value="<?php echo $izvodjac->izvodjac_id; ?>"><?php echo $izvodjac->naziv; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="naslov">Naslov:</label>
<input type="text" class="form-control" id="naslov" name="naslov" value="<?php echo set_value('naslov'); ?>" />
</div>
<div class="form-group">
<label for="tablatura">Tablatura:</label>
<textarea class="form-control" id="tablatura" name="tablatura"></textarea>
</div>
<button type="submit" class="btn btn-default" name="kreiraj">Kreiraj pjesmu</button>
<?php echo form_close(); ?>
</div>
<div class="col"></div>
Try this:
Controller:
public function kreiraj()
{
if (isset($_POST['kreiraj']))
{
$data1 = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$this->form_validation->set_rules('izvodjaci','Izvođači','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Morate odabrati izvođača');
$this->form_validation->set_rules('naslov', 'Naslov', 'required', array('required' => 'Naslov je obavezan'));
$this->form_validation->set_rules('tablatura', 'Tablatura', 'required', array('required' => 'Tablatura je obavezna'));
if ($this->form_validation->run() != FALSE)
{
$id= $this->pjesma_model->kreiraj($data1);
$data2 = array(
'izvodjac_izvodjac_id' => $this->input->post('izvodjaci'),
'pjesma_pjesma_id' => $id
);
$this->pjesma_model->pjesma_pjesmu($data2);
redirect('pjesma', 'location');
}
}
$data['izvodjaci'] = $this->izvodjac_model->svi_izvodjaci();
$this->load->view('zaglavlje');
$this->load->view('pjesma_kreiraj', $data);
$this->load->view('podnozje');
}
Model:
public function kreiraj($data1)
{
$this->db->insert('pjesma', $data1);
return $this->db->insert_id();
}
public function pjesma_pjesmu($data)
{
$this->db->insert('izvodi_pjesmu', $data);
return $this->db->insert_id();
}
You can use $this->db->insert_id();
Which will return the last inserted id. Add this line after inserting data in pjesma table which will return 'pjesma_id'. Then you can use it.
Like this -
public function kreiraj($data1, $data2)
{
$this->db->insert('pjesma', $data1);
// ge tlast inserted id
$pjesma_id = $this->db->insert_id();
$izvodjac_izvodjac_id = $data2['izvodjac_izvodjac_id'];
$data = array( 'pjesma_id' => $pjesma_id,
'izvodjac_izvodjac_id' => $izvodjac_izvodjac_id );
$this->db->insert('izvodi_pjesmu', $data);
}
You can do this thing like this, may be this will helps you,
$insert = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$this->db->insert('tabl1', $insert);
$output["insert_id"] = $this->db->insert_id();
if (isset($output["insert_id"])) {
$insert = array(
'field1' => $value1,
'field2' => $value2,
);
$this->db->insert('tabl2', $insert);
}

Using the Remember me feature with Sentry in Laravel 4

I'm trying to get a login form to 'remember' the user logging in and I just can't work out how to do it.
Here's my controller
public function getLogin()
{
// Return the view with the data
return View::make('users.login');
}
public function postLogin()
{
// Gather Sanitized Input
$input = array(
'email' => Binput::get('email'),
'password' => Binput::get('password'),
'rememberMe' => Binput::get('rememberMe')
);
// Set Validation Rules
$rules = array (
'email' => 'required|min:4|max:64|email',
'password' => 'required|min:6'
);
//Run input validation
$v = Validator::make($input, $rules);
if ($v->fails())
{
// Validation has failed
return Redirect::to('users/login')->withErrors($v)->withInput();
}
else
{
try
{
//Check for suspension or banned status
$user = Sentry::getUserProvider()->findByLogin($input['email']);
$throttle = Sentry::getThrottleProvider()->findByUserId($user->id);
$throttle->check();
// Set login credentials
$credentials = array(
'email' => $input['email'],
'password' => $input['password']
);
// Try to authenticate the user
$user = Sentry::authenticate($credentials, $input['rememberMe']);
Sentry::loginAndRemember($user);
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
// Sometimes a user is found, however hashed credentials do
// not match. Therefore a user technically doesn't exist
// by those credentials. Check the error message returned
// for more information.
Session::flash('error', 'Invalid username or password.' );
return Redirect::to('users/login')->withErrors($v)->withInput();
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
echo 'User not activated.';
Session::flash('error', 'You have not yet activated this account.');
return Redirect::to('users/login')->withErrors($v)->withInput();
}
// The following is only required if throttle is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
$time = $throttle->getSuspensionTime();
Session::flash('error', "Your account has been suspended for $time minutes.");
return Redirect::to('users/login')->withErrors($v)->withInput();
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
Session::flash('error', 'You have been banned.');
return Redirect::to('users/login')->withErrors($v)->withInput();
}
return Redirect::to('/');
}
}
/**
* Logout
*/
public function getLogout()
{
Session::flush();
Sentry::logout();
return Redirect::to('/');
}
And here's my View
#extends('layouts/master')
{{-- Web site Title --}}
#section('title')
#stop
{{-- Content --}}
#section('content')
<div class="tck-well span6 offset3">
<h1>Login</h1>
<form class="" action="{{ URL::to('users/login') }}" method="post">
{{ Form::token(); }}
<div class="control-group {{ ($errors->has('email')) ? 'error' : '' }}" for="email">
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input name="email" id="email" value="{{ Request::old('email') }}" type="text" class="input-xlarge" placeholder="E-mail">
{{ ($errors->has('email') ? $errors->first('email') : '') }}
</div>
</div>
<div class="control-group {{ $errors->has('password') ? 'error' : '' }}" for="password">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input name="password" value="" type="password" class="input-xlarge" placeholder="New Password">
{{ ($errors->has('password') ? $errors->first('password') : '') }}
</div>
</div>
<div class="control-group" for"rememberme">
<div class="controls">
<label class="checkbox inline">
<input type="checkbox" name="rememberMe" value="1"> Remember Me
</label>
</div>
</div>
<div class="form-actions">
<input class="button button-large button-secondary" type="submit" value="Log In">
Forgot Password?
</div>
</form>
</div>
#stop
Can someone help point me in the right direction please?
You could also use the helper method:
if( Input::get('rememberMe') ) {
$user = Sentry::authenticateAndRemember($credentials)
} else {
$user = Sentry::authenticate($credentials, false);
}
Similar to Devo's
// Try to log the user in
Sentry::authenticate(Input::only('email', 'password'), Input::get('remember-me', 0));
// For the view page
<input type="checkbox" name="remember-me" id="remember-me" value="1" /> Remember me;
Instead of,
$user = Sentry::authenticate($credentials, $input['rememberMe']);
Use,
if(!empty($input['rememberMe'])) {
$user = Sentry::authenticate($credentials, true);
} else {
$user = Sentry::authenticate($credentials, false);
}
And make sure you are getting some value in $input['rememberMe'].
From GitHub it seems setting gc_maxlifetime in php.ini (or .htaccess) is sometimes necessary as well..
session.gc_maxlifetime = 2592000
In app/config/session.php add this lines:
'lifetime' => 999999,
'expire_on_close' => false,

Resources