asp.net c投票系统(防刷功能)

asp教程.net c投票系统(防刷功能)
本文章是利用了asp.net教程 mssql做的一款防刷投票系统,他是利用了cookie来实现的。
*/

using system;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.data.sqlclient;

public partial class _default : system.web.ui.page
{
    protected string title = "1";   

    protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {
            votefun();
        }
    }

    protected void votefun()
    {
       
        sqlconnection conn = dbconn.conn();
        sqlcommand cmd = new sqlcommand("select vitems from votetest where vid = '" + title + "'", conn);
        try
        {
            conn.open();
            this.showtitle.text = convert.tostring(cmd.executescalar());
            cmd.commandtext = "select * from votedetails where vid ='" + title + "'";
            this.rbl.datasource = cmd.executereader();
            this.rbl.datatextfield = "contents";
            this.rbl.datavaluefield = "id";
            this.rbl.databind();
        }
        catch (sqlexception ex)
        {
            response.write(ex.errors.tostring());
        }
        finally
        {
            conn.close();
            cmd.dispose();
        }
    }
    //    投票

    protected void butvoteyes_click(object sender, eventargs e)
    {
        if (this.rbl.selectedindex != -1)
        {
            //投票防作弊
            httpcookie makecookie = new httpcookie("vote");//制造cookie
            httpcookie readcookie = request.cookies["vote"];//读出cookie
            //response.write("<hr/>" + title);
            if (readcookie == null)//从未投过票
            {
                makecookie.values.add("voteitem", title);
            }
            else
            {
                string strallitem = readcookie.values["voteitem"].tostring();//读取已投票的项
                if (strallitem.indexof(title) == -1)//未投过票
                {
                    makecookie.values.add("voteitem", readcookie.values["voteitem"] + title);
                }
                else//如果已投过票
                {
                    response.write("<script language=网页特效>alert('该主题你已经成功投过票,不能重新投票!');</script>");
                    return;

                }
            }
            response.appendcookie(makecookie);

 

 

 

 

 

 

 

            string selectid = this.rbl.selectedvalue.tostring();
            sqlconnection conn = dbconn.conn();
            sqlcommand cmd = new sqlcommand("update votedetails set num = num +1 where id = '" + selectid + "'", conn);
            try
            {
                conn.open();
                cmd.executenonquery();
                showmessage.box("投票成功!");
            }
            catch (sqlexception ex)
            {
                response.write(ex.errors.tostring());
            }
            finally
            {
                conn.close();
                cmd.dispose();
            }
        }
        else
        {
            showmessage.box("请选择投票项目");
        }
    }
    /// <summary>
    ///    查看结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butvoteno_click(object sender, eventargs e)
    {
        response.redirect("showvotedetails.aspx?vid="+title+"");
    }

 

 

 

 

 

 

 

 

 

 

    #region  测试代码
    //public static string m_str_voteid;
    //protected void page_load(object sender, eventargs e)
    //{
    //    if (!ispostback)
    //    {
    //        //获取用户选择的投票标题
    //        m_str_voteid = request["voteid"];
    //        //绑定投票标题和投票选项
    //        labbind();
    //        rblbind();
    //    }
    //}
    ////绑定label控件
    //private void rblbind()
    //{
    //    dataset ds = db.reds("select votetitle from tb_vote where voteid=" + m_str_voteid);
    //    labvotetitle.text = ds.tables[0].rows[0][0].tostring();
    //}
    ////绑定radiobuttonlist控件
    //private void labbind()
    //{
    //    dataset ds = db.reds("select * from tb_vote where voteid=" + m_str_voteid);
    //    rblvoteitem.datasource = ds;
    //    //votecontent字段绑定到text,voteitemid绑定到value
    //    rblvoteitem.datatextfield = "votecontent";
    //    rblvoteitem.datavaluefield = "voteitemid";
    //    rblvoteitem.databind();
    //}
    ////投票按钮
    //protected void btnvote_click(object sender, eventargs e)
    //{
    //    //投票防作弊
    //    httpcookie makecookie = new httpcookie("vote" + m_str_voteid);//创建cookie
    //    httpcookie readcookie = request.cookies["vote" + m_str_voteid];//读取cookie
    //    if (readcookie == null)
    //    {
    //        //从未投过票的话,将参与投票的标题放进cookie,并设置过期时间
    //        makecookie.values.add("voteitem", "<" + m_str_voteid + ">");
    //        makecookie.expires = datetime.maxvalue;
    //    }
    //    else
    //    {
    //        //读取已投票的项
    //        string p_str_allitem = readcookie.values["voteitem"].tostring();
    //        //如果未对该主题投过票,将该主题添加到cookie中
    //        if (p_str_allitem.indexof("<" + m_str_voteid + ">") == -1)
    //        {
    //            makecookie.values.add("voteitem", readcookie.values["voteitem"] + "<" + m_str_voteid + ">");
    //        }
    //        else
    //        {
    //            response.write("<script language=javascript>alert('该主题你已经投过票,不能重新投票!');</script>");
    //            //这里return很关键,直接跳出btnvote_click事件
    //            return;
    //        }
    //    }
    //    //执行投票操作,票数+1
    //    string p_str_voteitemid = this.rblvoteitem.selectedvalue;
    //    string p_str_cmdtxt = "update tb_voteitem set votetotal=votetotal+1 where voteitemid=" + p_str_voteitemid + " and voteid=" + m_str_voteid;
    //    bool p_bl_reval = db.exsql(p_str_cmdtxt);
    //    if (p_bl_reval)
    //    {
    //        //写入cookie
    //        response.appendcookie(makecookie);
    //        //在新窗口中弹出投票结果
    //        response.write("<script language=javascript>alert('投票成功,感谢你的支持!');windows.open('voteresult.aspx?voteid=" + m_str_voteid + "&title=" + server.urlencode(labvotetitle.text) + "','new');</script>");
    //    }
    //    else
    //    {
    //        response.write("<script language=javascript>alert('投票失败!');</script>");
    //    }
    //}
    ////查看结果按钮
    //protected void btnresult_click(object sender, eventargs e)
    //{
    //    response.write("<script language=javascript>alert('投票成功,感谢你的支持!');windows.open('voteresult.aspx?voteid=" + m_str_voteid + "&title=" + server.urlencode(labvotetitle.text) + "','new');</script>");
    //}
    #endregion
}

