File: /www/wwwroot/ceshi.lilmn.com/ajax.php
<?php
include_once("connect.php");
$action = $_GET['action'];
$id = 1;
$ip = get_client_ip();
if ($action == 'cvcv') {
cvcvs(1, $id, $ip);
} else {
echo jsons($id);
}
function cvcvs($type, $id, $ip) {
$ip_sql = mysql_query("select ip from votes_ip where vid='$id' and ip='$ip'");
$count = mysql_num_rows($ip_sql);
if ($count == 0) {//还没有顶过
if ($type == 1) {//顶
$sql = "update votes set cvcvs=cvcvs+1 where id=" . $id;
} else {//踩
$sql = "update votes set uncvcvs=uncvcvs+1 where id=" . $id;
}
mysql_query($sql);
$sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
mysql_query($sql_in);
echo jsons($id);
} else {
$msg = $type == 1 ? '您已经顶过了' : '您已经踩过了';
$arr['success'] = 0;
$arr['msg'] = $msg;
echo json_encode($arr);
}
}
function jsons($id) {
$query = mysql_query("select * from votes where id=" . $id);
$row = mysql_fetch_array($query);
$cvcv = $row['cvcvs'];
$uncvcv = $row['uncvcvs'];
$arr['success'] = 1;
$arr['cvcv'] = $cvcv;
$arr['uncvcv'] = $uncvcv;
$cvcv_percent = round($cvcv / ($cvcv + $uncvcv), 3) * 100;
$arr['cvcv_percent'] = $cvcv_percent . '%';
$arr['uncvcv_percent'] = (100 - $cvcv_percent) . '%';
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);
}
?>