form-validation in codeigniter - php

<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="password" name="password"/>
<br/>
<input type="submit" value="Login"/>
</form>
I use that code to validate a filled in form but the browser shows nothing after I submit. The URL of my browser is stuck at http://example.com/index.php/verifylogin
verifylogin.php is defined like this
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', '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
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('home', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>

class Auth extends CI_Controller{
public function __construct(){parent::__construct();}
/**
* Login Form
*
* $route['login'] = 'auth/login_form';
*
*/
public function login_form(){
$this->load->view('login_form');
}
/**
* Login Validation
*
* $route['login/check'] = 'auth/login_check';
* Point your form to login/check
*/
public function login_check()
{
if($this->form_validation->run() == TRUE)
{
//form validates...
}else
{
//no redirect! just show for again!
$this->login_form();
}
}
}

Related

My controller for register and login is not working (Codeigniter)

please help me... My controller for registration and login is not working. Whenever I input the data in either login or register it will back to register and login view and not the index/home nor the data that I input enter mysql.
I create it like when I success in inputting the data on register it will direct to login then when you login it will direct to home. Other is login, when I login it will direct to home if it can login.
Controller: Member.php
class Member extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function index() {
$this->load->view('front/login');
}
public function Login() {
$this->load->view('front/login');
}
public function Register() {
$this->load->view('front/register');
}
public function profile() {
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error","Please login first to view");
redirect('Member/Login');
}
$this->load->view('front/home');
}
}
Controller: Register.php
class Register extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function registerMember() {
//validate the data taken through the register form
$this->form_validation->set_rules('username','Username','required|is_unique[member.username]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5|min_length[6]');
$this->form_validation->set_rules('conf_password', 'Confirm Password', 'trim|required|min_length[6]|matches[password]');
if ($this->form_validation->run() == TRUE) {
//load the model to connect to the db
$this->load->model('Member_model');
$this->Member_model->insertMember();
//set message to be shown when registration is completed
$this->session->set_flashdata('success','You are registered!');
redirect('Member/Login');
} else {
$this->load->view('front/register');
}
}
}
Controller: Login.php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function loginMember() {
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('front/login');
} else {
$this->load->model('Member_model');
$reslt = $this->Member_model->checkLogin();
if ($reslt != false) {
//set session
$username = $_POST['username'];
$password = sha1($_POST['password']);
//fetch from databse
$this->db->select('*');
$this->db->from('member');
$this->db->where(array('username' => $username , 'password' => $password));
$query = $this->db->get();
$member = $query->row();
//if use exists
if ($member->username) {
//login message
$this->session->set_flashdata("success","You are logged in");
//set session variables
$_SESSION['user_logged'] = TRUE;
$_SESSION['username'] = $member->username;
//redirect
redirect('Member/profile','refresh');
}
} else {
//wrong credentials
$this->session->set_flashdata('error','Username or Password invalid!');
redirect('Member/Login');
}
}
}
//logging out of a user
public function logoutMember() {
unset($_SESSION);
redirect('Member/Login');
}
}
Model: Member_model.php
class Member_model extends CI_Model {
public function insertMember () {
//insert data
$data = array(
//assign data into array elements
'username' => $this->input->post('username'),
'email' =>$this->input->post('email'),
'password' => sha1($this->input->post('password'))
);
//insert data to the database
$this->db->insert('member',$data);
}
public function checkLogin() {
//enter username and password
$username = $this->input->post('username',TRUE);
$password = sha1($this->input->post('password',TRUE));
//fetch data from database
$this->db->where('username',$username);
$this->db->where('password',$password);
$res = $this->db->get('member');
//check if there's a user with the above inputs
if ($res->num_rows() == 1) {
//retrieve the details of the user
return $res->result();
} else {
return false;
}
}}
View:
Register.php
<body class="background-login">
<div class="main-w3layouts wrapper">
<h1> SignUp </h1>
<div class="main-agileinfo">
<div class="agileits-top">
<form method="post" action="<?php echo site_url('register/registerMember'); ?>" >
<input class="text" type="text" id="username" name="username" placeholder="Enter a username">
<input class="text email" type="email" id="email" name="email" placeholder="Enter your email">
<input class="text" type="password" id="password" name="password" placeholder="Enter a password">
<input class="text w3lpass" type="password" id="conf_password" name="conf_password" placeholder="Confirm your password">
<div class="wthree-text">
<label class="anim">
<input type="checkbox" class="checkbox" required="">
<span>I Agree To The Terms & Conditions</span>
</label>
<div class="clear"> </div>
</div>
<input type="submit" value="SignUp">
</form>
<p>Already have an Account? Login Now!</p>
</div>
</div>
Login.php
<body class="background-login">
<div class="main-w3layouts wrapper">
<h1> SignIn </h1>
<div class="main-agileinfo">
<div class="agileits-top">
<form method="post" action="<?php echo site_url('Login/loginMember'); ?>" >
<input class="text" type="text" id="username" name="username" placeholder="Your username"><br>
<input class="text" type="password" id="password" name="password" placeholder="Your password">
<input type="submit" value="Login"/>
</form>
<p>Don't have an Account? SignUp NOW!</p>
</div>
</div>
I even chop my code into part like this, but the problem still the same... Is just like the if form_validation->run is not running and just got cut to else...
So the problem is:
When I input data it will not enter the data or redirect to another page.
*register -> it will direct to register after i submit the data
What I want is when I submit the data it will direct to login.
*login -> it will direct to login after i submit the data
What I want is when I submit the data it will direct to home.
-> result ->
I also had this case . I solved it by passing $this in to run()
if ($this->form_validation->run($this) == TRUE)
You can pass array in where condition in checkLogin function
$where_array = array('username' => $username,'password' => $password);
$this->db->where($where_array);
$res = $this->db->get('member');
Hope it help you too!
You can not use the form_open function and html form tag at the same time, so remove anyone from it and pass the whole path in the action.
If you use form_open function then please add the form_close function.
in your register and login view
<?= form_open() ?>
<form action="#" method="post">
you added this. this is wrong.at a same time you open two form you need to remove a line.and add some action to form for example
<form action="<?= base_url('yourControllerName/YourMethodname')?>" method='post'></form>
and second thing in Member.php Controller you added this line
if ($this->form_validation->run() === FALSE){
Some code
}
this is wrong you need this
if ($this->form_validation->run() == FALSE)
please check and ping me......
Make sure your base_url in config.php is not null.
In register.php remove <?=form_open('member/login')?> and <?= form_close() ?>
Instead add, <form method="post" action="<?php echo site_url('Member/register'); ?>" >
and
</form>
redirect your page:
//login
if ($this->form_validation->run() === TRUE)
{
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
$user= $this->member_model->create_user();
if($user >0){
redirect('front/home');
} else {
redirect('front/login');
}
}
you can same as in singup

