Cookie投票程序

在前台页面先加入RadioButtonList控件

   if (!IsPostBack)
        {
            Per();
        }

    }
    protected void btn_toupiao_Click(object sender, EventArgs e)
    {
        string P_str_IP = Request.UserHostAddress.ToString();//获取客户端IP地址
        HttpCookie oldCookie = Request.Cookies["userIP"];
        if (oldCookie == null)
        {
            int flag = RadioButtonList1.SelectedIndex;
            switch (flag)
            {
                case 0: addCount(Server.MapPath("result1.txt")); break;
                case 1: addCount(Server.MapPath("result2.txt")); break;
                case 2: addCount(Server.MapPath("result3.txt")); break;
            }
            ClientScript.RegisterStartupScript(GetType(), "", "alert(‘投票成功,谢谢参与!‘)", true);

            HttpCookie newCookie = new HttpCookie("userIP");//定义新的Cookie对象
            newCookie.Expires = DateTime.MaxValue;//设置Cookie有效时间
            //添加新的Cookie变量IPaddress,值为P_str_IP
            newCookie.Values.Add("IPaddress", P_str_IP);
            Response.AppendCookie(newCookie);//将变量写入Cookie文件中

        }
        else
        {
            string P_str_oldIP = oldCookie.Values["IPaddress"];
            if (P_str_IP.Trim() == P_str_oldIP.Trim())
            {
                ClientScript.RegisterStartupScript(GetType(), "", "alert(‘您已经投过票了,不能再次投票,谢谢参与!‘)", true);
            }
            else
            {
                HttpCookie newCookie = new HttpCookie("userIP");//定义新的Cookie对象
                newCookie.Expires = DateTime.MaxValue;//设置Cookie有效时间
                //添加新的Cookie变量IPaddress,值为P_str_IP
                newCookie.Values.Add("IPaddress", P_str_IP);
                Response.AppendCookie(newCookie);//将变量写入Cookie文件中
                int flag = RadioButtonList1.SelectedIndex;
                switch (flag)
                {
                    case 0: addCount(Server.MapPath("result1.txt")); break;
                    case 1: addCount(Server.MapPath("result2.txt")); break;
                    case 2: addCount(Server.MapPath("result3.txt")); break;
                }
                ClientScript.RegisterStartupScript(GetType(), "", "alert(‘投票成功,谢谢参与!‘)", true);
            }
        }
    }
    //绑定投票数的百分比到前端显示
    protected string M_str_rate1;
    protected string M_str_rate2;
    protected string M_str_rate3;
    protected int P_int_count1;
    protected int P_int_count2;
    protected int P_int_count3;
    public void Per()
    {
        P_int_count1 = readCount(Server.MapPath("result1.txt"));
        P_int_count2 = readCount(Server.MapPath("result2.txt"));
        P_int_count3 = readCount(Server.MapPath("result3.txt"));
        int P_int_count = P_int_count1 + P_int_count2 + P_int_count3;
        if (P_int_count == 0)
        {

            lblresult.Text = "共有0人参与投票";
        }
        else
        {
            M_str_rate1 = (Convert.ToDouble(P_int_count1) * 100 / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
            M_str_rate2 = (Convert.ToDouble(P_int_count2) * 100 / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
            M_str_rate3 = (Convert.ToDouble(P_int_count3) * 100 / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
            lblBanana.Text = M_str_rate1;
            lblApple.Text = M_str_rate2;
            lblWatermelon.Text = M_str_rate3;
            lblresult.Text = "共有" + P_int_count.ToString() + "人参与投票。";
        }
    }
    //读取指定路径文件中存在的投票数
    public static int readCount(string P_str_path)
    {
        int P_int_count = 0;
        StreamReader sr;
        sr = File.OpenText(P_str_path);
        while (sr.Peek() != -1)
        {
            P_int_count = int.Parse(sr.ReadLine());
        }
        sr.Close();
        return P_int_count;
    }
    //向指定路径文件中写入投票数
    public static void addCount(string P_str_path)
    {
        int P_int_count = readCount(P_str_path);
        P_int_count += 1;
        StreamWriter sw = new StreamWriter(P_str_path, false);
        sw.WriteLine(P_int_count);
        sw.Close();
    }

Cookie投票程序

时间: 2024-07-31 13:50:00

Cookie投票程序的相关文章

Bluemix云端数据库服务ClearDB MySQL使用示例———Python开发投票程序

越来越多的服务走向云端,看不见摸不着,让没接触过的我感觉有些茫然,IBM的云平台Blumemix上提供了很多服务,我这里就把学习使用ClearDBMySQL Database服务的过程给大家分享一下.这是一个PHP的投票小程序,针对主题"你曾后悔进入 IT 行业吗?"展开投票功能,基于简单的Cookie操作判断用户有没有投过票,如果没有投过票,就显示投票选项和按钮,不显示投票结果:如果已经投票了,就直接显示投票结果,而不能再次投票. 构建一个类似的应用程序的前提条件 一个 Bluemi

Bluemix云端数据库服务使用示例———PHP投票程序

越来越多的服务走向云端,看不见摸不着,让没接触过的我感觉有些茫然,IBM的云平台Blumemix上提供了很多服务,我这里就把学习使用ClearDB MySQL Database服务的过程给大家分享一下.这是一个PHP的投票小程序,针对主题"你曾后悔进入 IT 行业吗?"展开投票功能,基于简单的Cookie操作判断用户有没有投过票,如果没有投过票,就显示投票选项和按钮,不显示投票结果:如果已经投票了,就直接显示投票结果,而不能再次投票. 构建一个类似的应用程序的前提条件 一个 Bluem

利用map的特性做一个简易的投票程序

pair 1. 概念:pair是 一种模版类型,每个pair 可以存储两个值,这两种值的类型无限制.也可以将自己写的struct类型的的对象放进去. 2. 用法: pair<int ,int >p (1,2); pair<int ,int > p1= make_pair(1,2); 3. 编写程序读入一系列string和int型数据,将每一组存储在一个pair对象中,然后将这些pair对象存储在vector容器并显示 #include<iostream> #includ

uyhip问题 最苛刻的投票程序

一个国家里有 N 个公民,这些公民从 1 到 N 依次编号.这是一个民主国家,国家做出的每个决定都需要全体公民投票,每个人必须且只能投一票. 不过,随着该国家人口数量的增加,这种投票方式的效率越来越低.于是,这个国家实行了一种新的民主制度.每过四年,这个国家将会举行一次"代表选举大会",届时,每个公民都必须且只能提名一个他信得过的人,来作为他自己的代表.注意,提名自己作为自己的代表也是允许的.对于每个被提名了的人,有百分之多少的人提名他,他就拥有了相当于多少张选票的权力(向下取整).在

织梦自己写的投票程序

<?phpob_start();session_start();require_once("config.php");?> <!---表单部分点击投票<form name="frm" action="{dede:global.cfg_cmsurl/}/toupiao/index.php" method="post"><input type="hidden" name=&q

翁恺老师零基础学JAVA语言第五周数组--投票程序例题-个人理解

// 输入数量不确定的[0,9]范围内的整数,统计每一种数字出现的次数,输入-1表示结束 // 需要一个int[10]的数组来录入每次输入的数字;需要一个n表示每次输入的数字 // num[] = {0,1,2,3,4,5,6,7,8,9},刚好对应0-9这10个数字 Scanner in = new Scanner(System.in); int[] num = new int[10]; int n = in.nextInt(); if(n >= 0 && n <= 9) {

破解网络投票IP限制、验证码限制、COokie限制、Seesion限制的方法!(转)

顾名思义,网络投票就是在网络上进行的投票活动,但和其他类型的投票不同的是:网络投票是建立在网络投票系统上的,而结果完全由程序输出,无需人工参与.这既是网络投票系统的优点也是其缺点,没有了人工的参与,其结果很可能被黑客利用.而网络投票系统本身的技术含量并不高,对于连续投票的验证能力薄弱,只要黑客掌握了原理,就可以轻松地实现连续刷票,对投票结果进行作弊.那么,黑客是如何突破网络投票系统的,又是如何实现无限制刷票的呢?下面我们就来对此进行揭密. 网络投票的验证方式 参与过网络投票活动的朋友都知道,当我

Cookie和Session详解

会话技术 (一)Cookie 是客户端技术 javax.servlet.http.Cookie 1)Cookie是什么? Cookie是一段小信息.Servlet把这些小信息(HTTP响应头中的Set-Cookie)写到客户端的缓存中,客户端访问服务器时还能带着小信息(HTTP请求头中的Cookie). 2)Cookie的属性: name:必须的 value:必须的.并且value值不能是中文 comment:(可选的)注释 path:(可选的) 写Cookie的程序的访问路径是:<a targ

内置对象之Cookie

if (!this.IsPostBack) { try { HttpCookie MyCookie = new HttpCookie("MyCookie"); MyCookie.Value = Server.HtmlEncode("我的Cookie应用程序");//自动把乱码转换成正常的字符串,如果直接赋值字符串就会出现乱码 MyCookie.Expires = DateTime.Now.AddDays(5); Response.Cookies.Add(MyCook