dou44364983 2015-12-17 05:49
浏览 11

登录页面无法正常运行

When I open the login page for the first time, even if I use correct username and password, the login page is not being redirected to home page. But on the second login it is properly being redirected to home page. The code of login.php looks like this.

<?php
session_start();
$_SESSION['login_user'] = false;
?>

Rest of login.php is simple html and the login form data is sent to validate.php where validate.php looks like this.

<?php
session_start();
    $username="";
    $password="";
    $usernameErr=""; 
    $passwordErr="";
    $empty=false;
    $_SESSION['login_user'] = false;
    if(isset($_POST['submit']))
    {
        if(empty($_POST['username']))
        {
            $empty=true;
            $usernameErr="Username is empty. Please try again";
        }
        else 
            {
                $username=$_POST['username'];
            }
        if(empty($_POST['password']))
        {
            $empty=true;
            $passwordErr="Password is empty. Please try again";
        }
        else 
            {
                $password=$_POST['password'];
            }
        if(!$empty)
        {
            $servername="myxor.in.mysql";
            $dbusername="myxor_in";
            $dbpassword="srikanth177";
            $dbname="myxor_in";
            $conn=new mysqli($servername, $dbusername, $dbpassword, $dbname);
            if($conn->connect_error) 
            {
                die("Connection failed: " . $conn->connect_error);
            }
            $sql = "SELECT firstname,package,username,password FROM users WHERE username='$username'";
            $result = $conn->query($sql);
            if (($result->num_rows) > 0) 
            {
                $row = $result->fetch_assoc();
                if($username === $row['username']) 
                    {
                        if($password === $row['password'])
                        {
                            $_SESSION['fname'] = $row['firstname'];
                            $_SESSION['pack'] = $row['package'];
                            //$_SESSION['login_user'] = $username;
                            $_SESSION['login_user'] = true;
                            header('Location: http://www.myxor.in/home.php');
                            exit;
                        }
                        else
                        {
                            $passwordErr = "Invalid Password. Please Try Again";
                        }
                    }
            } else
            {
                $usernameErr = "Invalid Username. Please Try Again";
            }
        }
    }
    else echo "submit not set";
    echo $usernameErr;
    echo "<br>";
    echo $passwordErr;
?>

Any suggestions why the login page is not functioning for the first time (Note that when I login second time its being properly redirected to required home.php)

  • 写回答

2条回答 默认 最新

  • douqian4411 2015-12-17 06:22
    关注

    You should print your session variables on the login page. Something like this:

    var_dump($_SESSION);
    

    So that when you enter the login information and are not redirected on the first try, you will see if the values are being set. That's the first step to take. Then you will have a better idea of what you need to change in your files.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真