Controoler:
users.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
public function index(){
$this->login();
}
public function login(){
if($this->session->userdata('logged_in')){
redirect('/blogs/index');
}
$getUser = $this->input->post('username');
$getPass = $this->input->post('password');
if($this->input->post('submit')){
if(!empty($getUser) && !empty($getPass)){
if($getUser=='demo' && $getPass=='demo'){
$dataSess = array(
'username' => $getUser,
'password' => $getPass,
'logged_in' => TRUE
);
$this->session->set_userdata($dataSess);
redirect('/blogs/index');
}
}
}
$this->load->view('login');
}
public function logout(){
$this->session->sess_destroy();
$this->login();
}
}
Blogs.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blogs extends CI_Controller {
function __construct(){
parent::__construct();
if(!$this->session->userdata('logged_in')){
redirect('/users/login');
}
}
public function index() {
$data['blogs_result'] = $this->db->get('userlogin')->result();
$this->load->view('view_blogs',$data);
}
public function blogsave(){
$getUser = $this->input->post('username');
$getPass = $this->input->post('password');
$saveData = array(
'username'=>$getUser,
'userpass'=>$getPass
);
$this->db->insert('userlogin',$saveData);
$this->index();
}
public function blogsdel(){
$delId = $this->uri->segment(3);
$this->db->delete('userlogin', array('id' => $delId));
$this->index();
}
public function blogsedit(){
$getId = $this->uri->segment(3);
$data['result'] = $this->db->get_where('userlogin', array('id' =>$getId))->result();
$this->load->view('edit_blogs',$data);
}
public function blogsupdate(){
$getId = $this->input->post('postid');
$getUser = $this->input->post('username');
$getPass = $this->input->post('password');
$data = array(
'username' => $getUser,
'userpass' => $getPass
);
$this->db->where('id', $getId);
$this->db->update('userlogin', $data);
$this->index();
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
views
login.php
<!DOCTYPE html>
<html>
<head>
<title>CI</title>
<script type="text/javascript" src="<?php echo base_url('assets/js/jquery-1.12.2.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/bootstrap/js/bootstrap.js'); ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.css'); ?>">
</head>
<body>
<h4>Login Page</h4>
<form method="post" action="<?php echo base_url('users/login') ?>">
<div class="form-group">
<label for="exampleInputUsername">Username</label>
<input type="text" name="username" class="form-control" id="exampleInputPassword1" placeholder="Username">
</div>
<div class="form-group">
<label for="exampleInputPassword">Password</label>
<input type="password" name="password" name="username" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<input type="submit" value="Create" name="submit" class="btn btn-success"/>
</form>
</body>
</html>
view_blogs.php
<!DOCTYPE html>
<html>
<head>
<title>CI</title>
<script type="text/javascript" src="<?php echo base_url('assets/js/jquery-1.12.2.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/bootstrap/js/bootstrap.js'); ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.css'); ?>">
</head>
<body>
<?php
// print_r($this->session->userdata('logged_in'));
?>
<a href="<?php echo base_url('users/logout'); ?>">Logout</a>
<form method="post" action="<?php echo base_url('blogs/blogsave') ?>">
<div class="form-group">
<label for="exampleInputUsername">Username</label>
<input type="text" name="username" class="form-control" id="exampleInputPassword1" placeholder="Username">
</div>
<div class="form-group">
<label for="exampleInputPassword">Password</label>
<input type="password" name="password" name="username" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<input type="submit" value="Create" name="submit" class="btn btn-success"/>
</form>
<br/><br/>
<table class="table table-bordered">
<tr>
<th>#</th>
<th>Username</th>
<th>Password</th>
<th>Action</th>
</tr>
<?php foreach ($blogs_result as $key => $value) {?>
<tr>
<td><?= $value->id; ?></td>
<td><?= $value->username; ?></td>
<td><?= $value->userpass; ?></td>
<td>
<a href="<?php echo base_url('blogs/blogsdel')?>/<?php echo $value->id ?>">Delete</a>
<a href="<?php echo base_url('blogs/blogsedit')?>/<?php echo $value->id ?>">Edit</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
output 1
output 2
0 comments:
Post a Comment