时间: 2024-08-02 14:30:05

asp.net c投票系统(防刷功能)的相关文章

投票系统防刷代码

投票系统防刷代码 $value =$this->host;         if(empty($_COOKIE["cook"])){      setcookie("cook", $value, time()+1800, "/");       $v_host = $this->host;         $v_ip = $this->get_real_ip();           $v_date =date("Y-

简单投票系统[防刷程序刷新]

简单投票系统[防刷程序刷新] <?php include_once("../inc/connect.php");  $value =$_SERVER['HTTP_HOST'];   if(empty($_COOKIE["cook"])){     if(sizeof($_POST)<9){ exit("<script>alert('对不起,你还有选项未选!');history.back();</script>"

用ASP.NET 2.0设计网络在线投票系统

asp.net|设计|投票|网络|在线 一.系统功能设计和数据库设计 1.系统功能设计和数据库设计 1.1 系统功能设计 网络在线投票系统实现的功能比较简单,具体如下: ◎投票项目的管理: ◎添加投票的项目: ◎删除投票的项目: ◎对项目进行投票: ◎查看项目的投票情况. 1.2 数据库设计 本系统的数据库设计比较简单,只需要存储投票的信息即可.在SQL Server 2000中创建一个数据库,名称为"WebVoteDB",并在该数据库中创建投票项目表Votes.其中"Vot

用Dreamweaver MX 2004制作ASP动态网站(投票系统篇)

dreamweaver|动态|投票 投票程序源文件:点击这里下载源文件 一.程序说明及投票系统流程图 总的来说,投票系统可分为3个模块:选票模块,选票处理模块和结果显示模块! 投票系统需要对某一项目的选择做出记录,一般情况下是存放在数据库中然后对投票情况进行统计并显示投票结果. 首先给出选票,即供投票者选择的窗体对象,当投票者按下投票按钮后,选票处理模块开始激活,对传送到服务器的数据作相应的处理,服务器端在处理时先判断用户选择的是那一项,然后把相应字段的值加1.实际上保存投票结果的数据库中的表只

投票系统-asp.net mvc 在safari无法设置和读取cookie

问题描述 asp.net mvc 在safari无法设置和读取cookie 最近做一个投票系统,需要用到cookie,在safari浏览器下 无法保存和获取cookie,其他浏览器都没有问题,浏览器已设置接受cookie,请问这是什么原因造成的? 解决方案 http://www.cnblogs.com/zsxfbj/p/safari_cookie_not_write.html 你的页面传值怎么传的,用session看看 解决方案二: 在同一个ActionResult中可以获取到,但是在其他Act

简单asp投票系统源码

本文章 一款简单asp教程投票系统源码,同时可以判断用户是否是当天第一次投票,如果不是就提示感谢您的支持,您已经投过票了,谢谢,否则就增加投票资料,并且把用户的ip地址保存到cookies里面,这样可以判断用户是否投票了. <%dim options,total,sql,i,answer if request.querystring("stype")="" then  if request.servervariables("remote_addr&q

防作弊很强的投票系统机制源代码

源代码|作弊|投票系统 这个是投票前一页代码.在此页点击投票.<%Response.Cookies("cook")="yes"%>以下是投票页代码<%if Request.Cookies("cook")<>"yes" thenresponse.write "<script language='javascript'>alert('cookies没开启无法进行投票');loca

Django快速开发之投票系统

参考官网文档,创建投票系统. ================ Windows  7/10 Python 2.7.10 Django 1.8.2 ================     1.创建项目(mysite)与应用(polls)            D:\pydj>django-admin.py startproject mysite D:\pydj>cd mysite D:\pydj\mysite>python manage.py startapp polls 添加到sett

国内ASP开源建站系统一览

使用ASP做网站虽然有点落伍,但在中国还是有很大市场的,因为大部分国内用户使用Windows Server服务器,在Windows Server的IIS环境下,ASP+SQLServer的性能丝毫不逊于PHP+MySQL,可惜ASP不支持跨平台,不支持Apache,因此发展受到了很大限制.在开源项目上,国内的ASP开源项目也没有PHP的多,不过也有一些,下面是我总结的一些基于ASP的建站开源系统. 论坛:DVBBS 动网论坛曾经是国内很流行的论坛,不过后来逐步衰落,在大用户量访问下,其性能不如D