PHP 개발 빨간색 및 파란색 투표 기능 튜토리얼 백엔드 페이지

vote.php 파일 만들기

프런트엔드에서 백엔드 vote.php를 요청하고, vote.php는 데이터베이스에 연결하고 수신된 매개변수를 기반으로 관련 기능을 호출합니다.

9.jpg

코드는 다음과 같습니다

<?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); } ?>
  • 코스 추천
  • 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~