cofig.php
<?php /**********************系统常量定义***********************/ /*数据库配置信息*/ $db_host = "localhost"; $db_user = "123456"; $db_pwd = "123456"; $db_name = "shujuku"; $db_prefix = "01_"; /*鼠标放到行上的效果*/ $onmouseover="onmouseout=this.style.backgroundColor='' onmouseover=this.style.backgroundColor='#f6f6f6'";
functions.php
<?php /*****************连接数据库函数*********************/ function getLink($db_host,$db_user,$db_pwd,$db_name) { //连接数据库 $link = @mysql_connect($db_host,$db_user,$db_pwd); if(!$link) { echo "<li>MySQL服务器连接失败,请与管理员联系</li>"; exit(); } if(!mysql_select_db($db_name)) { echo "<li>MySQL数据库选择失败,请与管理员联系</li>"; exit(); } mysql_query("set names utf8"); //连接标识符返回 return $link; }
conn.php
<?php //设置当前页面字符集为utf8 header("content-type:text/html; charset=utf-8"); /*包含文件*/ require("config.php"); require("functions.php"); //连接数据库 $link = getLink($db_host,$db_user,$db_pwd,$db_name); //echo "123";
login.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>后台登陆</title> </head> <body> <form id="form1" name="form1" method="post" action="login_check.php"> <table width="200" border="0" cellpadding="0" cellspacing="0"> <tr> <td>用户名:</td> <td><input type="text" name="username" id="username" size="10" maxlength="20" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" id="password" size="10" maxlength="20" /></td> </tr> <tr> <td> </td> <td> <input type="submit" name="btnlogin" id="btnlogin" value="登陆" /> <input type="hidden" name="ac" value="login1" /> </td> </tr> </table> </form> </body> </html>
login_check.php
<?php //包含数据库的文件 require("include/conn.php"); //判断表单数据是否有login.php传过来的 if(isset($_POST["ac"]) && $_POST["ac"] == "login1") { // echo "登陆成功!"; //获取用户名和密码 $username = trim($_POST["username"]); $password1 = trim($_POST["password"]); $password = md5($password1); // echo $password; //查询数据库中的账号和密码,与用户输入的账号和密码是否匹配 $sql = "SELECT * FROM {$db_prefix}admin WHERE username='$username' and password='$password'"; $result = mysql_query($sql); $records = mysql_num_rows($result); // echo $records; //判断记录总数是否为1,如果为1则将用户登陆信息写入数据库,如果为0,跳转登陆页面 if($records == 1) { $lastloginip = $_SERVER["REMOTE_ADDR"]; $lastlogintime = time(); // echo $lastlogintime; //更新数据库 $sql = "UPDATE {$db_prefix}admin SET lastloginip= '$lastloginip', lastlogintime=$lastlogintime, loginhits=loginhits+1 WHERE username='$username'"; // if(mysql_query($sql)) // { // $message = "登陆成功!"; // $url = "login.php"; // echo "<script>location.href='success.php?url=$url&message=$message'</script>"; // exit(); // } }else { $message = "用户名或密码错误,请重新登陆!"; echo "<script>location.href='error.php?message=$message'</script>"; exit(); } }else { $message = "非法用户!"; echo "<script>location.href='error.php?message=$message'</script>"; exit(); }