Error In Login In Codeigniter

Hi I am trying to make login code in codeigniter it gives following error
Unable to access an error message corresponding to your field name
username.
Unable to access an error message corresponding to your
field name password.
Class Verifylogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', '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
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('Home', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
echo $username = $this->input->post('username');
//query the database
echo $result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
View:
<html xmlns="w3.org/1999/xhtml">;
<head>
<title>Admin Panel</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open( 'verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" />
<br/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="password" name="password" />
<br/>
<input type="submit" value="Login" /> </form>
</body>
</html>

Codeigniter form validation is not showing errors

I'm new to Codegniter so go easy on me. I'm building a simple login form and I have successfully redirected to a page when the login credentials are correct. However, if I submit an empty form I get no error messages. I'm also using set_value in the form field and codeigniter does not refill what the user inputted once the form is submitted. Somehow that data is being cleared. Here are a few things i've done for clarity sake.
Auto-loaded the form_validation library
Auto-loaded form helper
Echoed validation_errors above form
account.php (controller)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Account extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user');
}
public function index() {
$this->load->view('home');
}
public function validate() {
$this->form_validation->set_rules('username', 'username', 'xss_clean|required');
$this->form_validation->set_rules('password', 'password', 'xss_clean|required|md5');
$user_data = array(
'username' => $this->input->post('username'),
'password' => MD5($this->input->post('password'))
);
if ($this->form_validation->run() == FALSE)
{
$data['title'] = "Login Fool";
$this->load->view('templates/header', $data);
$data['contents'] = $this->load->view('login_view', $data, TRUE);
$this->load->view('page', $data);
$this->load->view('templates/footer');
}
else
{
$validated = $this->user->validate($user_data['username'], $user_data['password']);
if($validated){
redirect(base_url() . 'account');
}
else{
$this->session->set_flashdata('LoginError', 'Sorry, your credentials are incorrect.');
redirect(base_url() . 'account/login');
}
}
}
public function login() {
$data['title'] = "Login Fool";
$this->load->view('templates/header', $data);
$data['contents'] = $this->load->view('login_view', NULL, TRUE);
$this->load->view('page', $data);
$this->load->view('templates/footer');
}
}
login_view.php (view)
<h1>Login Now</h1>
<?php
if(validation_errors() != false) {
echo "<div id='errors'>" . validation_errors() . "</div>" ;
}
?>
<?= ($this->session->flashdata('LoginError')) ? '<p class="LoginError">' . $this->session->flashdata('LoginError') . '</p>' : NULL ?>
<?php echo form_open('account/validate'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" value="<?php echo set_value('username'); ?>"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password" value="<?php echo set_value('password'); ?>"/>
<br/>
<input type="submit" value="Login"/>
</form>
Any ideas why the errors won't show above the form?
Turns out my model was the culprit. It was extending CI_Controller not CI_Model. Thanks to everyone who took a look at this. This code works.

how to set RememberMe CodeIgniter Spark

I am trying to set the joeauty / RememberMe-CodeIgniter-Spark. I added the rememberme.php inside the config forler, the Rememberme.php inside system/libraries/ made the changes inside autoload.php and config.php and created 2 tables( ci_cookies and ci_sessions) into the database.
If don't click the checkbox I can login, but if I select the checkbox nothing happens.
This is my controller:
function __construct()
{
parent::__construct();
$this->load->model('registerclient_model','',TRUE);
}
function index()
{
if($this->session->userdata('logged_in') || $this->session->userdata('user_id'))
{ redirect('client_private_area', 'refresh');}
else{
$this->load->library('form_validation');
$this->form_validation->set_rules('email_address', '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)
{
$data['error'] = 'Invalid email address and/or password.';
$this->load->view('templates/header');
$this->load->view('pages/login/client_login', $data);
$this->load->view('templates/footer');
}
else
{
//Go to private area
redirect('client_private_area', 'refresh');
}
}
}
function check_database($password)
{
$email = $this->input->post('email_address');
$result = $this->registerclient_model->login($email, $password);
if($result){
if($this->input->post('netid') == "on"){
$this->rememberme->setCookie($this->input->post('netid'));
if ($this->rememberme->verifyCookie()) {
// find user id of cookie_user stored in application database
$user = User::findUser($cookie_user);
// set session if necessary
if (!$this->session->userdata('user_id')) {
$this->session->set_userdata('user_id', $user);
}
$this->user = $user;
}
else if ($this->session->userdata('user_id')) {
$this->user = $this->session->userdata('user_id');
}
}
else
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'first_name' => $row->first_name,
'email_address' => $row->email_address
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
}
else
{
$this->form_validation->set_message('check_database', 'Invalid email address and/or password.');
return false;
}
}
this is my model:
function login($email, $password) {
//create query to connect user login database
$this->db->select('id, first_name, email_address, password');
$this->db->from('client_register');
$this->db->where('email_address', $email);
$this->db->where('password', $this->registerclient_model->hash($password));
$this->db->limit(1);
//get query and processing
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->result(); //if data is true
}
else
{
return false; //if data is wrong
}
}
this is my view:
<div class="client_login_content_form">
<h1>CLIENT LOGIN FORM</h1>
<p class="loginform_error"><?php echo validation_errors(''); ?></p>
<?php echo form_open('verifylogin'); ?>
<ul>
<li><input type="text" size="20" id="email" name="email_address" value="<?php echo set_value('email_address'); ?>" required placeholder="Email Address"/></li>
<li><input type="password" size="20" id="passowrd" name="password" value="<?php echo set_value('password'); ?>" required placeholder="Password"/></li>
<li><p><input type="checkbox" name="netid" id="netid" checked>Remember me</p></li>
<li><input type="submit" class="login_content_form_button" value="LOG IN"/></li>
</ul>
<p class="forgot_login">Forgot your password?</p>
</form>
</div>
<form action="<?php echo site_url('admin'); ?>"><input type="submit" value="Admin" class="admin_button" /></form>

log in not working 404 error

have a look at this code i keep getting an error 404 The requested URL not found
The requested URL /Survay_Test/verifylogin was not found on this server. have no idea what could be wrong.
can anyone help? here is my code
Controller : verifylogin.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Verifylogin extends CI_Controller {
function index()
{
//This method will have the credentials validation
$this->load->model('user','',TRUE);
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', '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
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('home', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('home_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
//redirect('home', 'refresh');
}
}
?>
model : user.php
<?php
Class User extends CI_Model
{
function login($username, $password)
{
$data['main_content'] = 'login_view';
$this->load->view('includes/template', $data);
$this->db->select('id, username, password');
$this->db->from('membership');
$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;
}
}
}
?>
View : login_view.php
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password"/>
<br/>
<input type="submit" value="Login"/>
</form>
</body>
Small Fix, you have to start controller name with lowercase like this...
<?php echo form_open('verifyLogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" />
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password" />
<br/>
<input type="submit" value="Login" />
</form>

Categories