Unable to load a form in codeigniter from a controller - php

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!

Related

Codeigniter (3.1.6) Auth does not work in Web Hosting

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]

POST not working because of a not configured correctly htaccess file

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.

Codeigniter view is not loading

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.

Codeigniter Login Issues

I'm having difficulties in understanding what's going on here.
I've made a login page, setting it to go to admin page. Without any user data yet, just to check if it's working. And It doesn't go. I hade some problems loading the library form_validation. So I added the parent construct.
Controller
class Login Extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('login');
$this->load->helper('url');
}
public function login() {
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata($user_session);
redirect('login/admin');
}
}
public function admin() {
$this->load->view('admin');
}
}
My Login View
<section class="login_content">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url().'login/login'; ?>" method="post">
<h1>Login no Sistema</h1>
<div>
<input type="text" name="username" class="form-control" placeholder="Username" required="" />
</div>
<div>
<input type="password" name="password" class="form-control" placeholder="Password" required="" />
</div>
<div>
<input type="submit" name="submit" value="Login" />
</div>
<div class="clearfix"></div>
<div class="separator">
<div class="clearfix"></div>
<br />
<div>
<h1> Sitio Monica e Marcia</h1>
<p>©2016 Todos os direitos reservados. Sitio Monica e Marcia.</p>
</div>
</div>
</form>
<!-- form -->
</section>
My Admin View
<div id="login" class="animate form">
<section class="login_content">
<h1>Bem vindo ao Admin</h1>
<?php
echo '<pre>';
print_r($this->session->all_userdata());
echo '<pre>';
?>
<!-- form -->
</section>
<!-- content -->
</div>
Here Is Your Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Description: Login controller class*/
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
}
public function admin($msg = NULL){
// Load our view to be displayed
// to the user
$data['msg'] = $msg;
if($msg == NULL)
{
$this->load->view('login');
}
else
{
//print_r($data);
//die();
$this->load->view('login',$data);
}
}
public function process(){
// Load the model
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = 'Invalid username or password';
$this->admin($msg);
}else{
// If user did validate,
// Send them to members area
redirect('home/check_isvalidated');
}
}
public function doLogout(){
$this->session->sess_destroy();
redirect(base_url());
}
}
Here IS The Model You Can Use Validation instead of Security
public function validate(){
// grab user input
$username = $this->security->xss_clean($this->input->post('user_name'));
$password = $this->security->xss_clean(md5($this->input->post('password')));
// Prep the query
$this->db->where('user_name', $username);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('admin');
// Let's check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'id' => $row->id,
'user_name' => $row->user_name,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
else {
return false;
}
}
}
Try this
redirect(base_url('login/admin'));
I think, you should check your link and session. This is the code
login.php in controller
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function index() {
//$this->load->view('login');
$this->load->view('login');
}
public function login() {
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata('userlogin',$user_session);
$this->admin();
}
}
public function admin() {
$this->load->view('admin');
}
login.php in view
<form class="form-horizontal" role="form" action="<?php echo 'http://localhost/answers/index.php/login/login'; ?>" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="username">Username:</label>
<div class="col-sm-10">
<input type="username" class="form-control" id="username" name="username" placeholder="Enter username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>
And admin.php in views
<div class="container" height=100%>
<h2>Welcome to the system,
<?php
$this->load->library('session');
$login_session = $this->session->userdata('userlogin');
//$username = $this->session->userdata('userlogin');
echo $login_session['Username'];
?>
</h2>
</div>
May you want to see the complete code like http://explicitphp.blogspot.co.id/2016/03/Codeigniter-Login-System-Tutorial-Without-Database-Connection.html
Hope answer your question. Have fun guys
first, i don't know if the Login.php file or the class Login extends CI_Controller is a reserved name, try changing it.
then you have a public function called login(), this can have problems with the constructor, because in php5 or above you call the constructor like the class name and you maybe override the constructor

Code Igniter form not posting

