物联网phpDENGLU
php吧
全部回复
仅看楼主
level 2
1.captcha
<?php
session_start();
$chars = "abcdefghijklmnopqrstuvwxyz";
$chars .= strtoupper($chars);
$chars .="0123456789";
$len = strlen($chars);
header("content-type:image/gif");//header头文件
$im = imagecreatetruecolor(40,20);//画布
$red = imagecolorallocate($im,255,0,0);
$gray = imagecolorallocate($im,250,250,250);
imagefill($im,0,0,$gray);//填充画布
$captcha = "";
for($i=0;$i<4;$i++)
{
$randNum = rand(0,$len-1);//0-61中任意的一个整数
$randChar = $chars[$randNum];//以上数字和大小字母集合中的随机任意的一个字符
$captcha .= $randChar;
imagestring($im,5,2+$i*9,2,$randChar,$red);
}
$_SESSION["realCode"] = $captcha;
for($i=0;$i<rand(100,200);$i++)
{
$Rclo = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im,rand(0,40),rand(0,20),$Rclo);
}
imagegif($im);//与头文件中的gif对应
?>
2.conn
<?php
$conn = mysqli_connect("127.0.0.1","root","root","dbStudy");
mysqli_query($conn,"SET NAMES UTF8");
?>
3.home
<?php
session_start();
$aSex = array("女士","先生");
?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>模拟登陆</title>
<style type="text/css">
body {
font-family: "微软雅黑";
font-size: 14px;
margin: 10px;
}
</style>
<script>
function check()
{
var notice = "";
if(document.getElementById("txtNickName").value == "")
{
notice += "请输入昵称!\n\n";
}
if(document.getElementById("txtPwd").value == "")
{
notice += "请输入密码!\n\n";
}
if(document.getElementById("txtCaptcha").value == "")
{
notice += "请输入验证码!\n\n";
}
if(notice != "")
{
alert(notice);
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<?php
if(!isset($_SESSION["dengluren"] ) )
{
?>
<form action="login.php" method="post" onSubmit="return check()">
<table bgcolor="#FFCCCC" width="600" border="0" align="center" cellpadding="6" cel
lsp
acing="1">
<tr>
<td height="36" colspan="3" align="left" bgcolor="#FFFFFF">用户登录</td>
</tr>
<tr>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">昵称:</td>
<td width="40%" height="36" align="left" bgcolor="#FFFFFF"><input name="txtNickName" type="text" id="txtNickName" size="30"></td>
<td width="40%" height="36" align="left" bgcolor="#FFFFFF">*</td>
</tr>
<tr>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">密码:</td>
<td width="40%" height="36" align="left" bgcolor="#FFFFFF"><input name="txtPwd" type="text" id="txtPwd" size="30"></td>
<td width="40%" height="36" align="left" bgcolor="#FFFFFF">*</td>
</tr>
<tr>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">验证码:</td>
<td width="40%" height="36" align="left" bgcolor="#FFFFFF"><input name="txtCaptcha" type="text" id="txtCaptcha" size="10"><img src="captcha.php" class="captcha"></td>
<td width="40%" height="36" align="left" bgcolor="#FFFFFF">*</td>
</tr>
<tr>
<td height="36" colspan="3" align="center" bgcolor="#FFFFFF">
<input type="submit" id="btnGo" name="btnGo" value="登 录">
  
<input type="reset" id="btnCancel" name="btnCancel" value="重 填"></td>
</tr>
</table>
</form>
<?php
}
else
{
include_once("conn.php");
$selectSQL="SELECT * FROM `yu_USER` WHERE `user_nickname` ='".$_SESSION["dengluren"]."'";
$record =mysqli_query($conn,$selectSQL);
$row = mysqli_fetch_array($record);
mysqli_free_result($record);
mysqli_close($conn);
?>
<table bgcolor="#CCCCFF" width="600" border="0" align="center" cellpadding="6" cellspacing="1">
<tr>
<td height="36" colspan="4" align="left" bgcolor="#FFFFFF">用户信息</td>
</tr>
<tr>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">用户昵称:</td>
<td width="30%" height="36" align="left" bgcolor="#FFFFFF"><?=$row["user_nickname"]?></td>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">登录时间:</td>
<td width="30%" height="36" align="left" bgcolor="#FFFFFF"><?php echo date("Y/m/d H:i:s");?></td>
</tr>
<tr>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">用户真名:</td>
<td width="30%" height="36" align="left" bgcolor="#FFFFFF"><?=$row["user_realname"]?></td>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">用户电话:</td>
<td width="30%" height="36" align="left" bgcolor="#FFFFFF"><?=$row["user_tel"]?></td>
</tr>
<tr>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">用户性别:</td>
<td width="30%" height="36" align="left" bgcolor="#FFFFFF"><?=$aSex[$row["user_sex"]]?></td>
<td width="20%" height="36" align="right" bgcolor="#FFFFFF">用户邮箱:</td>
<td width="30%" height="36" align="left" bgcolor="#FFFFFF"><?=$row["user_email"]?></td>
</tr>
<tr>
<td height="36" colspan="4" align="center" bgcolor="#FFFFFF"><a href="logout.php" onClick="return confirm('您真的打算退出系统吗?')">注销退出</a></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
4.login
<?php
include_once("conn.php");
session_start();//但凡使用SESSION变量的任何页面的开头都必须加这句代码:启用会话。
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>login</title>
</head>
<body>
<?php
//先检查验证码输入有效,即用户输入的验证码是否与系统产生的验证码是否一致;
$userCaptcha = strtolower(trim($_POST["txtCaptcha"]));
$realCaptcha = strtolower($_SESSION["realCode"]);
if($userCaptcha != $realCaptcha)
{
echo "<script>alert('验证码不正确!');location='home.php';</script>";
}
else
{
$userNickName = trim($_POST["txtNickName"]);
$userPwd = md5(trim($_POST["txtPwd"]));
$selectSQL= "SELECT * FROM `yu_USER` WHERE `user_nickname` ='".$userNickName."' AND `user_pwd`='".$userPwd."'";
$record =mysqli_query($conn,$selectSQL);
$rowNum =mysqli_num_rows($record);
mysqli_free_result($record);
if($rowNum==0)
{
echo "<script>alert('昵称或密码错误');location='home.php';</script>";
}
else
{
$_SESSION["dengluren"]=$userNickName;
echo "<script>alert('登录成功');location='home.php';</script>";
}
}
?>
</body>
</html>
5.logout
<?php
include_once("conn.php");
session_start();//但凡使用SESSION变量的任何页面的开头都必须加这句代码:启用会话。
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>login</title>
</head>
<body>
<?php
//先检查验证码输入有效,即用户输入的验证码是否与系统产生的验证码是否一致;
$userCaptcha = strtolower(trim($_POST["txtCaptcha"]));
$realCaptcha = strtolower($_SESSION["realCode"]);
if($userCaptcha != $realCaptcha)
{
echo "<script>alert('验证码不正确!');location='home.php';</script>";
}
else
{
$userNickName = trim($_POST["txtNickName"]);
$userPwd = md5(trim($_POST["txtPwd"]));
$selectSQL= "SELECT * FROM `yu_USER` WHERE `user_nickname` ='".$userNickName."' AND `user_pwd`='".$userPwd."'";
$record =mysqli_query($conn,$selectSQL);
$rowNum =mysqli_num_rows($record);
mysqli_free_result($record);
if($rowNum==0)
{
echo "<script>alert('昵称或密码错误');location='home.php';</script>";
}
else
{
$_SESSION["dengluren"]=$userNickName;
echo "<script>alert('登录成功');location='home.php';</script>";
}
}
?>
</body>
</html>
2021年06月17日 06点06分 1
1