ASP.Net 前臺登陸注冊和登陸人次的統計簡單實現

C#学习笔记(一):ASP.Net中基本对象和数据库操作

通过学习ASP.Net实现前一篇文章中程序的“前台”

对ASP.Net中几个基本对象使用的小结(即学即更)

ASP.Net 几个基本对象学习笔记

Response:在客户端发送的请求,服务端用一个响应对象来处理该请求,并将响应数据
发送到客户端.
结束之后销毁该对象;
最常用的方法是重定向到一个url
Response.Redirect(“url”);

Request: 获取该页的System.Web.HttpRequest对象
客户端向服务器发送请求。

目前使用到了 Request.Cookies

Session:对话,默认生命周期是20分钟
这个印象比较深,可以实现页级的数据传输
在一级页面创建对话,通过Global对Session_Start()进行设置,
在二级页面接收数据.
生命周期可以对Session.Timeout赋值来设置默认单位为分钟
Session有一个特点是可以储存任意类型的数据,需要使用时需要强制转换
对话的销毁 Session.Abandon();销毁一个对话

Application:
用来保存公共信息,比如网站登录人次统计

两个重要的方法:(1)Application.lock():锁定Web应用程序状态促进同步访问
(2) Application.Unlock():取消对Web应用程序状态的锁定以促进同步访问
统计人次的例子中需要结合新会话启动的方法结合使用,考虑到Application下方法返回类型
需要强制类型转换.

Session和Application 都涉及到Web窗体程序的Global事件
参看Global的事件有哪些,用到再现学
推荐这篇文章:
还有这篇,差不多吧都

  • 小结cookies的基本用法:

  • 一、创建Cookies
    HttpCookie cookie = new HttpCookie(“Login”);
    //生成名为 cookie 的HTTPCookie 对象其主键为Login,
    //也就是说我们之后要用Login作为索引来进行操作
    cookie.Values.Add(“Loginname”,string )
    //添加你需要的第一个子键,这里例子是登陆名
    cookie.Values.Add(“Loginpwd”,string )
    //第二个子键,更多的话可以按这个格式添加
    //如果只有一个子键则调用cookie.Value.Add()方法
    //string 在这个重载方法中是指cookie子键的值,可类比之前
    //数据库知识,子键看做字段,值概念一样.
    cookie.Expires = DateTime.Now.AddHours(2);
    //这里是设置cookies生命周期,在这里设置为事件发生算起保留2个小时
    //具体设置多少根据具体需求,DateTime.Now提供了多种方法供选择,
    //此外还有不同的设置时间的方法,移步其他博主博客。
    Response.Cookies.Add(cookie)
    //提交Cookies,按Response对象的基本作用,这里是客户端响应服务器请求
    //数据传到浏览器在本地写下Cookies文件.
    一般地,创建一个Cookies,我们创建一些子键并赋值,来记录数据,同时设
    置cookies的生命周期,最后通过response方法提交数据

  • 二.销毁Cookies:
    在这里我并不需要cookies来帮助我们记住一些信息,所以我们要做的是
    事件结束后cookies立即销毁,
    Httpcookie cookie = Request.Cookies[“AutoLogin”];
    //客户端向服务器发送请求查找[“AutoLogin”]为索引的Cookies
    if(cookie!=null)//存在的话我们将生命周期设为-1,也就是立即销毁,并更新
    cookie.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Add(cookie);//更新cookie

  • 三.使用cookies
    当我们需要使用cookies中的数据来实现“自动登录”功能的时候
    HttpCookie cookie = Request.Cookies[“AutoLogin”]
    if(!cookie=null)
    {
    //将你所需要的数据从cookie的子键中拿出来
    //例如: this.loginname.Text=Cookie[“Loginname”].ToString();
    }

数据库的链接访问实现登陆注册和登录人次统计等功能.
  • 对上一次Funsql增加的方法:便于实现注册时检查用户名是否存在
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data;

