PHP开发 红蓝两方投票功能教程之PHP

创建vote.php文件


前端请求了后台的vote.php,vote.php将根据接收的参数,连接数据库,调用相关函数。


<?php
header("Content-type:text/html;charset=utf-8");
$action = $_GET['action'];
$id = 1;
$ip = get_client_ip();//获取ip

if($action=='red'){//红方投票
   vote(1,$id,$ip);
}elseif($action=='blue'){//蓝方投票
   vote(0,$id,$ip);
}else{
   echo jsons($id);
}

函数vote($type,$id,$ip)用来做出投票动作,$type表示投票方,$id表示投票主题的id,$ip表示用户当前ip。首先根据用户当前IP,查询投票记录表votes_ip中是否已经存在当前ip记录,如果存在,则说明用户已投票,否则更新红方或蓝方的投票数,并将当前用户投票记录写入到votes_ip表中以防重复投票。

function vote($type,$id,$ip){

   $conn=mysqli_connect("localhost","root","root","vote");
   $ip_sql="select ip from votes_ip where vid='$id' and ip='$ip'";
   $que=mysqli_query($conn,$ip_sql);
   $count=mysqli_num_rows($que);
   if($count==0){//还没有投票
       if($type==1){//红方
           $sql = "update votes set likes=likes+1 where id=".$id;
       }else{//蓝方
           $sql = "update votes set unlikes=unlikes+1 where id=".$id;
       }
       mysqli_query($conn,$sql);

       $sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
       $result = mysqli_query($conn,$sql_in);
       if($result > 0){
           echo jsons($id);
       }else{
           $arr['success'] = 0;
           $arr['msg'] = '操作失败,请重试';
           echo json_encode($arr);
       }
   }else{
       $arr['success'] = 0;
       $arr['msg'] = '已经投票过了';
       echo json_encode($arr);
   }
}

函数jsons($id)通过查询当前id的投票数,计算比例并返回json数据格式供前端调用。

function jsons($id){ 
    $query = mysql_query("select * from votes where id=".$id); 
    $row = mysql_fetch_array($query); 
    $red = $row['likes']; 
    $blue = $row['unlikes']; 
    $arr['success']=1; 
    $arr['red'] = $red; 
    $arr['blue'] = $blue; 
    $red_percent = round($red/($red+$blue),3); 
    $arr['red_percent'] = $red_percent; 
    $arr['blue_percent'] = 1-$red_percent; 
     
    return json_encode($arr); 

获取用户真实IP的函数:get_client_ip()

function get_client_ip() {
   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
       $ip = getenv( "HTTP_CLIENT_IP");
   其他
       if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "未知"))
           $ip = getenv("HTTP_X_FORWARDED_FOR") ;
       否则
           if (getenv(" REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "未知"))
               $ip = getenv("REMOTE_ADDR");
           其他
               if (isset ($_SERVER[' REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR '] && strcasecmp($_SERVER['REMOTE_ADDR'], "未知"))
                  $ip = $_SERVER['REMOTE_ADDR'];
               其他
                   $ip = “未知”;
   返回($ip);
}

vote.php完整代码

<?php
header("Content-type:text/html;charset=utf-8");
$action = $_GET['action'];
$id = 1;
$ip = get_client_ip();//获取ip
if($action=='red'){//红方投票
    vote(1,$id,$ip);
}elseif($action=='blue'){//蓝方投票
    vote(0,$id,$ip);
}else{
    echo jsons($id);
}
function vote($type,$id,$ip){
    $conn=mysqli_connect("localhost","root","root","vote");
    $ip_sql="select ip from votes_ip where vid='$id' and ip='$ip'";
    $que=mysqli_query($conn,$ip_sql);
    $count=mysqli_num_rows($que);
    if($count==0){//还没有投票
        if($type==1){//红方
            $sql = "update votes set likes=likes+1 where id=".$id;
        }else{//蓝方
            $sql = "update votes set unlikes=unlikes+1 where id=".$id;
        }
        mysqli_query($conn,$sql);
        $sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')";
        $result = mysqli_query($conn,$sql_in);
        if($result > 0){
            echo jsons($id);
        }else{
            $arr['success'] = 0;
            $arr['msg'] = '操作失败,请重试';
            echo json_encode($arr);
        }
    }else{
        $arr['success'] = 0;
        $arr['msg'] = '已经投票过了';
        echo json_encode($arr);
    }
}
function jsons($id){
    $conn=mysqli_connect("localhost","root","root","vote");
    $que_sql="select * from votes where id=".$id;
    $query = mysqli_query($conn,$que_sql);
    $row = mysqli_fetch_array($query);
    $red = $row['likes'];
    $blue = $row['unlikes'];
    $arr['success']=1;
    $arr['red'] = $red;
    $arr['blue'] = $blue;
    $red_percent = round($red/($red+$blue),3);
    $arr['red_percent'] = $red_percent;
    $arr['blue_percent'] = 1-$red_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);
}
?>



继续学习
||
<?php header("Content-type:text/html;charset=utf-8"); $action = $_GET['action']; $id = 1; $ip = get_client_ip();//获取ip if($action=='red'){//红方投票 vote(1,$id,$ip); }elseif($action=='blue'){//蓝方投票 vote(0,$id,$ip); }else{ echo jsons($id); } function vote($type,$id,$ip){ $conn=mysqli_connect("localhost","root","root","vote"); $ip_sql="select ip from votes_ip where vid='$id' and ip='$ip'"; $que=mysqli_query($conn,$ip_sql); $count=mysqli_num_rows($que); if($count==0){//还没有投票 if($type==1){//红方 $sql = "update votes set likes=likes+1 where id=".$id; }else{//蓝方 $sql = "update votes set unlikes=unlikes+1 where id=".$id; } mysqli_query($conn,$sql); $sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')"; $result = mysqli_query($conn,$sql_in); if($result > 0){ echo jsons($id); }else{ $arr['success'] = 0; $arr['msg'] = '操作失败,请重试'; echo json_encode($arr); } }else{ $arr['success'] = 0; $arr['msg'] = '已经投票过了'; echo json_encode($arr); } } function jsons($id){ $conn=mysqli_connect("localhost","root","root","vote"); $que_sql="select * from votes where id=".$id; $query = mysqli_query($conn,$que_sql); $row = mysqli_fetch_array($query); $red = $row['likes']; $blue = $row['unlikes']; $arr['success']=1; $arr['red'] = $red; $arr['blue'] = $blue; $red_percent = round($red/($red+$blue),3); $arr['red_percent'] = $red_percent; $arr['blue_percent'] = 1-$red_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); } ?>
提交重置代码