当前位置: 首页 > news >正文

用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;
}
?>
 
------------------------------------------------------------------------------------------

相关文章:

  • 用PHP写的POP3电子邮件收取流程
  • linux下安装sniffit
  • linux mail命令
  • 网站不允许上传asp cer cdx htr等文件时
  • WINDOWS服务器安全设置
  • 全面封杀WVS扫描器扫描网站目录
  • webIPS防止扫描软件扫描网站
  • Linux 服务器安全配置
  • MSSQL安全设置的具体步骤和方法
  • Linux菜鸟入门级命令大全
  • ubuntu 10.10正式版
  • 菜鸟入门 Ubuntu 常用命令收集
  • 一系列测试技术
  • Web安全测试知多少
  • 常用的网站功能测试方法
  • #Java异常处理
  • 【comparator, comparable】小总结
  • 【翻译】babel对TC39装饰器草案的实现
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • Python 基础起步 (十) 什么叫函数?
  • SAP云平台里Global Account和Sub Account的关系
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • SQLServer之创建数据库快照
  • yii2中session跨域名的问题
  • 读懂package.json -- 依赖管理
  • 二维平面内的碰撞检测【一】
  • 简单数学运算程序(不定期更新)
  • 普通函数和构造函数的区别
  • 一道闭包题引发的思考
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 阿里云ACE认证学习知识点梳理
  • ​决定德拉瓦州地区版图的关键历史事件
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (差分)胡桃爱原石
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .gitignore文件---让git自动忽略指定文件
  • .NET DataGridView数据绑定说明
  • .NET 服务 ServiceController
  • .NET学习教程二——.net基础定义+VS常用设置
  • .NET运行机制
  • []AT 指令 收发短信和GPRS上网 SIM508/548
  • [202209]mysql8.0 双主集群搭建 亲测可用
  • [AIGC] 使用Curl进行网络请求的常见用法
  • [C++数据结构](31)哈夫曼树,哈夫曼编码与解码
  • [CTSC2014]企鹅QQ
  • [DevOps云实践] 彻底删除AWS云资源
  • [Django开源学习 1]django-vue-admin
  • [HeadFrist-HTMLCSS学习笔记][第一章Web语言:开始了解HTML]
  • [luoguP3159] [CQOI2012]交换棋子(最小费用最大流)
  • [nlp] id2str的vocab.json转换为str2id
  • [QT] TCP协议演示
  • [selenium] Handling Untrusted SSL certificate error in firefox
  • [shell,hive] 在shell脚本中将hiveSQL分离出去
  • [SPOJ]COT2