drv16759 2016-09-10 19:33
浏览 29
已采纳

用户通过PHP登录表单登录后数据无法正确显示

The code below is of my user login form. But, after the username and password matches and the page is being redirected, it shows error 'Undefined index in username in [path]'.

<?php
session_start();

require_once "dbreg.php";
$errormsg = array();
$errorcount = 0;
if (!empty($_POST)) {
  if (empty($_POST['username'])) {
    $errormsg[] = "Enter valid username";
    $errorcount++;
  }
  if(empty($_POST['password'])) {
    $errormsg[] = "Please enter password";
    $errorcount++;
}
if(!empty($_POST['username'])) {
$userquery = mysql_query("SELECT * FROM regform WHERE username='".$_POST['username']."'");
$useroutput = mysql_fetch_assoc($userquery);

if (empty($useroutput)) {
  $errormsg[] = "Invalid username or password";
  $errorcount++;
}
else {
  $queryoutput = mysql_query("SELECT * FROM regform WHERE username = '".$_POST['username']."' AND userpass = '".$_POST['password']."'");
  $newoutput = mysql_fetch_assoc($queryoutput);
  if (empty($newoutput)) {
    $errormsg[] = "Please enter valid login and password";
    $errorcount++;
  }
  else {
    $_SESSION['uid'] = $newoutput['id'];
    header("Location: http://localhost/classwork2/userprofile.php");
  }
}
}
}

 ?>
    <!DOCTYPE html>
    <html>

    <head>
        <meta charset="utf-8">
        <title>Login form</title>
    </head>

    <body>
      <h1>Login</h1>
      <?php
      if (!empty($errormsg)) {
        for ($i=0; $i < count($errormsg) ; $i++) {
          # code...
          ?>
          <div style="color:red"><?php echo $errormsg[$i]; ?></div>
          <?php
        }
      }
       ?>
        <table border="1">
            <form name="lognow" id="lognow" action="reglogin.php" method="post" enctype="multipart/form-data">
                <tr>
                    <td>
                        <label>Username</label>
                    </td>
                    <td>
                        <input type="text" name="username" id="username">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Password</label>
                    </td>
                    <td>
                        <input type="text" name="password" id="password">
                    </td>
                </tr>
                <tr>
                  <td>
                    <input type="submit" value="Login">
                  </td>
                </tr>
            </form>
        </table>
        <h3>Or</h3>
        <h2>
          <a href="reg1.php">Click Here</a> to Register.
        </h2>
    </body>

    </html>

And below is the code to the userprofile.php page

<?php
session_start();
require_once "dbreg.php";

$sql = "SELECT firstname, lastname FROM regform WHERE username = '" . $_SESSION['username'] . "'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

echo "Hello, " . $row['firstname'] . $row['lastname'] ;

?>

Will be kind if anyone can guide me what am i doing wrong. Also if i keep the above code inside an if(!empty($_POST['username'])) the page displays blank.

  • 写回答

1条回答 默认 最新

  • donglu9896 2016-09-10 19:52
    关注

    The problem is, you never did/set $_SESSION['username'] = "YOUR_USERNAME" after successful login, you just did $_SESSION['uid'] = $newoutput['id']; and redirected the user to userprofile.php page.

    So the solution is, construct your query based on user's id, rather than user's username. In userprofile.php page, change the query like this:

    $sql = "SELECT firstname, lastname FROM regform WHERE id = '" . $_SESSION['uid'] . "'";
    
    // your code
    

    Sidenote: mysql_* functions are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分