前端 html 用form 指向后台 LOGIN.PHP
<html><head>用户登录</head>
<form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
<p>
<label for="username" class="label">用户名:</label>
<input id="username" name="username" type="text" class="input" />
<p/>
<p>
<label for="password" class="label">密 码:</label>
<input id="password" name="password" type="password" class="input" />
<p/>
<p>
<input type="submit" name="submit" value=" 确 定 " class="left" />
</p>
</form>
</html>
后台 login.php
<?php//登录
if(!isset($_POST['submit'])){
exit('非法访问!')
}
$username = htmlspecialchars($_POST['username'])
$password = MD5($_POST['password'])
//包含数据库连接文件
include('conn.php')
//检测用户名及密码是否正确
$check_query = mysql_query("select * from user_list where username='$username' and password='$password' limit 1")
if($result = mysql_fetch_array($check_query)){
//登录成功
session_start()
$_SESSION['username'] = $username
$_SESSION['userid'] = $result['userid']
echo $username,' 欢迎你!进入 <a href="my.php">用户中心</a><br />'
echo '点击此处 <a href="login.php?action=logout">注销</a> 登录!<br />'
exit
} else {
exit('登录失败!点击此处 <a href="javascript:history.back(-1)">返回</a> 重试')
}
//注销登录
if($_GET['action'] == "logout"){
unset($_SESSION['userid'])
unset($_SESSION['username'])
echo '注销登录成功!点击此处 <a href="login.html">登录</a>'
exit
}
?>
aspx 里面加2个textbox 一个button.cs文件里面代码 我的服务器是.
protected void Button1_Click(object sender, EventArgs e)
{
string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlconn"])
SqlConnection conn = new SqlConnection(connString)//创建数据库链接
conn.Open()
string strsql = "select * from stuent where StuName='" + TextBox1.Text + "'and Stupassword='" + TextBox2.Text + "'"
SqlCommand cmd = new SqlCommand(strsql, conn)
SqlDataReader dr = cmd.ExecuteReader()
if (dr.Read())
{
Session["StuName"] = dr["StuName"]
Session["Stupassword"] = dr["Stupassword"]
if (TextBox1.Text == Session["StuName"].ToString() &&TextBox2.Text == Session["Stupassword"].ToString())
{
Response.Redirect("Default2.aspx")
}
}
else
{
Label1.Text = "登陆失败,请检测输入!"
}
}
web.config
<add name="sqlconn" connectionString="Data Source=.Integrated Security=SSPIInitial Catalog=xuesheng" providerName="System.Data.SqlClient"/>
1.建好用户表,表中设好用户名和密码字段及其内容;
2.登录窗体中,建立三个控件,一个用户名文本框,一个密码文本框,还有一个登陆按钮;
3.登录按钮的单击事件代码:
if isnull(me.用户名) or isnull(me.密码) thenmsgbox "请输入正确的用户名和密码!"
elseif isnull(DLookup("密码","用户表","用户名='" & me.用户名 & "'")) then
msgbox "用户名不存在!"
elseif DLookup("密码","用户表","用户名='" & me.用户名 & "'")=me.密码 then
docmd.openform 窗体名称
else
msgbox "密码错误!"
end if
'祝你成功!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)