顶、踩投票功能

PHP+MySql+jQuery实现的“顶”和“踩”投票功能

1 . [代码][PHP]代码

CREATE TABLE IF NOT EXISTS `votes` ( 
  `id` int(10) NOT NULL AUTO_INCREMENT, 
  `likes` int(10) NOT NULL DEFAULT '0', 
  `unlikes` int(10) NOT NULL DEFAULT '0', 
  PRIMARY KEY (`id`) 
) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 


INSERT INTO `votes` (`id`, `likes`, `unlikes`) VALUES 
(1, 30, 10); 

CREATE TABLE IF NOT EXISTS `votes_ip` ( 
  `id` int(10) NOT NULL, 
  `vid` int(10) NOT NULL, 
  `ip` varchar(40) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-------------div--------------------------------------

<div class="digg">  
    <div id="dig_up" class="digup"> 
        <span id="num_up"></span> 
        <p>很好,很强大!</p> 
        <div id="bar_up" class="bar"><span></span><i></i></div> 
    </div> 
       <div id="dig_down" class="digdown"> 
        <span id="num_down"></span> 
        <p>太差劲了!</p> 
        <div id="bar_down" class="bar"><span></span><i></i></div> 
    </div> 
    <div id="msg"></div> 
</div> 



-------------css--------------------------
.digg{width:420px; height:120px; margin:80px auto 20px auto; position:relative} 
#dig_up,#dig_down{width:200px; height:48px; margin:10px; position:relative;  
border:1px solid #d3d3d3; padding-left:42px; cursor:pointer} 
.digup{background:url(diggs.png) no-repeat 4px 2px;} 
.digup_on{background:url(diggs.png) no-repeat 4px -49px;} 
.digdown{background:url(diggs.png) no-repeat 4px -102px;} 
.digdown_on{background:url(diggs.png) no-repeat 4px -154px;} 
#num_up,#num_down{position:absolute; right:6px; top:18px; font-size:20px;} 
#dig_up p{height:24px; line-height:24px; color:#360} 
#dig_down p{height:24px; line-height:24px; color:#f30} 
.bar{width:100px; height:12px; line-height:12px; border:1px solid #f0f0f0;  
position:relative; text-align:center} 
.bar span{display:block; height:12px; } 
.bar i{position:absolute; top:0; left:104px;} 
#bar_up span{background:#360} 
#bar_down span{background:#f60} 
#msg{position:absolute; right:20px; top:40px; font-size:18px; color:#f00} 



-------jquery-------------------------

$(function(){ 
    //鼠标滑向和离开投票按钮时,变换背景样式 
    $("#dig_up").hover(function(){ 
        $(this).addClass("digup_on"); 
    },function(){ 
        $(this).removeClass("digup_on"); 
    }); 
    $("#dig_down").hover(function(){ 
        $(this).addClass("digdown_on"); 
    },function(){ 
        $(this).removeClass("digdown_on"); 
    }); 

    //初始化数据 
    getdata("do.php",1); 

    //单击“顶”时 
    $("#dig_up").click(function(){ 
        getdata("do.php?action=like",1); 
    }); 
    //单击“踩”时 
    $("#dig_down").click(function(){ 
        getdata("do.php?action=unlike",1); 
    }); 
}); 



---------------------------------------
function getdata(url,sid){ 
    $.getJSON(url,{id:sid},function(data){ 
        if(data.success==1){//投票成功 
            $("#num_up").html(data.like); 
            //通过控制宽度来显示百分比进度条效果 
            $("#bar_up span").css("width",data.like_percent); 
            $("#bar_up i").html(data.like_percent); 
            $("#num_down").html(data.unlike); 
            $("#bar_down span").css("width",data.unlike_percent); 
            $("#bar_down i").html(data.unlike_percent); 
        }else{//投票失败 
            $("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'}) 
            .animate({top:'-50px',opacity:0}, "slow"); 
        } 
    }); 
} 


---------php--------------------------

include_once("connect.php");//连接数据库 

$action = $_GET['action']; 
$id = 1; 
$ip = get_client_ip();//获取ip 

if($action=='like'){//顶 
    likes(1,$id,$ip); 
}elseif($action=='unlike'){//踩 
    likes(0,$id,$ip); 
}else{ 
    echo jsons($id); 
} 

------------------------------------
function likes($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 likes=likes+1 where id=".$id; 
        }else{//$sql = "update votes set unlikes=unlikes+1 where id=".$id; 
        } 
        mysql_query($sql); 

        $sql_in = "insert into votes_ip (vid,ip) values ('$id','$ip')"; 
        mysql_query($sql_in); 

        if(mysql_insert_id()>0){ 
            echo jsons($id); 
        }else{ 
            $arr['success'] = 0; 
            $arr['msg'] = '操作失败,请重试'; 
            echo json_encode($arr); 
        } 
    }else{ 
        $msg = $type==1?'您已经顶过了':'您已经踩过了'; 
        $arr['success'] = 0; 
        $arr['msg'] = $msg; 
        echo json_encode($arr); 
    } 
} 


-----------php-------------------------


function jsons($id){ 
    $query = mysql_query("select * from votes where id=".$id); 
    $row = mysql_fetch_array($query); 
    $like = $row['likes']; 
    $unlike = $row['unlikes']; 
    $arr['success']=1; 
    $arr['like'] = $like; 
    $arr['unlike'] = $unlike; 
    $like_percent = round($like/($like+$unlike),3)*100; 
    $arr['like_percent'] = $like_percent.'%'; 
    $arr['unlike_percent'] = (100-$like_percent).'%'; 

    return json_encode($arr); 
} 

2 .PHP投票小程序,防超时,防IP限制

<?php
//欢迎访问back.zhizhi123.com
//参考博文http://www.zhizhi123.com/?p=126
set_time_limit(0);//防止超时

function toupiao($i)

{

        $proxy = array('182.48.116.51:8080','110.177.63.191:9999','221.10.40.238:80','221.10.40.236:83','221.10.40.237:80','221.10.102.199:82','42.121.0.247:9999','42.121.28.111:3128','117.135.194.139:80','119.147.91.21:80','58.247.109.243:80','118.233.36.246:8585');

        $url = "http://www.example.com/tea/love.php";

        $ch = curl_init();

       $data = array (
        'id' => '13'
        );

//设置来源地址,如果不设置,论坛服务器有可能有验证不允许回复

curl_setopt($ch,CURLOPT_REFERER,"http://www.example.com/tea/index.php?m=content&amp;c=index&amp;a=lists&amp;catid=10");

//能保存cookie

curl_setopt($ch,CURLOPT_COOKIESESSION,true);

curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");

curl_setopt($ch, CURLOPT_PROXY, $proxy[$i]);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_URL,$url);

//为了支持cookie

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

if(curl_errno($ch))

{

        echo 'Curl error: ' . curl_error($ch);

}

}

for($i=0;$i<=11;$i++)

{

        echo "begin 第".$i."次投票";

        toupiao($i);

        echo 'end';

        echo "</br>";

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值