I have made a site in CodeIgniter2, but I can't get the forms to work, as I can't seem to even work out how to get it to post! Any help? Here is my code and the forms are only on the recommend, contact-us and support pages:
Form:
<div id="mainWhiteBox">
<h3>Tell people about us...</h3>
<p>If you know of a company or individual who need a really great design agency to help them with a project, let them know about us and benefit too. <br /><br />
<span class="customColour">We will give you £50 of Marks & Spencer vouchers for every client you recommend to us who goes on to become a client of xxxxx, it's that simple & there is no limit to the amount of vouchers you can earn!</span></p>
<div id="recommendSomeone">
<?php echo validation_errors(); print_r($_POST);?>
<?php echo form_open('recommend', array('id' => 'recommendForm')); ?>
<label for="friendName">Your Friend's Name</label>
<input type="text" id="friendName" value="<?php echo set_value('friendName'); ?>" />
<label for="friendEmail">Your Friend's Email Address</label>
<input type="email" id="friendEmail" value="<?php echo set_value('friendEmail'); ?>" placeholder="someone#youknow.com" />
<label for="customerName">Your Name</label>
<input type="text" id="customerName" value="<?php echo set_value('customerName'); ?>" />
<label for="customerEmail">Your Email Address</label>
<input type="email" id="customerEmail" value="<?php echo set_value('customerEmail'); ?>" placeholder="you#youremailaddress.com" />
<label for="friendConfirm"><input type="checkbox" id="friendConfirm" value="1" <?php echo set_checkbox('friendConfirm', '1'); ?> />I confirm that I know the person I am recommending above.</label>
<input type="submit" value="Submit Recommendation" />
</form>
<img src="<?=base_url(); ?>images/uploads/<?php echo $images[0]["image_filename"]; ?>" alt="<?php echo $images[0]["image_alt"]; ?>" width="180px" height="300px" class="floatRight" />
</div>
<p class="elevenFont">* Get £50 of Marks & Spencer vouchers per company or person recommended who goes on to open an account with xxxxx.</p>
</div>
<?php include("/home/xxxxx/libraries/application/views/widgets/newsWidget.php"); ?>
<?php include("/home/xxxxx/libraries/application/views/widgets/twitterWidget.php"); ?>
<?php include("/home/xxxxx/libraries/application/views/widgets/quickViewWidget.php"); ?>
<?php include("/home/xxxxx/libraries/application/views/widgets/fbLikePageWidget.php"); ?>
<?php include("/home/xxxxx/libraries/application/views/widgets/getQuoteBarWidget.php"); ?>
<?php include("/home/xxxxx/libraries/application/views/widgets/newsletterSubscribeWidget.php"); ?>
Controller:
<?php
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('pages_model');
}
public function view($page = 'home')
{
if ( ! file_exists('/home/urbanfea/libraries/application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = $this->pages_model->getTitle($page);
$data['showcase'] = $this->pages_model->getShowcase();
$data['news'] = $this->pages_model->getNewsWidgetContent();
$data['quote'] = $this->pages_model->getQuoteFromBank();
$data['images'] = $this->pages_model->getPageImageArray($page);
$data['PageStraplines'] = $this->pages_model->getStraplines($page);
$data['serverStatus'] = $this->pages_model->getIssue("1");
if($page == "support")
{
$this->load->view('templates/supportHead', $data);
}
else
{
$this->load->view('templates/head', $data);
}
if($page == "recommend" || $page == "contact-us" || $page == "support")
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('friendName', 'Friend\'s Name', 'required');
$this->form_validation->set_rules('friendEmail', 'Friend\'s Email Address', 'required');
$this->form_validation->set_rules('customerName', 'Customer\'s Name', 'required');
$this->form_validation->set_rules('customerEmail', 'Customer\'s Email Address', 'required');
//$this->form_validation->set_rules(FriendConfirm', 'Confirm you know the person', 'required');
if ($this->form_validation->run() === true)
{
$this->load->view('templates/formSuccess', $data); echo "a";
}
elseif($this->form_validation->run() === false && validation_errors() != "")
{
$this->load->view('templates/formError', $data); echo "b";
}
elseif($this->form_validation->run() === false)
{
echo "c";
}
}
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Edit
Here are the routes in my router:
$route['404_override'] = '';
$route['user/(:any)'] = 'user/view/$1';
$route['user'] = 'user/login';
$route['our-work/(:any)'] = 'our_work/view/$1';
$route['our-work'] = 'our_work';
$route['what-we-do/(:any)'] = 'what_we_do/view/$1';
$route['what-we-do'] = 'what_we_do';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Your form_open function echo form_open('recommend', array('id' => 'recommendForm')); will create the following output: <form method="post" accept-charset="utf-8" action="http:/example.com/index.php/recommend" />
This is looking for a controller called recommend, which I don't think is what you want. Change the form_open function so it directs your form to the proper controller/action.
Also, it doesn't look like your code is taking full advantage of the MVC framework. Instead of handling passing everything through the same controller/function and having all those if statements to load different views based on what $page is, you should have separate functions for each of those views.
EDIT:
Your form input elements are missing the name attribute. They must have the name attribute to be accessible through $_POST. Take a look at this page in the Codeigniter help. Maybe make use of the form_input function to generate the input fields?

Categories