本身用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;
}
?>
 
------------------------------------------------------------------------------------------
趙小蝸牛
QQ: 755721501
在不斷奉獻中謀求生存發展、打造本身的優秀品質,用人性最本質最動人的一面「營銷」本身!
相關文章
相關標籤/搜索