关于用户登录和注册处理的这一个模块这里做的是比较简单的,基本上是直接拿来用就可以的,这里自己直接给一下代码吧:

      首先是用户登录的首页:

<html>
<head>

<title>欢迎光临我的拍卖行</title>
<script language="JavaScript">
    //获得焦点
    function NameGetFocus()
    {
        document.frmLogin.user_name.focus();
    }

    //检查用户输入的昵称和密码是否合法
    function CheckValid()
    {
        if(document.frmLogin.user_name.value=="")
        {
            alert("Please input nick name!");
            document.frmLogin.user_name.focus();
            return false;
        }
        if(document.frmLogin.password.value=="")
        {
            alert("Please input password!");
            document.frmLogin.password.focus();
            return false;
        }   
        return true;
    }
</script>
</head>

<body onload="NameGetFocus()" bgcolor="#FFFFFF" text="#000000">
    <?php include("head.html")?>
<h1 align="center">欢迎光临我的拍卖行</h1>
<table width="75%" border="0" align="center" bgcolor="#FFFFFF">
  <tr> 
    <td align="center"> <img src="images/logo.gif" width="250" height="70"> 
    <td> 
  </tr>
  <tr> 
    <td align="center"> 
      <form name="frmLogin" method="post" action="check_user.php">
        用户名
        <input type="text" name="user_name">
        密码 
        <input type="password" name="password">
        <input type="submit" name="cmdLogin" value="登录" onclick="return CheckValid();">
      </form>
    </td>
  </tr>
  <tr> 
    <td align="center"><font color="#000099">如果您是首次登录本拍卖行,系统将自动注册您的信息</font> </td>
  </tr>
</table>
</body>
</html>

      接下来是对信息进行一个处理:

<?php
    //session_start();                                  //装载Session库,一定要放在首行
    //$user_name=$_POST["user_name"];
    //session_register("user_name");        //注册$user_name变量,注意没有$符号
session_start();                                    //装载Session库,一定要放在首行
$user_name=$_POST["user_name"];
$_SESSION["user_name"]=$_POST["user_name"];


    require_once("sys_conf.inc");           //系统配置文件,包含数据库配置信息

    //连接数据库
    $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);            
    mysql_select_db($DBNAME);              //选择数据库

     //查询是否存在登录用户信息
    $str="select name,password from buyer where name ='$user_name'";
    $result=mysql_query($str,$link_id);
    $rows=mysql_num_rows($result);
    $user_name=$_SESSION["user_name"];
    $password=$_POST["password"];
    //对于老用户
    if($rows!=0)
    {
        mysql_close($link_id);

        list($name,$password)=mysql_fetch_row($result);
        //如果密码输入正确
        if($password==$_POST["password"])
        {
            //转到商品显示页面  
            echo "<script language='javascript'>";  
            echo "location='display_goods.php';";
            echo "</script>";
        }
        //密码输入错误
        else
            require("relogin.php");
    }
    //对于新用户,将其信息写入数据库
    else
    {
        $str="insert into buyer (name,password) values('$user_name','$password')";
        $result=mysql_query($str, $link_id); //执行查询
        mysql_close($link_id);
        //转到商品显示页面  
        echo "<script language='javascript'>";  
        echo "location='display_goods.php';";
        echo "</script>";
    }
?>

      接下来是对没成功登录的人返回到登录的首页

      这个还是比较简单的和第一个有很多一样的地方:

<html>
<head>
<title>欢迎光临我的拍卖行</title>
<script language="JavaScript">
//获得焦点
function PasswordGetFocus()
{
    document.frmLogin.password.focus();
}

//检查用户输入的昵称和密码是否合法
function CheckValid()
{
    if(document.frmLogin.user_name.value=="")
    {
        alert("Please input user name!");
        document.frmLogin.user_name.focus();
        return false;
    }
    if(document.frmLogin.password.value=="")
    {
        alert("Please input password!");
        document.frmLogin.password.focus();
        return false;
    }   
    return true;
}
</script>
</head>

<body onload="PasswordGetFocus()" bgcolor="#FFFFFF" text="#000000">
    <?php include("head.html")?>
<h1 align="center"><font color=red>您输入的密码错误,请重新输入!</font></h1>
<h1 align="center">欢迎光临我的拍卖行</h1>
<table width="75%" border="0" align="center" bgcolor="#FFFFFF">
  <tr> 
    <td align="center"> <img src="images/logo.gif" width="250" height="70"> 
    <td> 
  </tr>
  <tr> 
    <td align="center"> 
      <form name="frmLogin" method="post" action="check_user.php">
        用户名: 
        <input type="text" name="user_name" value=<?php echo $_SESSION["user_name"]; ?>>
        密码: 
        <input type="password" name="password">
        <input type="submit" name="cmdLogin" value="登录" onclick="return CheckValid();">
      </form>
    </td>
  </tr>
  <tr> 
    <td align="center"><font color="#000099">如果您是首次登录本拍卖行,系统将自动注册您的信息</font> </td>
  </tr>
</table>
</body>
</html>

注意都有一个获得焦点的函数,这个自己可以直接拿来用啦。

posted on 2015-04-05 10:46  虽然如此  阅读(237)  评论(0编辑  收藏  举报