douxie9347 2014-07-03 13:32
浏览 45
已采纳

undefined变量:登录时的$ username [重复]

I have error when a user login

whats wrong?

in first time tell me welcome but after refresh page tell me error:

Notice: Undefined variable : username in index.php on line 175

<?php
$fn=2;
if(isset($_POST["smbit"])){
$fn=1;
$username = $_POST['username'];
$password = md5($_POST['password']);
$res=mysql_query("SELECT userid,isadmin FROM users WHERE user='$_POST[username]' and pass='$_POST[password]'",$cn);
   if($row=mysql_fetch_object($res)){
    $fn=0;
    mysql_query("UPDATE session SET islogin=1,isadmin=$row->isadmin,uid='$row->userid' where sid='$sid'");
}
}
if($islogin==1) $fn=0;
if($fn==1) echo "<b>Login Not Found!</b>";  
if($fn==1 || $fn==2){
?>
    <p></p>
    <form action="index.php" method="post">
    <i>Username:</i><input type="text" name="username"/>
    <i>Password:</i><input type="password" name="password">
    <input class="buttom" type="submit" name="smbit" value="Login">
    </form>
    <a href="register.php">Not Register?</a>
    <p></p>
<?php
}
if($fn==0)   echo "<h3 align=center>Welcome $username</h3><h4><a href=logout.php>Log out</a> </h4>";
?>
</div>
  • 写回答

1条回答 默认 最新

  • doupu1957 2014-07-03 13:38
    关注

    After refresh if(isset($_POST["smbit"])) returns false, $fn is equal to 2.

    Then, if condition if($islogin==1) is true (probably is true), $fn is set to 0.

    And in the last row, $fn == 0, but $username is undefined (because you set this variable in condition testing existence of $_POST['smbit']).

    Possible solution: after succesfully login set $_SESSION['username'] = $_POST['username'], and in this line work with session instead of undefined variable.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大