用PHP写的简单登陆首页

【小蜗牛嗷嗷之作
 
    需要事先在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();
    }
}
?>
<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">&nbsp;</td>
      <td height="160" bgcolor="#4f33d7">&nbsp;</td>
      <td bgcolor="#5b33d7">&nbsp;</td>
    </tr>
    <tr>
      <td width="40%" bgcolor="#4f33d7">&nbsp;</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">&nbsp;</td>
            <td width="215">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" height="10">&nbsp;</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">密&nbsp;&nbsp;&nbsp;&nbsp;码:</td>
            <td><input type="password" name="login_pass" maxlength="16" value="123456abcdef" class="loginInput">
            </td>
          </tr>
          <tr>
            <td>&nbsp;</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>&nbsp;</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">&nbsp;</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">&nbsp;</td>
    </tr>
    <tr>
      <td bgcolor="#5f33d7">&nbsp;</td>
      <td height="290" bgcolor="#4F33D7">&nbsp;</td>
      <td bgcolor="#5f33d7">&nbsp;</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;
}
?>
 
------------------------------------------------------------------------------------------

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值