我如何使用此代码重定向到用户主页

时间:2019-03-02 06:49:57

标签: php

我正在尝试将登录页面重定向到用户主页。我的代码未显示任何错误,但登录页面未重定向到用户主页。迫切期望一个适当的解决方案。提前致谢。下图是注册页面的数据库详细信息。

enter image description here

这是我的索引页代码

在此处输入代码

<tr> 
<td width="200px" height="70px" style="color: gray;"><b>ENTER USERNAME</b></td>
                            <td width="100px" height="70px" style="border-bottom: 1px solid black;color: gray;" ><input type="text" name="username" placeholder="ENTER USERNAME" style="width: 150px;height: 35px;background:none; border: none;"></td>
                        </tr>
                        <tr>
                            <td width="200px" height="70px" style="color: gray;"><b>ENTER PASSWORD</b></td>
                            <td width="200px" height="70px" style="border-bottom: 1px solid black;color: gray;" ><input type="PASSWORD" name="password" placeholder="ENTER PASSWORD"style="width: 150px;height: 35px;background: none; border: none;"></td>

                        </tr>
                        <tr>
                            <td><input type="submit" name="login" value="login"style="width: 150px;height: 35px;  background:none;border-radius: 10px; border-color: gray; padding: 10px  "></td>
                        </tr>
                    </form>

                        <h2>dont have an account signup here</h2>
                        <li><a href="signup-form.php">signup</a></li>





                    </table>

                    <?php
    if(isset($_POST['login']))
                    {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = $db->query("select * from signup where username = '$username' && password = '$password'");
    $count = $query->rowcount();
    $row = $query->fetch();
    if ($count > 1){
    session_start();
    $_SESSION['id'] = $row['user_id'];
     header('location:user-home.php'); 
    }else{
     header('location:index.php'); 
    }
    }
    ?>

2 个答案:

答案 0 :(得分:0)

问题在于以下情况

if($count > 1){
}

如果数据库中存在带有相关用户名和密码的条目,则$query->rowcount();将返回所选行号(应为one(1))。 条件应该是

if($count == 1){
  //home-page
}
else{
 //index page
}

答案 1 :(得分:0)

请从if ($count > 1){}更改为if ($count > 0){}

您的代码如下:

if ($count > 0){
    session_start();
    $_SESSION['id'] = $row['user_id'];
     header('location:user-home.php'); 
    }else{
     header('location:index.php'); 
    }