如果我取消注释 session_start() 代码,页面不会显示在浏览器上


Page doesnt display on browser if i uncomment the session_start() code

这是包含会话代码的四个页面。 当我运行sign_up.php页面时,出现一个错误,指出无法显示该页面。所以这些会议给了我一个问题。我已经在每个页面上都包含了会话代码,但我相信问题出在header(location:........); 所以请提供任何解决方案。

sign_up.php

<?php 
//session_start();
//if (!isset($_SESSION["user_login"])) {
//   header("Location: sign_up.php");
//} else {
//    $username = $_SESSION["user_login"];
//}

?>
<!----------------------------------------------------------------------------------------------------->

<h1> Sign Up </h1>
    <hr>
    <div class = "user_type"> 
     <form action="sign_up.php" method="POST" enctype="multipart/form-data">
                <input type="radio" value="Student" id="radioOne" name="account" checked/>
                    <label for="radioOne" class="radio" chec>Student   </label>
                <input type="radio" value="Landlord" id="radioTwo" name="account" />
                    <label for="radioTwo" class="radio">Landlord</label>
                <hr/>    
             <div class = "gender_options"> 
                <input type="radio" value="Male" id="male" name="gender" checked/>
                    <label for="male" class="radio" chec>Male</label>
                <input type="radio" value="Female" id="female" name="gender" />
                    <label for="female" class="radio">Female</label>    
            </div> 
             <input type="text" name="name" id="name" placeholder="Full Name" required/> <br/><br/>
             <input type="email" name="email" id="name" placeholder="Email" pattern="[a-z0-9._%+-]+@aston.ac.uk" required/> <br/><br/>
             <input type="text" name="password" id="name" placeholder="Password" required/><br/><br/>
             <input type="text" name="password2" id="name" placeholder="Retype Password" required/><br/><br/>
             By clicking Sign Up, you agree on our <a href="#">terms and condition</a>. <br/><br/>  
             <a href="#" class="button" style="margin-left: 600px; "><input type="submit" name="submit" value="Sign Up"/></a>
     </form>     
    </div>
    <hr>
<!---- log in code--->    
    <?php
    enter code here
    if (isset($_POST["user_login"]) && isset ($_POST["user_pass"])){
    // formatting field via reg replace to ensure email and password only conisists of letters and numbers preg_replace('#[^A-Za-z0-9]#i','', 
    $login_user = $_POST["user_login"];
    $login_password = $_POST["user_pass"];
    // password is encryted in DB (MD5) therefore user inputted password will not match encryted password in DB - we have to assign new var
    $decrypted_password = md5($login_password);
// Query which finds user (if valid) from DB - Achieving authentication via username and password       
$user_query = mysqli_query($connect, "SELECT * FROM users WHERE email = '$login_user' AND password = '$decrypted_password' AND closed = 'no' LIMIT 1"); 
    $check_user = mysqli_num_rows($user_query); // checking to see if there is infact a user which those credentials in the DB
        if ($check_user==1){
        while ($row = mysqli_fetch_array($user_query)){
                $id = $row['user_id'];
            }
enter code here
            // if the user credentials are correct, log the user in:
            $_SESSION["user_login"] = $login_user;
                header( "Location: profile_student.php" ); // refresh page
            exit;
                // if user row does not equal 1 ... 
            //exit; 
        } else {
            echo "<div class='wrong_login'>
                        <p> Email or password is incorrect, please try again. </p>

                     </div>";
        }
}   
    ?>

    <h1> Log In </h1> 
    <hr>
    <div class ="login_form">
                    <form action="sign_up.php" method="POST">
                        <input type="text" name="user_login" placeholder="Email"  pattern="[a-z0-9._%+-]+@aston.ac.uk"  required/><br/><br/>
                        <input type="text" name="user_pass" placeholder="Password" required/> <br/><br/>
                        <input type="submit" name="login_submit" value="Log In"/> 
                    </form>
                </div>
      </div>

首页.php

<?php
session_start();
if (!isset($_SESSION["user_login"])) {
   header("Location: profile_student.php");
} else {
      $username = $_SESSION["user_login"];
}
include ("connect.php");
echo "Hello,";
echo"<br/> Would you like to logout? <a href = 'logout.php'>LogOut</a>";
?>

profile_student.php

这是用户登录时的页面,此页面将允许他们访问他们的信息等。

   <?php
    session_start();
    if (!isset($_SESSION["user_login"])) {
       header("Location: sign_up.php");
    } else {
          $username = $_SESSION["user_login"];
    }
    include ("includes/connect.php");
    ?>

注销.php

这是我网站的注销代码

<?php

session_start();
session_destroy();
unset($_SESSION);
session_write_close();
header( "Location: ../index.php" );
die;
?>    
不要在每个

页面中执行session_start,而是创建一个通用.php文件并将此文件包含在所有必需的页面中。另外,您需要确保在会话启动之前没有空格,否则它会抛出标头已发送错误!

你是真的,问题是标题。

正在创造一个无限循环说:你来sign_up?如果$_SESSION['user_login']不存在,请转到sign_up。它一遍又一遍地重复。因为$_SESSION['user_login']不能在你第一次上sign_up时存在。

所以只需这样做:在您的sign_up页面上。

<?php 
session_start();

因此,请删除if / else条件。