求大神看看,小弟我做点赞时,限制每个ip只能点赞每篇文章一次,代码要如何实现

WBOY
Release: 2016-06-13 12:26:42
Original
1213 people have browsed it

求大神看看,我做点赞时,限制每个ip只能点赞每篇文章一次,代码要怎么实现。
//前端ajax提交数据

     <input type="button"  onClick="change_love({$article.article_id})" value="赞一下"/></a><br />      <!--<input type="button" value="点赞" onClick="zan()"/>--><br />      {$article.love}</p><br />    </div><br />  </div><br /></section><br /><script><br />function change_love(article_id)<br />{<br /> //var Lastsend = document.getCookie('ECS_auction_Lastsend_'+article_id);<br /> //alert('点赞成功');<br /> $.ajax({<br />    url:'article.php',//后台处理路径<br />    type:'post',//post提交方式<br />    data:{id:article_id},//点赞的文章id<br />    success:function(data){//数据返回成功      <br />       if(data==1){//成功        <br />      }<br />   }<br /><br />});<br />}<br /></script>
Copy after login



//后台更新数据库并判断ip是否存在
$realip=getip();<br />//echo $realip;<br />modifyipcount($realip);<br /><br />@$love=$_POST['id'];<br />if(isset($love)){<br />	$query="SELECT * FROM ecs_article_love where article_id='".$article_id."'";<br />	$result=mysql_query($query);<br />	$row=mysql_fetch_row($result);<br />	//dump($row);<br />	$ip1=$row[2];<br />	$ip2=$realip;<br />	//echo $ip;<br />	if(!empty($row) && strcmp($ip1,$ip2)==0){<br />		echo "<script>alert('ip已存在')</script>";<br />	}else{<br />		//$iptime=time();<br />		//$day=date('Y-m-d');<br />		//if($row[article_id]!=$article_id || $row[ip]!='')<br />		$query="INSERT INTO ecs_article_love (ip,article_id) VALUES ('".$ip2."','".$article_id."')";<br />		mysql_query($query);<br />		echo "<script>alert('点赞成功')</script>";<br />	}<br />}
Copy after login


//判断ip的表


------解决思路----------------------
我觉得吧,设计一个表,只有两个字段即可,一个是文章id,一个是用户的ip,两个字段作为联合主键。
每次用户访问,查询,查到了就是点赞过。没查到就可以点赞,由于是联合主键,肯定只能插入一条,因此通过数据库就帮你做了一次限制。查看文章的点赞数,直接where 文章id即可count。

因此,联合主键的顺序是:文章id,用户ip
------解决思路----------------------
前面上现的 html 和 js 代码,表示你向 ajax 返回数据的代码有问题(入口点不对)
下面出现的 php 错误信息,表示你的程序没有做容错处理

先解决了这些再说

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!