一个简单的php用户登陆模块

发布时间:2020-10-08编辑:脚本学堂
本文介绍下,一个新手朋友写的php用户登录模块,很简单,适合初学的朋友参考。

包括如下页面:
 

left.php   登陆页面
userlogin.php  验证功能
login.php   注销功能
config.auth.php 数据库连接功能

说明:
left.php通过post提交信息给userlog.php处理,userlog.php调用config.auth.php连接数据库进行数据处理,然后把结果返回left.php,logout.php就只是注销session功能。没有加入cookie,还暂时不需要保存cookie!

1,left.php
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登陆系统</title>
</head>
<body>
<?php
    session_start(); 
    if ($_SESSION[username]) { 
    ?>
    <form id="form3" name="form3" method="post" action="">
    <table width="150" border="1">
        <tr>
          <td width="64">当前用户</td>
          <td width="70"><?php echo $_SESSION[username];?></td>
        </tr>
        <tr>
          <td colspan="2"><div align="center"><a href="logout.php">注销</a></div></td>
        </tr>
      </table>
    </form>
    <?php
    }else { 
    ?>
    <form id="form1" name="form1" method="post" action="userlogin.php">
      <table width="150" border="1">
        <tr>
          <td width="64">用户</td>
          <td width="70"><label>
            <input name="username" type="text" id="textfield" size="10" />
          </label></td>
        </tr>
        <tr>
          <td>密码</td>
          <td><label>
            <input name="password" type="password" id="textfield2" size="10" />
          </label></td>
        </tr>
        <tr>
          <td colspan="2">
            <div align="center">
              <input type="submit" name="submit" id="button" value="提交" />
              <input type="reset" name="reset" id="button2" value="重置" />
              </div></td>
        </tr>
      </table>
    </form>
    <?php
    } 
    ?>
    </body>
    </html>

2,config.php
 

复制代码 代码示例:
<?php 
    $dbhost="localhost"; 
    $dbuser="root"; 
    $dbpassword="123456"; 
    $dbname="auth"; 
    $conn=mysql_connect("$dbhost","$dbuser","$dbpassword") or die('不能连接服务器'.mysql_error()); 
    mysql_select_db("$dbname",$conn) or die("数据库访问错误".mysql_errno()); 
    mysql_query("set names gb2312"); 
?>

3,userlogin.php
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>登陆系统_www.jb200.com</title> 
</head> 
<body> 
<?php 
    session_start(); 
    require("config.auth.php"); 
    if ($_POST[submit]) { 
        $sql="select * from userinfo where username = '$_POST[username]' and password = '$_POST[password]'"; 
        $result=mysql_query($sql); 
        $numrows=mysql_num_rows($result); 
        if ($numrows == 1) { 
            $row=mysql_fetch_assoc($result); 
            $_SESSION[username]=$row[username]; 
            //echo $_SESSION[username]; 
            echo "<script>alert('登陆成功!');window.location.href='left.php';</script>"; 
        }else { 
            echo "<script>alert('登陆失败!');window.location.href='#'" /script>"; 
        } 
    }else { 
        echo "<script>alert('请通过正确途径登陆!');window.location.href='#'" /script>"; 
    } 
    mysql_free_result($result); 
    mysql_close($conn); 
?> 
</body> 
</html>

4,logout.php
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>登陆系统_www.jb200.com</title> 
</head> 
<body> 
<?php 
 session_start(); 
 unset($_SESSION[username]); 
 session_destroy(); 
 echo "<script>alert('注销成功!');window.location.href='#'" /script>"; 
?> 
</body> 
</html>