Skip to content

Commit a7d39fe

Browse files
authored
Merge pull request #12 from hanfengmi/master
logIn+logOut
2 parents b233769 + 31fbd26 commit a7d39fe

17 files changed

+148
-45
lines changed

application/controllers/Category.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ public function index()
88
}
99

1010
public function AddCategory() {
11-
$post = $this->input->post(NULL, TRUE);
12-
$data = array(
13-
'name' => $post['cat_name'],
14-
);
15-
$this->load->model('Category_model');
16-
$category_list = $this->Category_model->add_category($data);
17-
echo json_encode($category_list);
18-
// var_dump($post);
19-
// echo $post->;
11+
session_start();
12+
$post = $this->input->post(NULL, TRUE);
13+
$data = array(
14+
'name' => $post['cat_name'],
15+
);
16+
$nonces = $_SESSION['nonces'];
17+
$this->load->model('Category_model');
18+
$category_list = $this->Category_model->add_category($data, $nonces);
19+
echo json_encode($category_list);
20+
// var_dump($post);
21+
// echo $post->;
2022
}
2123

2224
public function DeleteCategory() {

application/controllers/Home.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
3-
3+
require APPPATH . '/libraries/ImplementJwt.php';
44
class Home extends CI_Controller {
55

6+
public function __construct(){
7+
parent::__construct();
8+
$this->load->helper('url');
9+
$this->objOfJwt = new ImplementJwt();
10+
}
11+
612
public function index()
713
{
14+
15+
if(isset($_COOKIE['auth'])){
16+
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
17+
$user = $decodeToken;
18+
} else {
19+
$user = NULL;
20+
}
21+
822
$this->load->library('layout');
923
$this->load->model('admin_model');// 都是获取所有数据接口,可复用
1024
$product = $this->admin_model->getProductData();
1125
$catgory = $this->admin_model->getCatgoryData();
1226
$data['products'] = $product;
1327
$data['catgories'] = $catgory;
28+
$data['user'] = $user;
29+
// var_dump($user);
30+
1431
$this->layout->view('home',['data' => $data]);
1532
}
1633
}

application/controllers/Item.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
33

4+
require APPPATH . '/libraries/ImplementJwt.php';
45
class Item extends CI_Controller {
56

7+
public function __construct(){
8+
parent::__construct();
9+
$this->objOfJwt = new ImplementJwt();
10+
}
11+
612
public function index()
713
{
14+
if(isset($_COOKIE['auth'])){
15+
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
16+
$user = $decodeToken;
17+
} else {
18+
$user = NULL;
19+
}
20+
21+
$data['user'] = $user;
22+
823
$this->load->library('layout');
924
// $this->load->view('item');
10-
$this->layout->view('item');
25+
$this->layout->view('item',['data' => $data]);
1126
}
1227
}

application/controllers/Login.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Login extends CI_Controller {
77
public function __construct(){
88
parent::__construct();
99
$this->objOfJwt = new ImplementJwt();
10+
$this->load->helper('cookie');
1011
}
1112

1213
public function index(){
@@ -19,6 +20,7 @@ public function index(){
1920
public function Login() {
2021
$this->load->helper('url');
2122
$this->load->library('layout');
23+
session_start();
2224
// $this->output->set_header('Access-Control-Allow-Credentials:true');
2325
// https://codeigniter.com/user_guide/libraries/input.html
2426
// To return all POST items and pass them through the XSS filter set the first parameter NULL while setting the second parameter to boolean TRUE.
@@ -44,29 +46,33 @@ public function Login() {
4446
'prefix' => NULL,
4547
'httponly' => TRUE
4648
);
49+
// set cookie + set session
4750
$this->input->set_cookie($cookie);
51+
$_SESSION['nonces'] = md5(rand(1,10000));
4852
// $decodeToken = $this->objOfJwt->DecodeToken($jwtToken);
4953
// echo $decodeToken;
5054
// var_dump($_COOKIE);
5155

5256
$response = array('status'=>'2','msg'=>'success','data'=>$user[0]);
5357
echo json_encode($response);
54-
// if($user[0]->role == 0){
55-
// // 跳转到 admin
56-
// // redirect('http://47.98.195.42/php/admin', 'location');
57-
58-
// }else{
59-
// // 跳转到首页
60-
// // redirect('http://47.104.15.106/home');
61-
// // redirect('http://47.98.195.42/php/home', 'location');
62-
// }
6358
}else{
6459
$response = array('status'=>'0','msg'=>'failed','data'=>$user);
6560
// echo json_encode($user);
6661
// $this->layout->view('item');
6762
echo json_encode($response);
63+
}
64+
}
65+
66+
public function LogOut() {
67+
session_start();
68+
$response = array('status'=>'0','msg'=>'failed');
69+
try {
70+
$response = array('status'=>'2','msg'=>'success');
71+
unset($_SESSION['nonces']);
72+
delete_cookie('auth');
73+
echo json_encode($response);
74+
}catch(PDOEXCEPTION $e){
75+
echo $e->getMessage();
6876
}
69-
70-
71-
}
77+
}
7278
}

application/controllers/ProductAPI.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class ProductAPI extends CI_Controller {
66

77
public function __construct()
88
{
9-
parent::__construct();
9+
parent::__construct();
10+
1011
}
1112

1213
public function index()
@@ -42,18 +43,20 @@ public function ProductDetail() {
4243
}
4344

4445
public function AddProduct() {
46+
session_start();
4547
// https://codeigniter.com/user_guide/libraries/input.html
4648
// To return all POST items and pass them through the XSS filter set the first parameter NULL while setting the second parameter to boolean TRUE.
4749
$post = $this->input->post(NULL, TRUE);
50+
4851
$data = array(
4952
'name' => $post['name'],
5053
'price' => $post['price'],
5154
'catid' => $post['catid'],
5255
'description' => $post['description']
53-
// ["file"]=> string(8) "demo.jpg"
5456
);
57+
$nonces = $_SESSION['nonces'];
5558
$this->load->model('Product_model');
56-
$product_list = $this->Product_model->add_product($data);
59+
$product_list = $this->Product_model->add_product($data,$nonces);
5760
echo json_encode($product_list);
5861
// var_dump($post);
5962
// echo $post->;

application/models/Category_model.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ public function __construct()
77
}
88

99

10-
public function add_category($data){
10+
public function add_category($data, $nonces){
1111
$response = array('status'=>'0','msg'=>'failed','data'=>'');
1212
try{
1313
// 防止 sql 注入。
1414
// $dataEscape = $this->db->escape($data);
15-
$query = $this->db->insert( 'categories' , $data );
16-
// $response['data'] = $query;
17-
$response['status'] = '2';
18-
$response['msg'] = 'success';
19-
return $response;
15+
if(isset($nonces) && $nonces == $_SESSION['nonces']){
16+
$query = $this->db->insert( 'categories' , $data );
17+
// $response['data'] = $query;
18+
$response['status'] = '2';
19+
$response['msg'] = 'success';
20+
return $response;
21+
}else {
22+
echo 'error';
23+
}
2024
}catch(PDOEXCEPTION $e){
2125
echo $e->getMessage();
2226
}

application/models/Product_model.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ public function get_product_detail($productid){
1818
}
1919
}
2020

21-
public function add_product($data){
21+
public function add_product($data, $nonces){
22+
// session_start();
2223
$response = array('status'=>'0','msg'=>'failed','id'=>'');
2324
try{
2425
// 防止 sql 注入。 需要每个字段单独拿出来判断。
2526
// $dataEscape = $this->db->escape($data);
26-
$query = $this->db->insert( 'products' , $data );
27-
$response['id'] = $this->db->insert_id();
28-
$response['status'] = '2';
29-
$response['msg'] = 'success';
30-
return $response;
27+
// Apply and validate secret nonces for every form
28+
if(isset($nonces) && $nonces == $_SESSION['nonces']){
29+
$query = $this->db->insert( 'products' , $data );
30+
$response['id'] = $this->db->insert_id();
31+
$response['status'] = '2';
32+
$response['msg'] = 'success';
33+
return $response;
34+
}else {
35+
echo 'error';
36+
}
3137
}catch(PDOEXCEPTION $e){
3238
echo $e->getMessage();
3339
}

application/views/admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
data: values,
192192
url: "./ProductAPI/UpdateProduct",
193193
dataType: 'json',
194+
194195
beforeSend: function() {
195196
console.log('正在请求')
196197
},

application/views/header.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22
defined('BASEPATH') OR exit('No direct script access allowed');
33
?>
44

5-
<div class="page-header">
6-
<h1>Phase 1</h1>
7-
<div class="row">
8-
<div class="dropdown col-sm-3 col-sm-offset-9 col-md-3 col-md-offset-9">
5+
<div class="row page-header" >
6+
<h1 class="col-md-4">Phase 1</h1>
7+
<div class="col-md-2 col-sm-3" style="border:1px solid #ccc; padding:20px 0; border-radius:10px; text-align:center">
8+
<?php
9+
if($data){
10+
echo
11+
'
12+
<p>' . $data['email'] . '</p>
13+
<button type="button" class="logOut">signOut</button>
14+
';
15+
} else {
16+
echo
17+
'
18+
<p>guest</p>
19+
<button type="button" class="logIn">logIn</button>
20+
';
21+
}
22+
?>
23+
</div>
24+
<div class="col-md-6 col-sm-9">
25+
<div class="dropdown col-sm-offset-7 col-md-3 col-md-offset-4">
926
<button class="btn-default">Shopping Cart</button>
1027
<div class="dropdown-content">
1128
<!-- 渲染 -->
@@ -22,6 +39,8 @@
2239
init:function(){
2340
this.getShoppingCarData();
2441
this.changeProductNum();
42+
this.logOut();
43+
this.logIn();
2544
},
2645
getShoppingCarData:function(){
2746
var shopingList = JSON.parse(localStorage.getItem("shopCar")) || [];
@@ -68,9 +87,39 @@ function findProd(prod) {
6887
$('.total-product').html(total);
6988
})
7089

71-
}
90+
},
7291

92+
logOut(){
93+
$('.page-header').on('click', '.logOut', function(){
94+
$.ajax({
95+
type: "post",
96+
data: {},
97+
url: "./Login/LogOut",
98+
dataType: 'json',
99+
success: function(data) {
100+
// that.setShoppingCar(data);
101+
if(data.status == 2){
102+
alert('logOut');
103+
window.location.reload();
104+
}
105+
},
106+
error: function() {
107+
alert("ajax error");
108+
}
109+
});
110+
})
111+
},
73112

113+
logIn(){
114+
$('.page-header').on('click', '.logIn', function(){
115+
var url = window.location.href;
116+
if(url.indexOf('home')>0){
117+
window.location.href = url.split('home')[0]+'login'
118+
}else {
119+
window.location.href = url.split('item')[0]+'login'
120+
}
121+
})
122+
}
74123

75124
}
76125
header.init();

application/views/home.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="container-fluid">
2-
<?php $this->load->view('header'); ?>
2+
<?php $this->load->view('header',['data' => $data['user']]); ?>
33
<div class="row container-fluid product-container">
44
<div class="col-sm-3 col-md-2 sidebar">
55
<ul class="nav nav-sidebar cat-nav-sidebar">

application/views/item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="container-fluid">
2-
<?php $this->load->view('header'); ?>
2+
<?php $this->load->view('header',['data' => $data['user']]); ?>
33

44
<div class="row container-fluid">
55
<!-- <div class="col-sm-3 col-md-2 sidebar">

public/imgs/20.jpeg

400 KB
Loading

public/imgs/6.jpg

4.72 KB
Loading

public/imgs/boseqc30-thumb.jpg

3.74 KB
Loading

public/imgs/ps4-thumb.jpg

5.02 KB
Loading

public/imgs/wii-thumb.jpg

5.2 KB
Loading

public/imgs/xone-thumb.jpg

4.72 KB
Loading

0 commit comments

Comments
 (0)