3

我使用 sql 创建了一个简单的登录系统

它有 4 个主要组件 index - 询问用户名并通过 checklogin - 检查凭据 logsuccess 主页 - 成功登录后的登陆页面

错误生成在帖子末尾给出

Index.php asks for username and pass

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE>Nottingham Uni</TITLE>

      <script type="text/javascript" src="js/mootools-1.2.1-core-yc.js"></script>
      <script type="text/javascript" src="js/process.js"></script>

      <link rel="stylesheet" type="text/css" href="style.css" />
    </HEAD>

     <BODY>

    <center>
    <div id="intro">
      <p>&nbsp;</p>
      <p><img align="absmiddle" src="images/nott-uni-logo.jpg"></p>
    </div>

    <div id="status">

    <fieldset><legend align="center">Authentication</legend>

    <div id="login_response"><!-- spanner --></div>

    <form id="login" name="login" method="post" action="checklogin.php">
    <table align="center" width="300" border="0">
    <tr>
    <td width="80">Username</td><td><input id="name" type="text" name="myusername"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="mypassword"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input id="submit" type="submit" name="submit" value="Login">
    </tr>
    </table>
    </form>
    </fieldset>

    </div>
    </center>
     </BODY>
    </HTML>


checklogin.php checks for the credentials

    <?php

    $link = mysql_connect('www.xxxxx.com', 'xxxxxx', 'xxxxxx');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("brainoidultrafb", $link);

    // username and password sent from form 
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword']; 

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sql="SELECT * FROM logintbl WHERE stu_email='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){

    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword"); 
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>


If its success it goes to homepage.php

logsuccess.php is below

    <?php
    session_start();
    if(!session_is_registered(myusername)){
    header("location:homepage.php");
    }
    ?>
    <html>
    <body>
    Login Successful
    </body>
    </html>


these codes are give in the following errors

    Deprecated: Function session_register() is deprecated in /home/content/58/9508458/html/pabrowser/checklogin.php on line 29

    Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/content/58/9508458/html/pabrowser/checklogin.php:29) in /home/content/58/9508458/html/pabrowser/checklogin.php on line 29

    Deprecated: Function session_register() is deprecated in /home/content/58/9508458/html/pabrowser/checklogin.php on line 30

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/pabrowser/checklogin.php:29) in /home/content/58/9508458/html/pabrowser/checklogin.php on line 31
4

2 回答 2

11

而不是这样做:

session_register("myusername");
session_register("mypassword"); 

你可以简单地做:

session_start();
$_SESSION['username'] = 'something';
$_SESSION['password'] = 'something';

要检查是否设置了用户名,您可以执行以下操作:

session_start();
if(!isset($_SESSION['username'])){
    // not logged in
}

请注意,我的session_start()检查/初始化正上方有该功能。在您的代码中,您可能希望将其添加到脚本的顶部,以防止出现“PHP 已发送的标头”消息

另外,请不要将mysql_*函数用于新代码。它们不再被维护并且社区已经开始了弃用过程。看到红框了吗?相反,您应该了解准备好的语句并使用PDOMySQLi。如果你不能决定,这篇文章将有助于选择。如果你想学习,这里有一个很好的 PDO 教程

关于您的代码的最后一件事。看起来您没有正确散列密码,这被认为是不好的做法。如果攻击者控制了您的数据库,您需要对数据库中的人员进行一些解释(例如,您必须告诉他们攻击者获得了他们的所有密码)。

于 2012-06-24T12:05:40.527 回答
0

@您可以通过在任何不推荐使用的函数之前使用来避免此问题。喜欢@session_register()

于 2013-04-05T06:21:50.683 回答