在将用户定向到用户帐户页面时,在登录期间使用php中的会话时,页面为空

时间:2018-10-31 11:03:07

标签: php session mysqli phpmyadmin

我在其中有一个登录页面,我已经设置了一些会话,因此,一旦用户登录到定向到用户页面(例如,该页面上的www.sitename / users / index.php)后,就会有个人资料信息和密码&email更改更新等。常规的东西。 我遇到的问题是通过php标头功能重定向后用户登录后,用户个人资料页面部分未显示。

这是我的php登录代码:

<?php 
session_start();
include 'include/db-conn.php';
if (isset($_POST['submit'])) {
    $username = $_POST['user_name'];
    $email = $_POST['user_email'];
    $password = $_POST['user_password'];

    $sql = "SELECT * FROM `all_users` WHERE `user_name`='$username' AND `user_email`='$email'";
    $result = mysqli_query($conn, $sql);
    $user = mysqli_fetch_assoc($result);

    if ($user['status'] == '0') {
        echo "<script>alert('Your Account needs to be verified first , check your email that you got during signing up!')</script>";
        exit();
        header("Location: index.php");
    }
    if ($user['status'] == '2') {
        echo "<script>alert('Your Account Status is 2!')</script>";
        exit();
        header("Location: index.php");
    }
    if ($user['status'] == '3') {
        echo "<script>alert('Your Account Status is 3!!')</script>";
        exit();
        header("Location: index.php");
    }
    if ($user['status'] == '4') {
        exit();
        header('Location: index.php');
    }

    if ($user['user_name'] == $username && $user['user_email'] == $email && password_verify($password, $user['user_password'])) {
        $_SESSION['id'] = $user['id'];
        $_SESSION['userid'] = $user['user_random_id'];
        $_SESSION['username'] = $user['user_name'];
        $_SESSION['email'] = $user['user_email'];
        $_SESSION['user_role_type'] = $user['user_role_type_name'];
        $_SESSION['main_user_type'] = $user['main_user_group'];
        $_SESSION['sub_user_type'] = $user['sub_user_group'];
        $_SESSION['logged_in'] = true;
        echo '<script>location.href=users/index.php;</script>';
        header("Location: users/index.php");
        //ob_enf_fluch();
    } else {
        echo "<script>alert('Please check your credentials. Your user name, email or password are incorrect')</script>";
        header("Location: index.php");
    }
}

?>

此会话会检查用户帐户页面,以便他们只有在登录时才能访问:

<?php 
if (!isset($_SESSION['main_user_group'])) {
    header("Location: index.php");
    exit();
}

?>

这是html代码:

<div class="panel panel-default">
                <div class="panel-heading">Log In</div>
                <div class="panel-body">
                    <form name="userLogin" action="login-script.php" method="POST" onsubmit="return validateForm()">


                        <!---->
                        <div class="form-group">
                            <label for="user_name">Username</label>
                            <input required="required" placeholder="enter here" type="text" name="user_name" class="form-control">
                        </div>
                        <hr>

                        <!---->
                        <div class="form-group">
                            <label for="user_email">Email</label>
                            <input required="required" placeholder="enter here" type="email" name="user_email" class="form-control">
                        </div>
                        <hr>

                        <!---->
                        <div class="form-group">
                            <label for="user_password">Password</label>
                            <input required="required" placeholder="enter here" type="password" name="user_password" class="form-control">
                        </div>
                </div>
                <div class="panel-footer">
                    <div class="form-group">
                        <input placeholder="enter here" type="submit" name="submit" value="submit" class="form-control btn btn-success">
                    </div>
                </div>
                </form>
            </div>

这是用户部分www.somesite.com/users/index页面的内容:

   <?php include '../include/db-conn.php'; ?>
<?php include '../include/header.php'; ?>
<?php include 'session-check.php'; ?>
<!--Section-->
<div class="container-fluid p0 m0 ">
    <ul class="breadcrumb m0 p20">
      <li>Users</li>
      <li class="active">Account</li>
    </ul> 
</div>
<!-- Section-->

