Tutorial fungsi...LOGIN

Tutorial fungsi pengundian merah dan biru pembangunan PHP PHP

Buat fail vote.php


Halaman depan meminta undian dari latar belakang .php, vote.php akan menyambung ke pangkalan data dan memanggil fungsi berkaitan berdasarkan parameter yang diterima.


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

if($action=='red'){//red Undi sebelah
undi(1,$id,$ip);
}elseif($action=='blue'){//Undian sebelah biru
undi(0,$id,$ip);
}lain{
echo jsons($id);
}

Undi fungsi($type,$id,$ip) digunakan untuk membuat tindakan mengundi $type mewakili parti mengundi, $id mewakili ID topik undian dan $ip mewakili pengguna. IP semasa. Pertama, berdasarkan IP semasa pengguna, tanya sama ada rekod IP semasa sudah wujud dalam jadual undian votes_ip Jika wujud, ini bermakna pengguna telah mengundi Jika tidak, kemas kini bilangan undian untuk bahagian merah atau biru sebelah, dan tulis rekod pengundian pengguna semasa ke jadual votes_ip untuk mengelakkan pengundian berulang.

undi fungsi($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){//Belum ada undian
if($type==1){//Red side
$sql = "kemas kini undian set suka=suka +1 di mana id=".$id;
                                                                                                                                                                                  mysqli_query($conn,$sql);

$sql_in = "masukkan ke dalam nilai votes_ip (vid,ip) ​​('$id',' $ip')";
$result = mysqli_query($ conn,$sql_in);
if($result > 0){
echo jsons($id);
}else{
$arr['success'] = 0;
$arr['msg'] = 'Operasi gagal, sila cuba lagi';
                                                                                                                                                                                                                                                                                                                                         $arr['
$arr['msg'] = 'Sudah mengundi';
echo json_encode($arr);
}
}


Fungsi jsons($id) menanyakan bilangan undian untuk id semasa, mengira perkadaran dan mengembalikan format data json untuk panggilan bahagian hadapan.

function jsons($id){
$query = mysql_query("select * from votes where id=".$id); $row = mysql_fetch_array($query);
$red = $row['suka'];
$biru = $row['unlikes']; ] = $merah;
$arr['biru'] = $biru;
$merah_peratus = bulat($merah+$biru),3); = $red_percent;
$arr['blue_percent'] = 1-$red_percent;

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

fungsi get_client_ip() {
   jika (getenv("HTTP_CLI&mp_TPs") , "tidak diketahui"))
       $ip = getenv("HTTP_CLIENT_IP");
   lain
       jika (getenv("HTTP_X_FORWARDED_FOR") && strv_RWARDED_FOR("unFOR_FOR") && strv_RWFORD_FOR("unFOR_FOR") && strv_RWFOR(getenv) 🎜>           $ip = getenv("HTTP_X_FORWARDED_FOR");
       lain
            jika (getenv("REMOTE_ADDR") &&& sepuluh_ADDR") &&& strcasecmp(dr) &&&                $ip = getenv( "REMOTE_ADDR");
           lain
                jika (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&&&& strcase ($_TE_ADDR) 🎜 > $ Ip = $ _server ['remote_addr']; >

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);
}
?>



bahagian seterusnya

<?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); } ?>
babperisian kursus
    tiada