需要事先在MySQL建立好数据库,存储有相关的用户信息,这个工作很简单,所以我的代码也没必要面面具到,只是大概说一下自己的登陆方法就可以。如果觉得我PHP菜鸟,请不要笑,我至今都还没有看过一点关于PHP的书籍呢!用都是最简单的语法,跟C一样,如果哪方面不懂就baidu一下就OK了。login.php里有完整的html代码,我当时设置了两种颜色的登陆界面,通过点击页面的某个地方就提交切换颜色,我这里的颜色切换还是用到php来判断输出颜色的(当然也可以用javascript来实现)。 ////////////// login.php
<?
include("./config.php");
if ($_POST['login']) {
/* 默认登陆失败 */
$login_status = "Failure";
$login_name = $_POST['login_name'];
$login_pass = $_POST['login_pass'];
$md5_pass = md5($login_pass);
/* 从数据库中获取用户信息做对比 */
$query = "select utype, realname from users where username='$login_name' and password='$md5_pass'";
if ($result = mysqli_query($link, $query)) {
if ($row = mysqli_fetch_row($result)) {
$login_status = "Success";
$utype = $row[0];
$realname = $row[1];
if ($realname == "") {
$realname = $login_name;
}
} else {
if ($thisStyle) {
$error_message = "密码错误,请重新输入!";
} else {
$error_message = "用户名或密码错误,请重新输入!";
}
}
mysqli_free_result($result);
} else {
$error_message = "未知错误!请通知系统管理员!";
}
/* 记录登陆日志 */
$client_ip = get_client_ip();
$query="insert into login_logs (login_name, login_time, login_status, client_ip) values('$login_name', now(), '$login_status', '$client_ip')";
$result = mysqli_query($link, $query);
if (!$result) {
printf("Can't query to MySQL Server. Errorcode: %s ", mysqli_error($link));
exit();
}
mysqli_close($link);
/* Let me go or not? */
if ($login_status == "Success") {
/* 验证成功,开启Session */
session_start();
$_SESSION['utype'] = $utype;
$_SESSION['username'] = $login_name;
$_SESSION['realname'] = $realname;
/* 跳转到 main.php */
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=main.php\">";
exit();
}
}
?>
SnailWarrior信息管理系统
用户名:class="loginInput">
密 码:
记住用户名记住密码
<html>
<head>
<title>登陆首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/common.css" rel="stylesheet" type="text/css" />
</head>
<?php
if ($_POST['adminLogin'] || ($_POST['login'] && $_POST['loginType'] == "admin")) {
$thisStyle = 1;
} else {
$thisStyle = 0;
}
if ($thisStyle) {
$color = "#cc0000";
$font_color = "white";
$loginType = "admin";
$chgLogin = "userLogin";
$chgLoginVal = "用户登陆";
} else {
$color = "#ffcc33";
$font_color = "red";
$loginType = "user";
$chgLogin = "adminLogin";
$chgLoginVal = "管理员登陆";
}
?>
<body>
<form name="loginForm" method="post" action="<?php echo $PHP_SELF; ?>" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#5b33d7"> </td>
<td height="160" bgcolor="#4f33d7"> </td>
<td bgcolor="#5b33d7"> </td>
</tr>
<tr>
<td width="40%" bgcolor="#4f33d7"> </td>
<td bgcolor="<?php echo $color ?>" height="210"><table bgcolor="#0000cc" align="center" width="280" cellspacing="0" style="table-layout:fixed">
<tr bgcolor="<?php echo $color ?>">
<td width="65" height="20"> </td>
<td width="215"> </td>
</tr>
<tr>
<td colspan="2" height="10"> </td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle" height="30" class="wbText18">SnailWarrior信息管理系统</td>
</tr>
<tr>
<td colspan="2" align="center" valign="bottom" height="20" style="color:#FFFF00;font:14px;font-weight:bold;"><?php if ($thisStyle) echo "管理员登陆"; else echo "用户登陆"; ?></td>
</tr>
<tr>
<td align="right" class="wbText12">用户名:</td>
<td><input name="login_name" maxlength="16" <?php if ($thisStyle) echo "value=\"admin\""; else echo "value=\"hoho\""; ?> class="loginInput"></td>
</tr>
<tr>
<td align="right" class="wbText12">密 码:</td>
<td><input type="password" name="login_pass" maxlength="16" value="123456abcdef" class="loginInput">
</td>
</tr>
<tr>
<td> </td>
<td><table cellspacing="0">
<tr style="color:#FFFF00;font:11px;font-weight:bold;vertical-align:bottom">
<?php if (!$thisStyle) { ?>
<td><input type="checkbox" name="remember_name"></td>
<td>记住用户名</td>
<?php } ?>
<td><input type="checkbox" name="remember_pass"></td>
<td>记住密码</td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td><table>
<tr>
<td width="70"><input name="login" type="submit" style="height:22px; font:11px; width:60px;" value="登录">
</td>
<td><input name="<?php echo $chgLogin ?>" type="submit" style="height:22px; font:11px; width:80px;" value="<?php echo $chgLoginVal ?>" >
</td>
</tr>
<tr><input name="loginType" type="hidden" value="<?php echo $loginType ?>"></tr>
</table>
</tr>
<tr>
<td height="6"> </td>
</tr>
<tr bgcolor="<?php echo $color ?>">
<td colspan="2" height="20" align="center" style="color:<?php echo $font_color ?>; font:12px;font-weight:bold;"><?php if($error_message) echo $error_message; ?></td>
</tr>
</table></td>
<td width="40%" bgcolor="#4f33d7"> </td>
</tr>
<tr>
<td bgcolor="#5f33d7"> </td>
<td height="290" bgcolor="#4F33D7"> </td>
<td bgcolor="#5f33d7"> </td>
</tr>
</table>
</form>
</body>
</html>
////////////// config.php
<?php
//error_reporting(0);
header("content-type: text/html; charset=UTF-8");
/* 数据库配置 */
$dbhost = "localhost";
$dbuser = "SnailWarrior";
$dbpass = "SnailPassword";
$dbname = "users";
/* 连接MySQL数据库 */
$link = mysqli_connect(
$dbhost, /* The host to connect to */
$dbuser, /* The user to connect as */
$dbpass, /* The md5_pass to use */
$dbname); /* The default database to query */
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit();
}
/* 设置utf8字符集 */
if (!mysqli_set_charset($link, "utf8")) {
printf("Error loading character set utf8: %s", mysqli_error($link));
}
/* 获取客户端IP */
function get_client_ip() {
$client_ip = "unknown";
if($_SERVER['HTTP_CLIENT_IP']){
$client_ip=$_SERVER['HTTP_CLIENT_IP'];
}elseif($_SERVER['HTTP_X_FORWARDED_FOR']){
$client_ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$client_ip=$_SERVER['REMOTE_ADDR'];
}
return $client_ip;
}
?>
------------------------------------------------------------------------------------------赵小蜗牛QQ: 755721501E-mail: snailwarrior@qq.com在不断奉献中谋求生存发展、打造自己的优秀品质,用人性最本质最动人的一面“营销”自己!