Codeigniter grocerycrud : 404 Page Not Found - php

When i login to the admin page i got the error 404 Page Not Found.The page you requested was not found.
I am tring to login to the admin page in the following code:
Controller:
public function index()
{
$data = $this->data;
$this->load->view('admin/index.php',$data);
}
public function login()
{
$data = $this->data;
$username=$this->input->post("username");
$password=$this->input->post("password");
print_r($username); die();
$this->load->model('admin/grocery_crud_model');
$res=$this->grocery_crud_model->check_login($username,$password);
if($res==1)
{
$this->load->view('admin/main.php',$data);
}
else
{
$data['error']="Invalid Username or Password";
$this->load->view('admin/index.php',$data);
}
}
public function home()
{
$data = $this->data;
$this->load->view('admin/index.php',$data);
}
Grocery crud model:
public function check_login($username,$password)
{
$query=$this->db->query(" select * from tbl_register where username='".$username."' and password='".$password."'");
return $query->num_rows() ;
}
View:
<form name="login" action="<?php echo site_url('admin/admin/login');?>" method="post">
<div class="col-md2"></div>
<div class="col-md8 lgmid">
<div class="error" style="margin-left:10px; margin-top:35px;"><?php //echo $error;?></div>
<div class="lg"><label><b>Username</b></label> <input type="text" name="username" class="textbox"></div>
<div class="lg"><label><b>Password</b></label> <input style="margin-left:2px" type="password" name="password" class="textbox"><br/></div>
<div align="center"><input type="submit" class="lgbtn" value="Submit" /></div>
</div>
<div class="col-md2"></div>
</form>
Here in model the table used is tbl_register. Is there any problem with the model .Please provide solution for this issue.

You cant load view like this
$this->load->view('admin/index.php',$data); //wrong
you have to load without file extension
$this->load->view('admin/index',$data); //correct
so folder structure would be
application
controllers
model
view
admin
index
and in Model
public function check_login($username,$password)
{
$query=$this->db->query(" select * from tbl_register where username='$username' and password='$password'");
$result = $query->result_array();
$count = count($result);
return $count;
}
EDIT 01
and in form action
Change this to
action="<?php echo site_url('admin/admin/login');?>"
this
action="<?php echo base_url().'admin/admin/login'?>"
in order to use base_url() load url library in autoload.php

remove this
$config['base_url'] = 'http://localhost/ASoft/Projects/AutoGadi2Amy';
$config['base_url'] = '';
just keep empty

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

Not able to pass the session value from view to controller while create login form in CI

controller: Add_user.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add_user extends CI_Controller {
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url', 'captcha', 'email'));
$this->load->model('ads_data');
}
public function home()
{
$data['admin_id'] = $this->session->userdata('admin_id');
print_r($data['admin_id']);
}
}
view: login.php
<?php
if($this->input->post('submit'))
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->db->select('username,password');
$this->db->from('admin');
$where = "username='$username' and password = '$password'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$num = $query->num_rows();
if($num > 0)
{
$this->db->select('username,password,admin_id');
$this->db->from('admin');
$where = "username='$username' and password = '$password'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$this->session->set_userdata('admin_id',$result);
if($result == true)
{
redirect('add_user/home');
}
}
else
{
echo "<p style='color: red;font-weight: 400;margin-right: 60px;'>Wrong email id or password! </p>";
}
}
?>
<form class="form-signin" method="post">
<input type="text" name="username" id="username" class="form-control" placeholder="username" required autofocus>
<input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
<input type="submit" id="submit" name="submit" class="btn btn-lg btn-primary btn-block" value="Sign In">
</form>
In this code I have create a login form in my view where I want when I click on submit button it redirect me on add_user/home as I mention above the code and also pass the session value inside the controller but now I am unable to pass session value. It's working on localhost but not working on live. So, how can I print session value or pass to the controller?
From Comment
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = BASEPATH . 'cache/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Why have you added login code in your view when you are using CI. A preferred way will be to have action attribute of the form tag send the form data to your controller. Then you can perform validation as well as set the session variable.
<form method = 'post 'action = '<?php echo base_url()."/Controller/function"?>'

how to store id or any value in session variable and call session in all pages using ci?