<div class="container-fluid">
    <div class="container">
        <div class="row">
            <br>
            <div class="col-md-3">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Links
                    </div>
                    <div class="panel-body p0">
                        <?php include 'common-links.php'; ?>
                    </div>
                </div>
            </div>
            <div class="col-md-9">
                <form action="">
                    <div class="col-md-3">
                        <div class="panel panel-default p0">
                            <div class="panel-heading">Image</div>
                            <div class="panel-body p0">
                                <img class="img-responsive" src="https://s3.amazonaws.com/uifaces/faces/twitter/rem/128.jpg" width="100%" height="100%" alt="">
                                <label for="user_image"><input class="form-control" type="file" name="user_image"></label>
                            </div>
                            <div class="panel-footer">
                                <p>User Role:</p>
                                <p>Username:</p>
                                <p>Joined:</p>
                                <p>User ID:</p>
                                <p>Email Verified:</p>
                            </div>
                        </div>                                                             
                    </div>

                    <div class="col-md-9">
                        <div class="panel panel-default p0">
                            <div class="panel-heading">Profile Info</div>
                            <div class="panel-body">
                                <div class="form-group">
                                    <label for="user_title">Title</label>
                                    <select  class="form-control select" name="user_title" id="">
                                        <option value="non-selected">Non Selected</option>
                                        <option value="Mr">Mr</option>
                                        <option value="Mrs">Mrs</option>
                                        <option value="Miss">Miss</option>
                                        <option value="oters">Others</option>
                                    </select>
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_gender">Gender</label>
                                    <select  class="form-control select" name="user_gender" id="">
                                        <option value="non-selected">Non Selected</option>
                                        <option value="Female">Female</option>
                                        <option value="Male">Male</option>
                                        <option value="Tansgender">Tansgender</option>
                                        <option value="Bi-Sexual">Bi-Sexual</option>
                                        <option value="oters">Others</option>
                                    </select>
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_first_name">First Name</label>
                                    <input required class="form-control" type="text" name="user_first_name" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_middle_name">Middle Name</label>
                                    <input required class="form-control" type="text" name="user_middle_name" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_last_name">Lastname</label>
                                    <input required class="form-control" type="text" name="user_last_name" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_dob">D.O.B</label>
                                    <input required class="form-control"  type="date" name="user_dob" placeholder="enter here">
                                </div>
                                <hr class="">

                                <textarea class="tinymce form-control"  cols="30" rows="10" name="about_user" placeholder="Write something about yourself!"></textarea>
                                <hr>

                                <h3>Address</h3>
                                <div class="form-group">
                                    <label for="user_address_number">Address No</label>
                                    <input required class="form-control" type="text" name="user_address_number" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_address_street">Address Street</label>
                                    <input required class="form-control" type="text" name="user_address_street" placeholder="enter here">
                                </div>
                                <hr>

                                <!--get country from the db-->
                                <div class="form-group">
                                    <label for="user_address_country">Country(select)</label>
                                    <select class="form-control select" name="user_address_country" id="">
                                        <option value="non-selected">None Selected</option>
                                        <option value="GB">United Kingdom</option>
                                    </select>
                                </div>
                                <hr>

                                <!--get state from the db-->
                                <div class="form-group">
                                    <label for="user_address_state">State / County /Province / Region (select)</label>
                                    <select class="form-control select" name="user_address_state" id="">
                                        <option value="non-selected">None Selected</option>
                                        <option value="GB">United Kingdom</option>
                                    </select>
                                </div>
                                <hr>

                                <!--get city from the db-->
                                <div class="form-group">
                                    <label for="user_address_city">City (select)</label>
                                    <select class="form-control select" name="user_address_city" id="">
                                        <option value="non-selected">None Selected</option>
                                        <option value="GB">United Kingdom</option>
                                    </select>
                                </div>
                                <hr>

                                <!--get town from the db-->
                                <div class="form-group">
                                    <label for="user_address_town">Town  (select)</label>
                                    <select class="form-control select" name="user_address_town" id="">
                                        <option value="non-selected">None Selected</option>
                                        <option value="GB">United Kingdom</option>
                                    </select>
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_address_post_code_zip">Post / Zip Code </label>
                                    <input required class="form-control" type="text" name="user_address_post_code_zip" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_phone_number">Phone Number</label>
                                    <input class="form-control" type="tel" name="user_phone_number" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_mobile_number">Mobile Number</label>
                                    <input class="form-control" type="tel" name="user_mobile_number" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <label for="user_email">User Email</label>
                                    <input required class="form-control" type="email" name="user_email" placeholder="enter here">
                                </div>
                                <hr>

                                <div class="form-group">
                                    <input class="form-control btn-success" type="submit" name="save" value="Save">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <br>    
</div>

<?php include '../include/footer.php'; ?>

2 个答案:

答案 0 :(得分:0)

如果此代码不起作用,请尝试提供该代码,请检查您的密码哈希格式是否正确。 注意:,user / index.php必须启动会话“ session_start();”

<?php 
    session_start();
    include 'include/db-conn.php';
    if (isset($_POST['submit'])) {
        $username = $_POST['user_name'];
        $email = $_POST['user_email'];
        $password = md5($_POST['user_password']);
        $sql = "SELECT * FROM `all_users` WHERE `user_name`='$username' AND `user_email`='$email' AND `user_password`='$password'";
        $result = mysqli_query($conn, $sql);
        $user = mysqli_fetch_assoc($result);
        if(!empty($user)){
            if ($user['status'] == '0') {
                echo "<script>alert('Your Account needs to be verified first , check your email that you got during signing up!')</script>";
                exit;
                header("Location: index.php");
            }elseif($user['status'] == '2') {
                echo "<script>alert('Your Account Status is 2!')</script>";
                exit;
                header("Location: index.php");
            }elseif($user['status'] == '3') {
                echo "<script>alert('Your Account Status is 3!!')</script>";
                exit;
                header("Location: index.php");
            }elseif($user['status'] == '4') {
                exit;
                header('Location: index.php');
            }
            $_SESSION['id'] = $user['id'];
            $_SESSION['userid'] = $user['user_random_id'];
            $_SESSION['username'] = $user['user_name'];
            $_SESSION['email'] = $user['user_email'];
            $_SESSION['user_role_type'] = $user['user_role_type_name'];
            $_SESSION['main_user_group'] = $user['main_user_group'];
            $_SESSION['sub_user_type'] = $user['sub_user_group'];
            $_SESSION['logged_in'] = true;
            header("Location: users/index.php");
        }else {
            echo "<script>alert('Please check your credentials. Your user name, email or password are incorrect')</script>";
            header("Location: index.php");
        }
    }
?>

答案 1 :(得分:0)

未显示html部分的原因是html位于session_start()之前;因为我使用php include函数主要通过header.php在整个网站上呈现html。 通过添加session_start()可以解决该问题。在由php include函数呈现的header.php的开头,或者只是确保session_start();在任何给定情况下都在html标签上方。

如果我错了,请纠正我,但这似乎对我有用! 谢谢