namespace WebApplication1
{
    public class Funsql
    {
        //查询
        public DataTable ExecuteQuery(string sqlstr)
        {
            //这个就是命令行执行
            string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
            MySqlCommand cmd;
            //这个是用来建立数据库连接的类    
            MySqlConnection con;
            //包含sql语句执行的结果,并提供一个方法从结果中阅读一行
            MySqlDataAdapter msda;
            //建立虚拟表
            DataTable dt = new DataTable();
            //上面两个配合使用,先建立连接后通过执行sqlstr语句
            //得到msda数据,然后这个数据填充在虚拟表dt中,最终返回数据
            con = new MySqlConnection(url);
            con.Open();//接通数据库
            cmd = new MySqlCommand(sqlstr, con);//执行查询语句,sqlstr从点击中来
            cmd.CommandType = CommandType.Text;//用文本形式解释命令行字符串;
            msda = new MySqlDataAdapter(cmd);//SELECT命令作为数据更新参数
            msda.Fill(dt);
            con.Close();//关闭链接
            /**/
            return dt;
        }
        //增删改
        public int ExecuteUpdate(string sqlstr)
        {

            string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";



            MySqlConnection con = new MySqlConnection(url);
            con.Open();//接通数据库
            MySqlCommand cmd = new MySqlCommand(sqlstr, con);//cmd命令在这里得到相应命令
                                                             // cmd.CommandType = CommandType.Text;//用文本形式解释命令行字符串;
            int exeresult = 0;
            exeresult = cmd.ExecuteNonQuery();//直接对数据库中数据动手

            return exeresult;
            //返回的是处理数据个数
        }

  
        //注册查找用户名是否存在于 表中
        public int ExecuteCheck(string sever, string port ,string database,string user,string pwd,string sqlcheck,string srhtab)
        {
            string url = "server = "+sever+";port ="+port+";database ="+database+";user ="+user+"; password ="+pwd+";";
            MySqlConnection con = new MySqlConnection(url);
            con.Open();
            MySqlCommand cmd = new MySqlCommand(sqlcheck, con);
            MySqlDataAdapter check = new MySqlDataAdapter(cmd);
            DataSet checkset = new DataSet();
            int n = check.Fill(checkset, srhtab);
            con.Close();
            return n;//返回的值不为零说明存在和查找条件相符的数据
        }
        
        

    }
}
  • 注冊
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using MySql.Data.MySqlClient;
using System.Data;
namespace WebApplication1
{
    public partial class First : System.Web.UI.Page
    {
 
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            string id = this.txtuid.Text; string pwd = this.txtpwd.Text;
            string repwd = this.txtrpwd.Text;
            string sqlin= "insert into student.user(userid,userpwd) values ('" + id + "','" + pwd + "');";
            string selcheck = "select userid from student.user where userid='" +  id + "';";//用来检查用户名是否存在
            string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
            Funsql Dog = new Funsql();
 
                
                 
                    MySqlConnection con = new MySqlConnection(url);
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand(selcheck, con);
                    MySqlDataAdapter check = new MySqlDataAdapter(cmd);
                    DataSet setcheck = new DataSet();
                    int n = check.Fill(setcheck, "user");
            if (n != 0)//找到相同的用户名
            {
                this.txtuid.Text = "";
                Response.Write("用户名已被使用");
            }
            else
            {
                Dog.ExecuteUpdate(sqlin);//将用户信息保存在数据库
                                         //跳转进入登陆页面
                con.Close();//断开连接了嗷
                Thread.Sleep(3000);//等待3秒
                Response.Redirect("Login.aspx");
                
            }

        }
        //取消的话返回登陆界面
        protected void brncl_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }
    }
}
  • 登陸
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        //为了解决密码不能被COOKIE赋值,将登陆实际需要的
        protected void Page_Load(object sender, EventArgs e)
        {
               //判断是否是首次进入页面,然后获取cookie 实现自动登陆
            if(!IsPostBack)
            {
                //请求获取索引为自动登录COOKIE中的信息
                HttpCookie cookie = Request.Cookies["AutoLogin"];
                if (cookie != null)
                {
                    this.loginuid.Text = cookie["Loginname"].ToString();
                    this.loginpwd.Text = cookie["Loginpwd"].ToString();
                   //这种赋值是不行的,因为password型或许没有set方法
                }
            }
        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
             
            
            string userid = this.loginuid.Text;
            string userpwd = this.loginpwd.Text;
            string sqlcheck = "Select * from student.user where userid='" + userid + "' and userpwd='" + userpwd + "';";
            string url = "server = localhost;port=3306;database = student;user=root;password=159753abcDEF";
            MySqlConnection con = new MySqlConnection(url);
            con.Open();
            MySqlCommand cmd = new MySqlCommand(sqlcheck,con);
            MySqlDataAdapter srh = new MySqlDataAdapter(cmd);
            DataSet srhcheck = new DataSet();
            int n = srh.Fill(srhcheck, "user");
            if(n!=0)
            {
                //登陆成功 
                
                this.lab2.Text = "登陆成功辣!正在跳转...";
                //
                //利用Session将用户信息发送
                Session["User"] =userid;//创建了对话
                Response.Redirect("Main.aspx");         
            }
            else
            {
                //登陆失败
                this.lab2.Text = "用户名或密码错误,请重试";
                this.loginuid.Text = "";
                this.loginpwd.Text = "";
            }
            
        }
        //注册页面跳转
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("First.aspx");
        }

        protected   void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(CheckBox1.Checked)
            {
                //写入cookies 在两小时内会有效
                //利用Cookie请求浏览器保存用户信息
                HttpCookie cookie = new HttpCookie("AutoLogin");
                cookie.Values.Add("Loginname", loginuid.Text);
                cookie.Values.Add("Loginpwd", loginpwd.Text);
                cookie.Expires = DateTime.Now.AddHours(2);
                Response.Cookies.Add(cookie);
            }
            else//如果不记住的话cookie立即销毁
            {
                //Request是向服务器请求索引COOKIS
                HttpCookie cookie = Request.Cookies["AutoLogin"];
                //如果找到了
                if(cookie!=null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-1);//立即销毁
                    Response.Cookies.Add(cookie);//实际上重新修改了信息

                }
            }
        }
    }
}
  • Global:關於Session設置和Application統計網站 人次
void Session_Start(object sender, EventArgs e)
        {
            //新会话的启动
            Session.Timeout = 1;//生命周期设置为1分钟        
            Application.Lock();
            Application["count"] = (int)Application["count"] + 1;//新会话启动后计数
            Application.UnLock();
            
        }

接著做吧,主功能挺簡單。接著去學習網站美化吧。嚶嚶嚶

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值