首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php登录代码问题

问php登录代码问题
EN

Stack Overflow用户
提问于 2009-01-09 16:53:05
回答 7查看 883关注 0票数 0

我使用这个代码进行登录,但是当我输入用户名和密码时,它只会加载页面,登录页面会再次显示。这一切为什么要发生?

代码语言:javascript
复制
<?php
    session_start();
    if(!isset($_POST['username']) || !isset($_POST['password']) || empty($_POST['username']) || empty($_POST['password']))
    {
?>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Welcome to yachts database &nbsp;&nbsp;&nbsp;</title>
        <script type="text/javascript">
        function validate()
        {
            var username=document.getElementById("username").value;
            var password=document.getElementById("password").value;
            if(username.length==0)
            {
                alert("Please enter your user name");
                document.getElementById("username").focus();
                document.getElementById("username").select();
                return false;
            }
            if(password.length==0)
            {
                alert("Please enter your password");
                document.getElementById("password").focus();
                document.getElementById("password").select();
                return false;
            }
            submitOK="false";
        }
        </script>
        <?php
            include("styles.txt");
        ?>
        </head>
        <body onLoad="self.focus();document.login.username.focus()">
        <br><br><br><br><br><br><br>
        <center><font size=5 color=blue><b>University of ....... </b></font></center>
        <center><font size=5 color=blue><b>Yachts Database Project</b></font></center>
        <form name="login" method="post" action="<?php $_SERVER['PHP_SELF'];?>" onSubmit="return validate()">
        <table width="350" border="1" align="center" cellpadding=0 cellspacing=0  bgcolor=#ffffff bordercolor=#ffffff>
        <tr>
            <th colspan=2 height=30 bgcolor=#050E8C><font size=4 color=#ffffff>Please, enter user name and password</font></th>
        </tr>
        <tr>
            <th bgcolor=#E0E0FF><font size=4 color=#2020ff>User name</font></th>
            <td bgcolor=#E0E0FF align=center><input type="text" name="username" id="username" size="30"></td>
        </tr>
        <tr>
            <th bgcolor=#E0E0FF><font size=4 color=#2020ff>Password</font></th>
            <td bgcolor=#E0E0FF align=center><input type="password" name=" password" id="password" size="30"></td>
        </tr>
        <tr>
            <td bgcolor=#E0E0FF colspan=2 align=right><input type="submit" name="login" value="&nbsp;&nbsp;Login&nbsp;&nbsp;">&nbsp;&nbsp;<input type="reset" name="reset" value="&nbsp;&nbsp;Reset&nbsp;&nbsp;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=register.php class=links>Register</a>&nbsp;&nbsp;&nbsp;&nbsp;</td>
        </tr>
        </table>
        </form>
        </body>
        </html>
<?php
    }
    else
    {
        $connect= mysql_connect("localhost","root") or die ("Sorry, Can not connect to database");
        mysql_select_db("login") or die (mysql_error());
        $username1=$_POST['username'];
        $password1=$_POST['password'];
        if($username1 && $password1)
        {
            $query="SELECT * FROM users WHERE username='$username1' AND password='$password1'";
            $result=mysql_query($query,$connect) or die(mysql_error());
            $rowcount=mysql_num_rows($result);
        }
        if($rowcount)
        {
            //$_SESSION['username']=$rowcount['username'];
            $user1=$username1;
            $pass1=$password1;
            session_register("user1");
            session_register("pass1");
            header("location: main.php");
        }
        else
        {
?>
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Welcome to yachts database &nbsp;&nbsp;&nbsp;</title>
            <script type="text/javascript">
            function validate()
            {
                var username=document.getElementById("username").value;
                var password=document.getElementById("password").value;
                if(username.length==0)
                {
                    alert("Please enter your user name");
                    document.getElementById("username").focus();
                    document.getElementById("username").select();
                    return false;
                }
                if(password.length==0)
                {
                    alert("Please enter your password");
                    document.getElementById("password").focus();
                    document.getElementById("password").select();
                    return false;
                }
                submitOK="false";
            }
            </script>
            <?php
                include("styles.txt");
            ?>
            </head>
            <body  onLoad="self.focus();document.login.username.focus()">
            <br><br><br><br><br><br><br>
            <center><font size=5 color=blue><b>University of ....... </b></font></center>
            <center><font size=5 color=blue><b>Yachts Database Project</b></font></center>
            <form name="login"  method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" onSubmit="return validate()">
            <table width="350" border="1" align="center" cellpadding=0 cellspacing=0  bgcolor=#ffffff bordercolor=#ffffff>
            <tr>
                <th colspan=2 height=30 bgcolor=#050E8C><font size=4 color=#ffffff>Please, enter user name and password</font></th>
            </tr>
            <tr>
                <th bgcolor=#E0E0FF><font size=4 color=#2020ff>User name</font></th>
                <td bgcolor=#E0E0FF align=center><input type="text" name="username" id="username" size="30"></td>
            </tr>
            <tr>
                <th bgcolor=#E0E0FF><font size=4 color=#2020ff>Password</font></th>
                <td bgcolor=#E0E0FF align=center><input type="password" name=" password" id="password" size="30"></td>
            </tr>
            <tr>
                <td bgcolor=#E0E0FF colspan=2 height=30 align=right><input type="submit" name="login" value="&nbsp;&nbsp;Login&nbsp;&nbsp;">&nbsp;&nbsp;<input type="reset" name="reset" value="&nbsp;&nbsp;Reset&nbsp;&nbsp;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=register.php class=links>Register</a>&nbsp;&nbsp;&nbsp;&nbsp;</td>
            </tr>
            <tr>
                <td bgcolor=#E0E0FF colspan=2 height=30 align=center><font size=3 color=#ff0000>User name or Password wrong!</font></td>
            </tr>
            </table>
            </form>
            </body>
            </html>
            <?php
        }
    }
?>
EN

回答 7

Stack Overflow用户

发布于 2009-01-09 17:21:51

关于这段代码,有几件事你应该修改一下。

  1. 您不需要执行!isset()和empty():just empty()
  2. 您不应该复制登录页面的HTML -要么将其放入变量中,要么将其include()并使用PHP将错误消息放入其中。
  3. 您使用的是过时的HTML,如<center>和<font> -在javascript中用CSS
  4. 替换它们,submitOK="false";应该是return true;
  5. name=" password"应该是name="password" (属性中的空格应该被修剪掉,但是我不想依赖它。
  6. 你正在把原始输入放到mysql查询中,这使你自己容易受到sql注入的影响。使用mysql_escape_string()
  7. Don't使用http://),使用session_register,而不是使用$_SESSION。
  8. 标题位置应该是一个规范的URL (启动http://),尽管我所知道的每个浏览器都接受相对的URL.
  9. Usability -去掉reset按钮。
票数 12
EN

Stack Overflow用户

发布于 2009-01-09 17:33:46

出于兴趣,为该表单生成的HTML是什么样子的?

作为

代码语言:javascript
复制
action="<?php $_SERVER['PHP_SELF'];?>"

应该是

代码语言:javascript
复制
action="<?php echo $_SERVER['PHP_SELF'];?>"

否则,您可能会发现生成的html是action="“,在这种情况下,它可能不会像您预期的那样处理表单

票数 2
EN

Stack Overflow用户

发布于 2009-01-09 17:15:47

你有大量的代码来实现你想要实现的目标。在不显得居高临下的情况下,我建议回到绘图板,谷歌的'php用户认证‘或类似的东西,看看是否有一些简单的例子,你可以效仿。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/428836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档