controller: test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url', 'captcha', 'email'));
$this->load->model('Fetch_data');
}
public function student()
{
$this->load->view('student-dashboard/index');
}
}
view: login.php
<?php
if($this->input->post('login'))
{
$email2 = $this->input->post('email2');
$password2 = $this->input->post('password2');
$this->db->select('email,password');
$this->db->from('students');
$where = "email='$email2' and password = '$password2'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$num = $query->num_rows();
if($num >'0')
{
$this->db->select('email,password,student_id');
$this->db->from('students');
$where = "email='$email2' and password = '$password2'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$data['student_id'] = $this->session->set_userdata('student_id',$result);
if($result == true)
{
redirect('/test/student', $data);
}
}
else
{
echo "<p style='color: red;font-weight: bold;'>Wrong email id or password! </p>";
}
}
?>
<form method="post">
<div class="form-group">
<input type="text" name="email2" id="email2" placeholder="Enter Your Email" class="text-line"/>
</div>
<div class="form-group">
<input type="password" name="password2" id="password2" placeholder="Enter Your Password" class="text-line"/>
</div>
<div class="form-group">
<input type="submit" name="login" id="login" value="Login" class="btn btn-warning"/>
</div>
</form>
I am new in codeigniter. In this code I am creating login form and it work perfectly. Now, I want to store student_id into session variable through which I can give welcome message on my all pages. So, How can I do this ? please help me.
Thank You
Change this line
$data['student_id'] = $this->session->set_userdata('student_id',$result['student_id']);
to this one:
$this->session->set_userdata('student_id',$result['student_id']);
$data['student_id'] = $this->session->userdata('student_id');
You have to load session also
$this->load->library('session');
If you want to add/create session data :
$data = array(
'student_id' => 'id',
'name' => 'name',
<!-- Your parameters -->
);
$this->session->set_userdata($data);
In order to use Session library you should load it in the constructor method in each controller you need it as :
$this->load->library('session');
By doing this, session variables will be available for all views you are loading in this controller.
In your view you can give welcome message with :
<?php echo "Hello ".$this->session->userdata('student_id');?>
Anyway, i suggest you to move your php code from the view (login.php) to the controller (test.php). It eases your work
you can stored the data as array like this change field as you want
$session = array(
'isLoggedIn' => TRUE,
'username' => $result['username'],
'roleid' => $result['roleid'],
'id' => $result['id']
);
$this->session->set_userdata($session);
and retrive data like
$roleid=$this->session->userdata('roleid');

Show logged user info in browser, PHP php codeigniter

I want to display current user info from database after login.
Here is my code.
Controller 1 (Welcome)
public function getValues() {
$this->load->model('display_model');
$result=$this->display_model->authenticateUser();
if($result){
$this->session->set_userdata('id', $result['id']);
redirect('/member/home/');
}
else
redirect('/welcome/');
Controller 2 (member)
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
if($this->session->userdata('id')==NULL)
{
redirect('/welcome/');
}
}
public function home() {
$this->load->view('member_home');
}
Model
public function authenticateUser(){
$name =$_POST['name'];
$password=$_POST['password'];
$where=array('name'=>$name,
'password'=>$password);
return $this->db->select('name, password, id')
->from('member')
->where($where)
->get()->row_array();
View 1 (login_view)
<h1>Member Login Form</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('Welcome/getValues'); ?>
Username: <br/>
<input type="text" name="name" /> <br>
Password: <br>
<input type="password" name="password" /> <br>
<input type="submit" value="Login" name="submit"/>
</form>
View 2: After login view (member_home)
<?php
foreach($result as $row){
?>
<li><?php echo $row['first_name'].' '.$row['last_name'];?>
<br/><strong>Email: </strong><?php echo $row['email'];?>
</li>
Now error shows
Message: Undefined variable: result
How to solve it Need Help.. Thanks in Advance
You are not passing the $result value to your home view update the code as follows:
Controller 1:
public function getValues() {
$this->load->model('display_model');
$result=$this->display_model->authenticateUser();
if($result){
$this->session->set_userdata('id', $result['id']);
redirect(member/home/$result['id']);
}
else
redirect('/welcome/');
Controller 2:
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
if($this->session->userdata('id')==NULL)
{
redirect('/welcome/');
}
}
public function home($id = '') {
$data['id'] = $id;
$this->load->view('member_home',$data);
}
NOTE:
you must change the query result from row_array() to result_array() as you are using foreach on the view

CodeIgniter update record

Controler
//class News
public function update($slug)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['news_item']=$this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/update', $data);
//$this->load->view('save',$save);
$this->load->view('templates/footer');
}
Model new_model.php
//class News_model
public function get_news($slug = FALSE)
{
if($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
public function update_news($slug)
{
$query=$this->db->where('slug', $slug);
$this->db->update('news' ,$query);
return $query->row_array();
}
in update.php view file code given below..
view update.php file
<h2>Update New Item</h2>
<?php echo form_open('news/update') ?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $news_item['title']; ?>" readonly/><br>
<label for="text">Text</label>
<textarea name="text" cols="35" rows="16"><?php echo $news_item['text'];?></textarea><br>
save
</form>
data will be fetch but problemb is that when i click on "save" link page not found error generatos why?
how can call this view save.php file..
Change
save
to
<input type="submit" value="save" />
Here you have used link in-place of submit button.
When you use submit button it will post/get data on url that is there in the form action.
Here you can use :
<?php echo form_submit('mysubmit', 'Submit Post!'); ?>
It will produce...
<input type="submit" name="mysubmit" value="Submit Post!" />
For more details : Form Helper

Categories