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);
}
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__## $que=mysqli_query($conn,$ip_);##a_ ); $que);
if($count==0){//尚未投票
if($type==1){//紅方
+1 where id=".$id;
}else{//藍色方
$ }
mysqli_query($conn,$sql);
$sql_in = "insert into votes_ip (vid,ip) values conn,$sql_in);
if($result > 0){
$arr['success'] = 0;
$arr['msg'] = '操作失敗,重試';
echo json_encode($arr);
0;
$arr['msg'] = '已經投票過了';
echo json_encode($arr);
}
#}
#}
'}
繼續學習
# $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__## $que=mysqli_query($conn,$ip_);##a_ ); $que);
if($count==0){//尚未投票
if($type==1){//紅方
+1 where id=".$id;
}else{//藍色方
$ }
mysqli_query($conn,$sql);
$sql_in = "insert into votes_ip (vid,ip) values conn,$sql_in);
if($result > 0){
$arr['success'] = 0;
$arr['msg'] = '操作失敗,重試';
echo json_encode($arr);
0;
$arr['msg'] = '已經投票過了';
echo json_encode($arr);
}
#}
#}
'}
函數jsons($id)透過查詢目前id的投票數,計算比例並傳回json資料格式供前端呼叫。
function jsons($id){
$query = mysql_query("select * from votes where id=".$id
$red = $row['likes'];
$blue = $row['unlikes'];
$arr['success']=1;
$arr['success']=1;
] = $red;
$arr['blue'] = $blue;
$red_percent = round($red/($red+$ ),3); = $red_percent;
$arr['blue_percent'] = 1-$red_percent;
# return json_encode($arr
#1return json_encode($arr
$query = mysql_query("select * from votes where id=".$id
$red = $row['likes'];
$blue = $row['unlikes'];
$arr['success']=1;
$arr['success']=1;
] = $red;
$arr['blue'] = $blue;
$red_percent = round($red/($red+$ ),3); = $red_percent;
$arr['blue_percent'] = 1-$red_percent;
# return json_encode($arr
#1return json_encode($arr
取得使用者真實IP的函數:get_client_ip()
function get_client_ip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP") , "未知"))
$ip = getenv("HTTP_CLIENT_IP");
else
ED_FOR"), "未知"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
elsec mp(getenv("REMOTE_ADDR"), "未知"))
$ ip = getenv( "REMOTE_ADDR");
else
if secmp($_SERVER['REMOTE_ADDR'], "未知"))
$ip = $_SERVER['REMOTE_ADDR'];
$ip = 「未知」;
return ($ip);
#}
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP") , "未知"))
$ip = getenv("HTTP_CLIENT_IP");
else
ED_FOR"), "未知"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
elsec mp(getenv("REMOTE_ADDR"), "未知"))
$ ip = getenv( "REMOTE_ADDR");
else
if secmp($_SERVER['REMOTE_ADDR'], "未知"))
$ip = $_SERVER['REMOTE_ADDR'];
$ip = 「未知」;
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); } ?>##################
||
<?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);
}
?>
- 課程推薦
- 課件下載
-
初級帝國CMS企業仿站教程
3048人在看 -
初級WordPress零基礎新手建置個人部落格及企業網站
6743人在看 -
初級極致CMS零基礎建站教學視頻
2724人在看 -
初級前端專案-尚優選【HTML/CSS/JS技術綜合實戰】
3117人在看 -
中級Vue3.0從0到搭建通用後台管理系統專案實戰
5351人在看 -
初級零基礎前端課程【Vue進階學習及實戰應用】
2821人在看 -
初級WEB前端教學【HTML5+CSS3+JS】
3506人在看 -
初級apipost極速入門
2161人在看 -
中級Vue3+TypeScript實戰教學-企業級專案實戰
3208人在看 -
初級簡單聊聊PHP創業那點事
17423人在看 -
中級VUE電商專案(前台&後台 雙專案實戰)
3828人在看 -
初級Apipost實戰應用【api、介面、自動化測試、mock】
2265人在看
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~