I am creating a simple login site(first step of the project) and I get error regardless of the information I fill in the username and password forms.
this is my code:
login.php
<?php
class login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
function login_control()
{
$data['title'] = "Login page";
$this->load->view("loginview", $data);
}
function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run())
{
//true
$username = $this->input->post('username');
$password = $this->input->post('password');
//model function
$this->load->model('server_model');
if($this->server_model->can_login($username, $password))
{
$session_data = array('username' => $username);
$this->session->set_userdata($session_data);
redirect(base_url() . 'login/enter');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url() . 'login/login_control');
}
}
else
{
//false
$this->login_control();
}
}
function enter()
{
if($this->session->userdata('username') != '')
{
echo 'Welcome - '. $this->session->userdata('username');
echo '<label>Logout</label>';
}
else
{
redirect(base_url().'login/login_control');
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect(base_url() . 'login/login_control');
}
}
server_model.php
<?php
class server extends CI_Model
{
function can_login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows()>0)
{
return true;
}
else
{
return false;
}
}
}
loginview.php
<!DOCTYPE html>
<html>
<head>
<title>Login | <?php echo $title; ?></title>
<link rel ="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
<div class = "container">
<br /><br /> <br />
<form method="post" action="<?php echo base_url(); ?>login/login_validation">
<div class="form-group">
<label>Enter Username</label>
<input type="text" name="username" class="form-control" />
<span class="text-danger"><?php echo form_error('username'); ?>
</span>
</div>
<div class="form-group">
<label>Enter Password</label>
<input type="password" name="password" class="form-control" />
<span class="text-danger"><?php echo form_error('password'); ?>
</span>
</div>
<div class="form-group">
<input type="submit" name="insert" value="Login" class="btn btn-info" />
<?php
echo '<label class="text-danger">'.$this->session->flashdata("error").'</label>';
?>
</div>
</form>
</div>
</body>
</html>
My .htaccess file looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
my routes.php is like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'login/login_control';
$route['login/login_control'] = 'loginview';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I think the error is probably at my .htaccess or routes files but I have been trying to fix this for hours and I couldnt solve the problem.
Any help appreciated
So as I thought the problem was something really stupid of me. I am using Sublime Text so when I save a file I need to also add the extension .php which apparently I hadn't done. Thanks to everyone who tried on solving this problem although there wasn't any real problem out there.
Related
I recently purchased a web hosting and uploaded my database as well as my system (made with codeigniter).
All is well until I log In.
My website is fine as well as the landing page of my system.
The problem is after I log it shows not found.
Here's my view
<form class="form-horizontal form-material" method="post" id="loginform" action="Login/login_auth">
<div class="form-group m-t-40">
<div class="col-xs-12">
<input class="form-control" name="email" value="<?php if(isset($_COOKIE['email'])) { echo $_COOKIE['email']; } ?>" type="text" required placeholder="Username">
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input class="form-control" name="password" value="<?php if(isset($_COOKIE['password'])) { echo $_COOKIE['password']; } ?>" type="password" required placeholder="Password">
</div>
</div>
<div class="form-check">
<input type="checkbox" name="remember" class="form-check-input" id="remember-me">
<label class="form-check-label" for="remember-me">Remember me plz!</label>
</div>
<div class="form-group text-center m-t-20">
<div class="col-xs-12">
<button class="btn btn-info btn-lg btn-login btn-block text-uppercase waves-effect waves-light" type="submit">Log In</button>
</div>
</div>
</form>
And my Controller As
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('login_model');
$this->load->model('dashboard_model');
}
public function login_auth()
{
$response = array();
//Recieving post input of email, password from request
$email = $this->input->post('email');
$password = sha1($this->input->post('password'));
$remember = $this->input->post('remember');
#Login input validation\
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('email', 'User Email', 'trim|xss_clean|required|min_length[7]');
$this->form_validation->set_rules('password', 'Password', 'trim|xss_clean|required|min_length[6]');
if($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('feedback','UserEmail or Password is Invalid');
redirect(base_url() . 'login', 'refresh');
}
else
{
//Validating login
$login_status = $this->validate_login($email, $password);
$response['login_status'] = $login_status;
if ($login_status == 'success')
{
if($remember)
{
setcookie('email',$email,time() + (86400 * 30));
setcookie('password',$this->input->post('password'),time() + (86400 * 30));
redirect(base_url() . 'login', 'refresh');
}
else
{
if(isset($_COOKIE['email']))
{
setcookie('email',' ');
}
if(isset($_COOKIE['password']))
{
setcookie('password',' ');
}
redirect(base_url() . 'login', 'refresh');
}
}
else
{
$this->session->set_flashdata('feedback','UserEmail or Password is Invalid');
redirect(base_url() . 'login', 'refresh');
}
}
}
}
It works fine in local server (Xampp) but when I placed it in a web hosting it does not work. Any thoughts?
Did you delete the routes index.php in config.php?
if true, you must create a .htaccess file.
Example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have a form that is well structured and well coded, but when i use the post to send data to my controller.. the variables recieving the post data are empty. I tryed user an alert to see if the problem was the input fields and used a vardump on the controller varibles recieving the data and the result was 0.
After i realized every thing was ok.. the only thing that was not and the only thing i don't really understand how it works is the .htaccess file.
PS: the corrent .htaccess just removes the index.php file from the url of the codeigniter framework!
So resuming how do i enable any "data request (post, get, etc..)" from my tradition htaccess file below :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Controller code [EDITED]
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url'); //obligatory
$this->load->database();
$this->load->model('Login_model');
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index()
{
//sending $data array of variables to view's or to model's
$data = array('header_title' => 'Nova Democracia - Login');
$this->load->view("login", $data);
}
public function login_validation(){
$username = $this->input->POST('username');
$password = $this->input->POST('password');
//echo "username - " + $username;
//echo "password - " + $password;
//break;
if($username != '' && $password != '')
{
$validate = $this->Login_model->validate($username, $password);
if($validate->num_rows > 0){
$data = $validate->row_array();
$nome = $data['nome'];
$apelido = $data['apelido'];
$id = $data['id'];
$username = $data['username'];
$nivel = $data['nivel'];
$sesdata = array(
'nome' => $nome,
'apelido' => $apelido,
'username' => $username,
'nivel' => $nivel,
'id' => $id,
'logged_in' => TRUE
);
$this->session->set_userdata($sesdata);
// access login for admin
if($nivel == '0'){
redirect(base_url() . 'Admin_Controller');
// access login for staff
}elseif($nivel == '1'){
redirect(base_url() . 'Admin_Controller/1');
// access login for author
}elseif($nivel == '2'){
redirect(base_url() . 'Admin_Controller/2');
}
}else{
echo $this->session->set_flashdata('msg','Username - '.$username.' or Password - '.$password.' is Wrong');
redirect(base_url().'Login_Controller');
}
}else
{
echo $this->session->set_flashdata('msg','Os campos não podem estar em branco');
redirect(base_url().'Login_Controller');
}
}
function logout(){
$this->session->sess_destroy();
redirect(base_url().'Login_Controller');
}
}
View code [EDITED]
<!DOCTYPE html>
<html>
<head>
<title><?php echo $header_title; ?></title>
<!-- include of site header css, js, links, metas, etc-->
<?php $this->load->view('includes/header'); ?>
</head>
<body class="bg-gradient-success">
<div class="container">
<!-- Outer Row -->
<div class="row justify-content-center">
<div class="col-xl-10 col-lg-12 col-md-9">
<div class="card o-hidden border-0 shadow-lg my-5" style="margin-bottom: 0 !important;">
<div class="card-body p-0">
<!-- Nested Row within Card Body -->
<div class="row">
<div class="col-lg-6 d-none d-lg-block bg-login-image" style="background-image: url('<?php echo base_url(); ?>assets/images/login_img/bg1.jpg');"></div>
<div class="col-lg-6">
<div class="p-5">
<div class="text-center">
<!-- <h1 class="h4 text-gray-900 mb-4">Eleições <?php echo date('Y'); ?></h1> -->
<h1 class="h4 text-gray-900 mb-4 nexa_bold">Administração</h1>
</div>
<form class="user" method="POST" action="<?php echo site_url('index.php/Login_Controller/login_validation'); ?>">
<div class="form-group">
<input type="text" class="form-control form-control-user" name="username" id="username" aria-describedby="userHelp" placeholder="Nome">
<span class="text-danger"><?php echo form_error('username'); ?></span>
</div>
<div class="form-group">
<input type="password" class="form-control form-control-user" name="password" id="password" placeholder="Senha">
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group">
<div class="custom-control custom-checkbox small">
<input type="checkbox" class="custom-control-input" id="customCheck">
<label class="custom-control-label" for="customCheck">Lembrar</label>
</div>
</div>
<input type="submit" id="buttao" name="submit" class="btn btn-success btn-user btn-block" value="Login">
<?php echo '<p class="text-danger text-center mt-2">'. $this->session->flashdata('msg') .'</p>'; ?>
</form>
<hr>
<div class="text-center">
<a class="small text-success" href="#">Clique aqui, em casos de reclamações!</a>
</div>
</div>
</div>
</div>
</div>
</div>
<h6 class="text-white">Desenvolvido pela : brandigniters</h6>
</div>
</div>
</div>
<?php $this->load->view('includes/footer'); ?>
</body>
<script type="text/javascript">
$(document).ready(function(){
$('#buttao').click(function(){
alert("Username - " + $('#username').val() + " Password - " + $('#password').val());
});
});
</script>
</html>
Model code [EDITED]
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate($username, $password){
$this->db->where('username', $username);
$this->db->where('password', $password);
$result = $this->db->get('tbl_admin_users', 1);
return $result;
}
}
I resolved my issue changing the model to :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate($username, $password){
$this->db->select('*');
$this->db->from('tbl_admin_users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get();
return $query;
}
}
And the Controller function to :
public function login_validation(){
$username = $this->input->POST('username');
$password = $this->input->POST('password');
if($username != NULL && $password != NULL)
{
$validate = $this->Login_model->validate($username, $password);
$rows = $validate->num_rows();
if($rows > 0){
$data = $validate->row_array();
$nome = $data['nome'];
$apelido = $data['apelido'];
$id = $data['id'];
$username = $data['username'];
$nivel = $data['nivel'];
$sesdata = array(
'nome' => $nome,
'apelido' => $apelido,
'username' => $username,
'nivel' => $nivel,
'id' => $id,
'logged_in' => TRUE
);
$this->session->set_userdata($sesdata);
// access login for admin
if($nivel == '0'){
redirect(base_url() . 'Admin_Controller');
// access login for staff
}elseif($nivel == '1'){
redirect(base_url() . 'Admin_Controller/1');
// access login for author
}elseif($nivel == '2'){
redirect(base_url() . 'Admin_Controller/2');
}
}else{
echo $this->session->set_flashdata('msg','Username - '.$username.' or Password - '.$password.' is Wrong');
redirect(base_url().'Login_Controller');
}
}else
{
echo $this->session->set_flashdata('msg','Nenhum dos campos pode estar em branco !');
redirect(base_url().'Login_Controller');
}
}
also changed the .htaccess file to :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# ALLOW ONLY NECESSARY REQUEST METHODS
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|OPTIONS|POST|PROPFIND|PUT) [NC]
RewriteRule .* - [F,L]
Glad you found your answer. I have a recommendation for you to simplify the controller code. Put it in an answer because using a comment is too hard.
You do not need to copy the array returned by $validate->row_array() because you use all the same index keys. All you need to do is add the "logged_in' key/value to $data.
// call num_rows() in the if condition
// no need for an extra variable
//$rows = $validate->num_rows();
// $model might return FALSE if something goes wrong in the database
// always make sure it's not FALSE before making a call any result method.
if($validate && $validate->num_rows())
{
$data = $validate->row_array();
$data['logged_in'] = TRUE;
}
$this->session->set_userdata($data);
Also, in the model, you do not need function __construct(). The only time you need to call parent::__construct(); is if the child class does some work in the constructor. This is true for any PHP class that extends for a parent.
I have a simple sign up/Login registration project in php/codeigniter
and I always get this particular error while clicking on the Sign in or the Register button
Error: "The requested URL /CI/user/login was not found on this server."
Here is the Controller file user.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
if(($this->session->userdata('user_name')!=""))
{
$this->welcome();
}
else{
$data['title']= 'Home';
$this->load->view('header_view',$data);
$this->load->view("registration_view.php", $data);
$this->load->view('footer_view',$data);
}}
public function welcome()
{
$data['title']= 'Welcome';
$this->load->view('header_view',$data);
$this->load->view('welcome_view.php', $data);
$this->load->view('footer_view',$data);
}
public function login()
{
$email=$this->input->post('email');
$password=md5($this->input->post('pass'));
$result=$this->user_model->login($email,$password);
if($result) $this->welcome();
else $this->index();
}
public function thank()
{
$data['title']= 'Thank';
$this->load->view('header_view',$data);
$this->load->view('thank_view.php', $data);
$this->load->view('footer_view',$data);
}
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->user_model->add_user();
$this->thank();
}
}
public function logout()
{
$newdata = array(
'user_id' =>'',
'user_name' =>'',
'user_email' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
$this->session->sess_destroy();
$this->index();
}
}
?>
Here is the view file registration_view.php
<html>
<div id="content">
<div class="signup_wrap">
<div class="signin_form">
<?php echo form_open("user/login"); ?>
<label for="email">Email:</label>
<input type="text" id="email" name="email" value=""/>
<label for="password">Password:</label>
<input type="password" id="pass" name="pass" value=""/>
<input type="submit" class="" value="Sign in"/>
<?php echo form_close(); ?>
</div><!--<div class="signin_form">-->
</div><!--<div class="signup_wrap">-->
<div class="reg_form">
<div class="form_title">Sign Up</div>
<div class="form_sub_title">It's free and anyone can join</div>
<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open("user/registration");?>
<p>
<label for="user_name">User Name:</label>
<input type="text" id="user_name" name="user_name" value="<?php echo set_value('user_name'); ?>" />
</p>
<p>
<label for="email_address">Your Email:</label>
<input type="text" id="email_address" name="email_address" value="<?php echo set_value('email_address'); ?>" />
</p>
<p>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="<?php echo set_value('password'); ?>" />
</p>
<p>
<label for="con_password">Confirm Password:</label>
<input type="password" id="con_password" name="con_password" value="<?php echo set_value('con_password'); ?>" />
</p>
<p>
<input type="submit" class="greenButton" value="Submit" />
</p>
<?php echo form_close(); ?>
</div><!--<div class="reg_form">-->
</div><!--<div id="content">-->
</html>
I think the main problem is with form_open ('form');
Here is the htacess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
You will need to pass complete url to the form_open function like this
echo form_open(base_url()."/user/login");
change this:
<?php echo form_open("user/login"); ?>
to this:
<?php echo form_open(base_url()."User/login"); ?>
in config:
$config['base_url'] = 'http://localhost/projectname/';
$config['index_page'] = '';
after that if doesn't work call your method from url if work correct post data to your method!!!
EDIT: in your htaccess file, change to. (Another suggestion, no guarantee)
RewriteBase /project
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
Check your /etc/apache2/apache2.conf or /etc/apache2/httpd.conf whichever exists. Basically, apache does not acknowledge the .htaccess file unless you declare the AllowOverride directive.
So, you have to open up the said config file, and add this to the bottom, if your full path to the sub-directory is /var/www/html/subdirectory/
<Directory /var/www/html/subdirectory/>
AllowOverride all
</Directory>
And don't forget to restart apache!
I'm using codeigniter for a login form validation. The code was previously working fine, but when I made some modifications, it just do not work now.
When I try to type in wrong or leave blank for password or username, the error message is shown as normal, but when I type in the correct username and password, $this->form_validation->run() just give me FALSE with an empty validation_errors() string.
I've tried to restore my old working controller php, but this error resists.
Full code related is available at
User Controller http://pastebin.com/nt2SDVnv
Login View http://pastebin.com/9hEc0EJB
User Model http://pastebin.com/p1z0zLM8
Only related code are pasted below:
Controller:
<?php
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('cookie');
}
function login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
$this->_username = $this->input->post('username'); //用户名
$remember_me = $this->input->post('remember_me');
$json_string = $this->input->cookie('userinfo');
$userinfo_json = json_decode($json_string);
if(isset($userinfo_json->username)){
if ($this->username_check($userinfo_json->username)){
$this->user_model->login($userinfo);
redirect('admin/dashboard');
}
}
if ($this->form_validation->run() == FALSE){
$this->load->view('account/login');
} else {
$userinfo=$this->user_model->get_by_username($this->_username);
$this->user_model->login($userinfo);
if($remember_me=="on"){$this->user_model->write_session($userinfo);}
redirect('admin/dashboard');
}
}
function username_check($username){
if ($this->user_model->get_by_username($username)){
return TRUE;
}else{
$this->form_validation->set_message('username_check', 'User name not exist.');
return FALSE;
}
}
function password_check($password) {
$password = md5($password);
if ($this->user_model->password_check($this->_username, $password)){
return TRUE;
}else{
$this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
return FALSE;
}
}
}
View:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login - <?=$this->admin_model->get_title();?></title>
<?php $this->load->view('gy/head');?>
</head>
<body>
<?php $this->load->view('gy/header');?>
<div class="hero-unit header">
<div class="container">
<div style="text-align:center;">
<h1>Sign in</h1>
<p class="lead">Log into <?=$this->admin_model->get_title();?>.</p>
</div>
</div>
</div>
<div class="container">
<div class="span5 offset3">
<?php if(validation_errors() !== '' || #(!$err_message == '')){ ?>
<div class="alert alert-error fade in">
×
<strong>Error!</strong> <?=validation_errors('<span>','</span>');?> <?=#$err_message?>
</div>
<?php } ?>
<?php if(#$message!=''){ ?>
<div class="alert fade in alert-success">
×
<strong>Success!</strong> <?=$message?>
</div>
<?php } ?>
<?php echo form_open('login',array('class'=>'form-horizontal')); ?>
<div class="control-group">
<label class="control-label" for="username">User name</label>
<div class="controls">
<input type="text" id="username" name="username" placeholder="User name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password">
</div>
</div>
<label for="remember_me" class="checkbox">
<input type="checkbox" id="remember_me" name="remember_me"> Remember me (30 days)
</label>
<div style="text-align:center;">
<input type="submit" name="submit" value="Log in" class="btn btn-primary">
</div>
</Form>
</div>
</div>
<?php $this->load->view('gy/footer');?>
</body>
</html>
Model:
<?php
class user_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
}
function login($userinfo)
{
$data = array('username'=>$userinfo->username,
'user_id'=>$userinfo->id,
'role'=>$userinfo->role,
'logged_in'=>TRUE);
$this->session->set_userdata($data);
}
function write_session($userinfo)
{
$user_json = json_encode($userinfo);
$cookie = array(
'name' => 'userinfo',
'value' => $user_json,
'expire' => '2592000',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
}
function get_by_username($username)
{
$this->db->where('username', $username);
$query = $this->db->get('users');
if ($query->num_rows() == 1)
{
return $query->row();
}
else
{
return FALSE;
}
}
function password_check($username, $password)
{
if($user = $this->get_by_username($username))
{
return $user->password == $password ? TRUE : FALSE;
}
return FALSE;
}
}
I suspect the trim rule. It doesn't return a Boolean and I suppose you haven't set any error message for that rule. trim() should be run on the username and password, after the validation runs successful. If you just wanna check if the input has blank spaces You could add a rule to check with the strpos() but again in a custom callback. Just remember to use the Identical opperator '===' instead of the equal '==' .
Controller :
$this->form_validation->set_rules('username','Username','callback_space_check|required|xss_clean|callback_username_check');
function space_check($input){
if(strpos(' ',$input)===false){
return TRUE;
}else{
$this->form_validation->set_message('space_check', '%s contains space.');
return FALSE;
}
}
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>