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;
}
}
Related
I'm trying to create some admin login. I think my code is correct because when I input wrong email and password the error message appears on login page. However, when I input correct data of my DB, it also shows error message.
Please help me. I appreciate any answer.
my controller (Admin.php):
public function index()
{
$this->admindashboard();
}
public function admindashboard()
{
$data = array();
/* $data['main_content'] = $this->load->view('admin_main', '', TRUE); */
$this->load->view('admin/admin_main', $data);
}
public function admin_regst()
{
$data = array();
$data['main_content'] = $this->load->view('admin-regist', '', TRUE);
$this->load->view('admin/admin_main', $data);
}
My other controller (Login_admin.php):
public function index()
{
$this->load->view('login');
}
public function adminchecklogin()
{
$data = array();
$adminemail = $this->input->post('admin_email', TRUE);
$adminpassword = $this->input->post('admin_psw', TRUE);
$this->load->model('M_login_admin');
$admindetails = $this->M_login_admin->admin_login_check($adminemail);
if (password_verify($adminpassword, $admindetails->admin_psw)) {
if ($admindetails->admin_status == 1) {
$session_data['adminid'] = $admindetails->admin_id;
$session_data['adminemail'] = $admindetails->admin_email;
$session_data['adminusername'] = $admindetails->admin_username;
$session_data['adminstatus'] = $admindetails->admin_status;
$this->session->set_userdata($session_data);
redirect('Admin');
} else {
$data['error_msg'] = "User ini tidak aktif....!!!";
redirect('login', $data);
}
} else {
redirect('error-login', $data);
}
}
public function login_error()
{
$data['error_msg'] = "Email atau Password Anda Salah....!!!";
$this->load->view('login', $data);
}
my model (M_login_admin.php):
public function admin_login_check($adminemail)
{
$admin_details = $this->db->select('*')
->from('admin')
->where('admin_email', $adminemail)
->get()
->row();
return $admin_details;
}
my view(login.php):
<body>
<section class="hero is-fullheight">
<div class="hero-body container has-text-centered">
<div class="login">
<img src="https://logoipsum.com/logo/logo-1.svg" width="325px" />
<p>
<?php
if (isset($success_msg)) {
echo $success_msg;
}
?>
</p>
<p>
<?php
if (isset($error_msg)) {
echo $error_msg;
}
?>
</p>
<form action="<?= base_url(); ?>Login_admin/adminchecklogin" method="POST">
<div class="box">
<div class="field">
<div class="control">
<input class="input is-medium is-rounded" type="email" placeholder="hello#example.com" autocomplete="username" name="admin_email" required />
</div>
</div>
<div class="field">
<div class="control">
<input class="input is-medium is-rounded" type="password" placeholder="**********" autocomplete="current-password" name="admin_psw" required />
</div>
</div>
<div class="field has-text-left ml-3 mt-5">
<label class="checkbox">
<input type="checkbox">
Remember me
</label>
</div>
</div>
<button class="button is-block is-fullwidth is-primary is-medium is-rounded" type="submit">
Login
</button>
</form>
<br>
</div>
</div>
</section>
My route
DB Column
DB Data
Interface
Your database has the admin_psw in plain text that would explain why the password_verify() is failing.
In order to use password_verify(), you have to have hashed the password using password_hash() This would normally be done when the user first registers or any time the user changes their password
Check the documentation for password_hash() in the PHP manual
i try to login by using this simple code but after i click the login button the else choice "Invalid Username or Password!" always appear even the username and password is match
Controller :
class Auth100 extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('AuthModel100');
}
public function login(){
if($this->input->post('login') && $this->validation('login')){
$login=$this->AuthModel100->getuser($this->input->post('username'));
if($login !=NULL){
if(password_verify($this->input->post('password'), $login->password_100)){
}
}
$this->session->set_flashdata('msg','Invalid Username or Password!');
}
$this->load->view('auth100/form_login_100');
}
public function logout(){
redirect('auth100/login');
}
public function changepass(){
if($this->input->post('change') && $this->validation('change')){
redirect('welcome');
}
$this->load->view('auth100/form_password_100');
}
public function validation($type){
$this->load->library('form_validation');
if($type=='login'){
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
} else {
$this->form_validation->set_rules('oldpassword', 'Old Password', 'required');
$this->form_validation->set_rules('newpassword', 'New Password', 'required');
}
if($this->form_validation->run())
return TRUE;
else
return FALSE;
}
Model :
class AuthModel100 extends CI_Model{
public function getuser($username){
$this->db->where('username_100', $username);
return $this->db->get('users100')->row();
}
}
View :
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Catshop100 | Login</title>
</head>
<body>
<div class="container" style="width:50%;">
<h3>Kitty Fan Shop - Login</h3>
<hr>
<div style="color: red;"><?=validation_errors()?></div>
<form action="" class="form-horizontal" method="post">
<div class="form-group">
<label for="username" class="control-label col-sm-2">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username">
</div>
</div>
<div class="form-group">
<label for="password" class="control-label col-sm-2">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" name="login" value="Login" class="btn btn-default">
</div>
</div>
</form>
</div>
</body>
</html>
Database :
please help me to fix this
You're getting an error because the error code is not in the else condition, I've written the corrected code below with necessary comments. See if it helps you.
public function login(){
if($this->input->post('login') && $this->validation('login')){
$login=$this->AuthModel100->getuser($this->input->post('username'));
if($login !=NULL){
if(password_verify($this->input->post('password'), $login->password_100)){ // success
// do something -- redirect to dashboard -- your logic
}else{ // fail
$this->session->set_flashdata('msg','Invalid Username or Password!');
$this->load->view('auth100/form_login_100');
}
}else{
// do something if $login is NULL
}
}
}
Try this once, you need to handle the else condition so that method can return you the what exactly has happened.
public function login() {
if ($this->input->post('login') && $this->validation('login')) {
$login = $this->AuthModel100->getuser($this->input->post('username'));
if ($login != NULL) {
if (password_verify($this->input->post('password'), $login->password_100)) {
$this->session->set_flashdata('msg', 'Login Successful!');
} else {
$this->session->set_flashdata('msg', 'You have entered an invalid password');
}
} else {
$this->session->set_flashdata('msg', 'You have not entered username');
}
}
$this->load->view('auth100/form_login_100');
}
I read on stack overflow about flash data only valid till next server request, therefore I made new flashdata for couple of message display..
here below is the my code
This is my controller Controller
public function login(){
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required|min_length[5]');
if($this->form_validation->run() == TRUE){
$username= $this->input->post('username');
$password= $this->input->post('password');
$this->load->model('Auth_model');
$user = $this->Auth_model->get_login();
if ($user == 0) {
//echo "<script>alert('wrong username');</script>";
$this->session->set_flashdata("msg","Username does not exists");
redirect("auth/login");
}
else{
print_r($user['username']);
if($username == $user['username'] && $password == $user['password']){
$this->session->set_flashdata("success","You are logged in");
$_SESSION['user_logged'] = TRUE;
$_SESSION['username'] = $user['username'];
redirect("user/dashboard","refresh");
}
else {
//echo "<script>alert('wrong password');</script>";
$this->session->set_flashdata("msg","Password does not match.");
redirect("auth/login");
}
}
}
$this->load->view('login_v');
}
Model
public function get_login(){
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
$this->db->select('*');
$this->db->from('users');
$this->db->where(array('username' => $username));
$query = $this->db->get();
$user = $query->row();
if ($this->db->affected_rows() != 1) {
return false;
}
else {
$data = array(
'user_id' => $user->user_id,
'username' => $user->username,
'password' => $user->password
);
//print_r($data);
//$this->session->set_userdata($data);
return $data;
}
}
view
<?php if(isset($_SESSION['success'])) {?>
<div class="alert alert-success"><?php echo $_SESSION['success']; ?></div>
<?php } ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php $this->session->flashdata('msg');?>
<form action="" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" id="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div>
<button class="btn btn-primary" name="login">Login</button>
</div>
</form>
I want to display
$this->session->set_flashdata("msg","Username does not exists");
but my if else is just doing redirect, the commented script tag works fine though.
How to make "msg" work?
Thanks in advance.
please add echo statement in the view like
<?php echo $this->session->flashdata('msg');?>
OR
<?=$this->session->flashdata('msg')?>
It should be like this :
Get your flashdata by using its key, Should be like this
<?php if(!empty($this->session->flashdata('msg'))) {?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('msg'); ?>
</div>
<?php } ?>
Or simply do like this:
<div class="alert alert-success"><?php echo $this->session->flashdata('msg'); ?></div>
For more : https://www.codeigniter.com/user_guide/libraries/sessions.html
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
I want to show an updated profile page of users on my site. The update on database is actually working but i want my userprofile page to show the updated datas once it clicked the submit button. What will I add here D:
MemberLoginController.php (controller)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberLoginController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('MemberLoginModel');
}
public function home(){
$this->load->view('pages/index');
}
public function userprofile(){
$this->load->view('member/userprofile');
}
public function useredit(){
$this->load->view('member/useredit');
}
public function memberlogin(){
$this->form_validation->set_error_delimiters('<p class=error>','</p>');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
if($this->form_validation->run()){
// Perform Actions after getting valid form inputs
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('index.php/MemberLoginController/members');
}else
$this->load->view('pages/index');
}
public function members(){
if($this->session->userdata('is_logged_in')){
$vis = "hidden";
$id = $this->session->userdata('id');
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo']=$this->MemberLoginModel->getMember($id);
$this->load->view('member/userprofile',$memberinfo);
}else{
redirect('index.php/HomeController/home');
}
}
public function edit($id){
$data = array(
"action" => base_url('/index.php/MemberLoginController/update/'.$id),
"data" => $this->db->get_where('member',array('id'=>$id))
);
$this->load->view('member/useredit', $data);
}
public function update($id){
$data = array(
'memberfname'=> $this->input->post('memberfname'),
'memberlname'=> $this->input->post('memberlname'),
'email'=>$this->input->post('email'),
);
$this->db->update('member',$data,array('id'=> $id));
redirect('index.php/MemberLoginController/getMember/'.$id);
}
public function getMember(){
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo']=$this->MemberLoginModel->getMember();
$this->load->view('member/userprofile',$memberinfo);
}
public function validate_credentials(){
$this->load->model('MemberLoginModel');
if($this->MemberLoginModel->login()){
echo ("<SCRIPT LANGUAGE = 'JavaScript'>
window.alert('Login Successfully')
window.location.href='userprofile'
</SCRIPT>");
exit();
return true;
}else{
echo ("<SCRIPT LANGUAGE = 'JavaScript'>
window.alert('Invalid username or password. Please click LOGIN again.')
window.location.href='home'
</SCRIPT>");
exit();
//$this->form_validation- >set_message('validate_credentials','Incorrect Email/Password');
return false;
}
}
}
?>
MemberLoginModel.php
<?php class MemberLoginModel extends CI_Model{
public function __construct()
{
parent::__construct();
}
function login()
{
$this->db->where('email',$this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('member'); /*i added 'member' table on db new members*/
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'id' => $rows->id,
'memberfname' => $rows->memberfname,
'memberlname' => $rows->memberlname,
'email' => $rows->email,
'logged_in' => TRUE
);
}
$this->session->set_userdata($newdata);
return true;
}else{
return false;
}
}
public function add_user()
{
$data = array(
'memberfname'=>$this->input->post('memberfname'),
'memberlname'=>$this->input->post('memberlname'),
'email'=>$this->input->post('email'),
'password'=>md5($this->input->post('password')),
);
$this->db->insert('member',$data);
}
public function getMember()
{
$query=$this->db->get('member');
return $query->result();
}
}
?>
Userprofile.php (view)
<body>
<div class="row rowpadding">
<div class="col-md-3 col-sm-3">
<div class="user-wrapper">
<div class="description">
<img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg">
</div>
<br><br>
</div>
</div>
<div class="col-md-6 col-sm-6 user-wrapper">
<div class="description">
<br>
<h2 class="name">Hi, <?php echo $this->session->userdata('memberfname'); ?>!</h2>
<hr/>
<div class="colwrapper">
<div class="cont-5">
<div class="cont-6 name"><p class="para-2"><span class="font-3">First Name: <?php echo $this->session->userdata('memberfname'); ?>
</span></p></div>
</div><br>
<div class="cont-7">
<div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name: <?php echo $this->session->userdata('memberlname'); ?></span></p></div>
</div><br>
<div class="cont-9">
<div class="cont-10"><p class="para-4"><span class="font-5">Email Address: <?php echo $this->session->userdata('email'); ?></span></p></div>
</div><br>
<div class="cont-11">
<div class="cont-12"><p class="para-5"><span class="font-6"></span></p></div>
</div><br>
</div>
<br><br>
</div>
</div>
<div class="col-md-3 col-sm-3 user-wrapper">
<div class="user-wrapper">
<br>
<div class="description">
<ul class="rightnavi"style="">
<li class="rightnavi">Add Profile Photo</li>
<li class="rightnavi">Update Your Profile</li>
</ul>
<hr/>
</div>
</div>
</div>
<!-- USER PROFILE ROW END-->
</div>
</body>
Useredit.php (View)
<form action="<?php echo $action;?>" method="POST" enctype="multipart/form-data">
<div class="container">
<div class="row rowpadding">
<div class="col-md-3 col-sm-3">
<div class="user-wrapper">
<div class="description">
<img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg">
</div>
<br><br>
</div>
</div>
<div class="col-md-6 col-sm-6 user-wrapper">
<div class="description">
<br>
<h2 class="name">Hi, <?php echo $this->session->userdata('memberfname'); ?>!</h2>
<hr />
<p>
<div class="colwrapper">
<div class="cont-5">
<div class="cont-6 name"><p class="para-2"><span class="font-3">First Name:
<input type="text" name="memberfname" value="<?php echo $this->session->userdata('memberfname'); ?>" required />
</span></p></div>
</div><br>
<div class="cont-7">
<div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name:
<input type="text" name="memberlname" value="<?php echo $this->session->userdata('memberlname'); ?>" required /></span></p></div>
</div><br>
<div class="cont-9">
<div class="cont-10"><p class="para-4"><span class="font-5">Email Address:
<input type="text" name="email" value="<?php echo $this->session->userdata('email'); ?>" required /></span></p></div>
</div><br>
<div class="cont-11">
<div>
<input type="hidden" name="hidden" value="<?php echo $this->session->userdata('id'); ?>"/>
<input type="submit" value="update">
</div>
</div>
<br><br>
</p>
</div>
</div>
</div>
</div>
</div>
</form>
You should combine form_validation object with your code.
public function update($id)
{
if ( (int)$id < 1)//$id is not an integer
{
redirect('memberlogincontroller/home', 'refresh');
}
else
{
$this->load->library('form_validation');//it's good to autoload it
$this->form_validation->set_rules('memberfname', 'First Name', 'trim|required');
$this->form_validation->set_rules('memberlname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required');
if ($this->validation_form->run() === FALSE)
{
$this->load->view('userprofile');
}
else
{
$data = array(
'memberfname' => $this->input->post('memberfname'),
'memberlname' => $this->input->post('memberlname'),
'email' => $this->input->post('email'),
);
$this->db->update('member',$data,array('id'=> $id));
redirect('memberlogincontroller/getMember/' . $id, 'refresh');
}
}
}
In model you are not passing id of specific member. It should be like this
public function getMember($id)
{
$this->db->where('id', $id);
$query = $this->db->get('member');
if ($query->num_rows() !== 1)
{
return FALSE;
}
return $query->row();//since you need just single member data
}
Also, in controller method code you have to pass $id to model:
public function getMember($id)
{
if (int($id) < 1)
{
redirect('memberlogincontroller/home', 'refresh');//no valid uri segment
}
else
{
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo'] = $this->MemberLoginModel->getMember($id);
if ( ! $memberinfo['memberinfo'])//returned FALSE from model
{
redirect('memberlogincontroller/home', 'refresh');//no $id in DB
}
else
{
$this->load->view('member/userprofile',$memberinfo);
}
}
}
Also, you should pay attention to naming convention. You shouldn't close file with closing php tag. My proposal is to read docs carefully since you could avoid lot of annoying bugs that way.
Redirect should be
redirect('MemberLoginController/members');
redirect('Controller/Method');
If method is not added, So it will call the index() method by default