forked from ShawnZeng1996/Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
like.php
executable file
·62 lines (61 loc) · 1.68 KB
/
like.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
header("Content-Type: application/json; charset=utf-8");
require('../../../wp-load.php');
$action = $_GET['action'];
$id = 1;
$ip = get_client_ip();
if($action=='add'){
likes($id,$ip);
}else if($action=='get'){
echo jsons($id);
} else {
exit();
}
global $wpdb;
function likes($id,$ip){
global $wpdb;
$likes = $wpdb->get_var($wpdb->prepare("SELECT likes FROM wp_votes_num WHERE ID = %d;",$id));
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM wp_votes_ip WHERE IP = '%s';",$ip));
if(0==$count){
$likes = $likes+1;
$wpdb->update( 'wp_votes_num',
array( 'likes' => $likes ),
array( 'id' => '1' ),
array( '%d' ),
array( '%d' )
);
$data['ip'] = $ip;
$wpdb->insert('wp_votes_ip', $data);
echo jsons($id);
}else{
$msg = 'repeat';
$arr['success'] = 0;
$arr['msg'] = $msg;
echo json_encode($arr);
}
}
function jsons($id){
global $wpdb;
$likes = $wpdb->get_var($wpdb->prepare("SELECT `likes` FROM wp_votes_num WHERE `id` = %d ", $id));
$arr['success'] = 1;
$arr['like'] = $likes;
return json_encode($arr);
}
//获取用户真实IP
function get_client_ip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else
if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return ($ip);
}
?>