不推荐使用:不推荐使用函数session_register(),并且无法修改标头信息

Tin*_*ina 3 php login

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

它有4个主要组件索引 - 用户名和传递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
Run Code Online (Sandbox Code Playgroud)

Pee*_*Haa 12

而不是做:

session_register("myusername");
session_register("mypassword"); 
Run Code Online (Sandbox Code Playgroud)

你可以简单地做:

session_start();
$_SESSION['username'] = 'something';
$_SESSION['password'] = 'something';
Run Code Online (Sandbox Code Playgroud)

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

session_start();
if(!isset($_SESSION['username'])){
    // not logged in
}
Run Code Online (Sandbox Code Playgroud)

请注意,我的session_start()功能正好在我的检查/初始化之上.在您的代码中,您可能希望将其添加到脚本的顶部以防止"已经由PHP发送的标头"消息.

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

关于你的代码的最后一件事.看起来你没有正确地对密码进行哈希处理,这被认为是不好的做法.如果攻击者掌握了您的数据库,您可以向数据库中的人员做一些解释(例如,您必须告诉他们攻击者获得了所有密码).

  • @mario这不是谎言,而是教育.它将在soem点弃用.对于**新**代码,它**开始切换更好.确保您的代码在将来的版本中继续工作有什么问题? (3认同)
  • 如果你想讨论这个问题,请随时来[聊天](http://chat.stackoverflow.com/rooms/11/php)而不是污染评论,我将非常乐意告诉你更多.@mario (2认同)