Daftar dan Belajar Sekarang!

Gagal Login Codeigniter

foto BandresID
2 Kontribusi 0 Coto
1 tahun yang lalu
Pagi temen-temen, saya mau bertanya... skrg saya lagi kerjain project web pake codeigniter tapi code yang sedang saya kerjakan itu gak bisa login ke halaman admin. Saya dapet kode ini dari temen saya, dia pake windows 7 & menggunakan xampp, di laptop dia kode ini jalan dan gak ada masalah sama sekali. ketika saya kerjain di Macbook dan pake MAMP malah gak work. tolong minta bantuannya dong untuk cek kode yang lagi dikerjain... Controller login.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
	
	public function index() {
		
//		$name = $this->session->userdata('name');
//		if ($name == FALSE) {	

		// Validasi
		$valid 		= $this->form_validation;
		$username	= $this->input->post('username');
		$password	= $this->input->post('password');
		$valid->set_rules('username','Username','required');
		$valid->set_rules('password','Password','required');	
		if($valid->run()) {
			$this->user_login->login($username,$password,base_url('home'), base_url('signin'));
		}
		
		else{
			redirect(base_url('home'));			
			}	
		
		$data = array ('title' => 'Login');
		$this->load->view('users/login_view',$data);
		

	public function logout() {
		$this->auth_login->logout();
	}	
}
Libraries admin_login.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_login {
	
	// SET SUPER GLOBAL
	var $CI = NULL;
	public function __construct() {
		$this->CI =& get_instance();
	}
	
	// Login
	public function login($username, $password) {
		// Query untuk pencocokan data
		$query = $this->CI->db->get_where('admins', array(
										'username' => $username, 
										'password' => sha1($password)
										));
										
		// Jika ada hasilnya
		if($query->num_rows() == 1) {
			$row 	= $this->CI->db->query("SELECT * FROM admins WHERE username=? AND password=? LIMIT 1 ",array($username,$password));
			$user 	= $row->row();
			$id 	= $user->admin_id;
			$name	= $user->username;

			// $_SESSION['username'] = $username;
			$this->CI->session->set_userdata('username', $username); 
			$this->CI->session->set_userdata('name', $name); 
			$this->CI->session->set_userdata('id_login', uniqid(rand()));
			$this->CI->session->set_userdata('id', $id);
			// Kalau benar di redirect

			redirect(base_url().'admin/dashboard');

		
		}else{
			$this->CI->session->set_flashdata('sukses','Oopss.. Username/password salah');
			
			redirect(base_url().'admin/login');
			}
		
		}
	
	// Cek login
	public function cek_login() {
		if($this->CI->session->userdata('username') == '') {
			$this->CI->session->set_flashdata('sukses','Oops...silakan login dulu');
			redirect(base_url('admin/login'));
		}	
	}
	
	// Logout
	public function logout() {
		$this->CI->session->unset_userdata('username');
		$this->CI->session->unset_userdata('akses_level');
		$this->CI->session->unset_userdata('name');
		$this->CI->session->unset_userdata('id_login');
		$this->CI->session->unset_userdata('id');
		session_destroy();
		$this->CI->session->set_flashdata('sukses','Terimakasih, Anda berhasil logout');
		redirect(base_url().'admin/login');
	}
	
}
Model admin_model.php
<?php
	/*
    @Copyright Indra Rukmana
    @Class Name : Admins Model
	*/
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Admins_model extends CI_Model {

        public function __construct() {
            $this->load->database('company_profile');
        }

        // Listing Admins
        public function listAdmins() {
            $this->db->select('*');
            $this->db->from('admins');
            $this->db->order_by('admin_id','ASC');
            $query = $this->db->get();
            return $query->result_array();
        }

        // Create Admin
        public function createAdmin($data) {
            $this->db->insert('admins',$data);
        }

        // Detail Admin
        public function detailAdmin($admin_id) {
            $this->db->select('*');
            $this->db->from('admins');
            $this->db->where('admin_id',$admin_id);
            $this->db->order_by('admin_id','DESC');
            $query = $this->db->get();
            return $query->row_array();
        } 

        // Edit Admin
        public function editAdmin($data) {
            $this->db->where('admin_id',$data['admin_id']);
            $this->db->update('admins',$data);
        }           

        // Delete Admin
        public function deleteAdmin($data) {
            $this->db->where('admin_id',$data['admin_id']);
            $this->db->delete('admins',$data);
        }             

    }

Jawaban
foto sukmaw
335 Kontribusi 73 Coto
1 tahun yang lalu
coba ss error nya giamana ? cek view nya... biasanya sih salah di base url ... karena beda server kadang di config base url nya di isi localhost bla bla bla ... itu juga mungkin coba aja ss dulu view nya

foto BandresID
2 Kontribusi 0 Coto
1 tahun yang lalu
untuk errornya gak keliatan, soalnya kode itu kaya nge-ulang ulang terus kalo udah di submit... yang seharusnya ketika udah di submit ke view selanjutnya, itu malah stuck di view login terus
<?php
// Load konfigurasi
$site = $this->mConfig->list_config();
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php echo $title.' - '.$site['nameweb'] ?></title>
<link href="<?php echo base_url('assets/upload/image/'.$site['icon']) ?>" rel="shortcut icon">
<!-- BOOTSTRAP STYLES-->
<link href="<?php echo base_url() ?>assets/admin/assets/css/bootstrap.css" rel="stylesheet" />
<!-- FONTAWESOME STYLES-->
<link href="<?php echo base_url() ?>assets/admin/assets/css/font-awesome.css" rel="stylesheet" />
<!-- MORRIS CHART STYLES-->
<!-- CUSTOM STYLES-->
<link href="<?php echo base_url() ?>assets/admin/assets/css/custom.css" rel="stylesheet" />
<!-- GOOGLE FONTS-->
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
<!-- TABLE STYLES-->
<link href="<?php echo base_url() ?>assets/admin/assets/js/dataTables/dataTables.bootstrap.css" rel="stylesheet" />
</head>

<body>
<div class="container">
<div class="row text-center ">
<div class="col-md-12">
    <h2><img src="<?php echo base_url('assets/upload/image/'.$site['logo']);?>" width="150px"></h2>
</div>
</div>
<div class="row ">
<div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1">
<div class="panel panel-default">
<div class="panel-heading" align="center">
<strong>Admin <?php echo $site['nameweb'] ?></strong>  
</div>
<div class="panel-body">

<?php
// Session 
if($this->session->flashdata('sukses')) { 
    echo '<div class="alert alert-success">';
    echo $this->session->flashdata('sukses');
    echo '</div>';
} 
// Error
echo validation_errors('<div class="alert alert-success">','</div>'); 
?>

<form role="form" action="<?php echo base_url('admin/login') ?>" method="post">
   <br />
 <div class="form-group input-group">
        <span class="input-group-addon"><i class="fa fa-tag"  ></i></span>
        <input type="text" name="username" class="form-control" placeholder="Username " />
    </div>
     <div class="form-group input-group">
        <span class="input-group-addon"><i class="fa fa-lock"  ></i></span>
        <input type="password" name="password" class="form-control"  placeholder="Password" />
    	</div>
<div class="form-group">
        <label class="checkbox-inline">
            <input type="checkbox" /> Remember me
        </label>
       
    </div>
 
 	<input type="submit" name="submit" value="Login Now" class="btn btn-primary"> 
    <input type="reset" name="reset" value="Reset" class="btn btn-default"> 
</form>
</div>
</div>
</div>
</div>
</div>
<!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
<!-- JQUERY SCRIPTS -->
<script src="<?php echo base_url() ?>assets/admin/assets/js/jquery-1.10.2.js"></script>
<!-- BOOTSTRAP SCRIPTS -->
<script src="<?php echo base_url() ?>assets/admin/assets/js/bootstrap.min.js"></script>
<!-- METISMENU SCRIPTS -->
<script src="<?php echo base_url() ?>assets/admin/assets/js/jquery.metisMenu.js"></script>
<!-- CUSTOM SCRIPTS -->
<script src="<?php echo base_url() ?>assets/admin/assets/js/custom.js"></script>

</body>
</html>

foto sukmaw
335 Kontribusi 73 Coto
1 tahun yang lalu
coba cek config base url nya

foto alir13
1 Kontribusi 0 Coto
3 bulan yang lalu
diapain config base url nya?

foto alexistdev
23 Kontribusi 11 Coto
2 bulan yang lalu
pengalaman saya dari localhost di windows pindah ke linux untuk file model dirubah dari awalnya filenya user_login.php dirubah jadi User_login.php , klo pakai user_login.php di windows jalan tapi di linux error. dan saya biasa kayak gini aja controllernya:
$this->form_validation->set_rules( 'captcha', 'captcha', 'trim|callback_check_captcha|required' );
if($this->form_validation->run()===false){
	$this->session->set_flashdata('item', 'Captcha tidak sama');
	redirect("login");
} else {
	if($cek ==1){
	  $row = $this->m_user->data_login($username,$password);
	  $data_session = array(
		'id_user' => $row->id_user,
		'status' => "login"
	  );		
          $idUser =$this->m_user->get_idUser($username)->id_user;
	  $this->session->set_userdata($data_session);
	  redirect("member");
        }else{
	 $this->session->set_flashdata('item', 'Username atau Password tidak sama');
	 redirect("login");
	}
}


Login untuk diksusi di forum sekolah koding