收到通知:未定义变量:_SESSION,将本地站点上传到虚拟主机后

我很快就要完成我的网站项目,并且遇到了一些麻烦。 今天,我买了一个虚拟主机服务器和一个域名。 一切都可以在我的本地主机上的xampp上完美运行,但是在服务器上,会话似乎无法正常工作。 我不知道为什么。 它向我显示错误:注意:未定义的变量:_SESSION

尽管,该会话确实在其中存储了一些内容,因为当我尝试进入登录页面时,它将重定向我回到我的主页。这样做是正确的,因为在登录页面的开头有一个if可以做到这一点。 当我转到logout.php时,它还会破坏会话的内容,而在以前不能不破坏它的情况下,让我输入login.php。

我将网站的php版本更新为7.3,到目前为止还没有运气。 我完全迷路了。我以为这个问题会有很多答案,但是我在网上找到的答案都没有帮助我。

这是登录页面的代码:

<?php
// Initialize the session
session_start();

if(!empty($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: index.php");
exit;
}

// Include config file
require_once "config.php";

// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Check if username is empty
if(empty(trim($_POST["username"]))){
    $username_err = "Please enter username.";
} else{
    $username = trim($_POST["username"]);
}

// Check if password is empty
if(empty(trim($_POST["password"]))){
    $password_err = "Please enter your password.";
} else{
    $password = trim($_POST["password"]);
}

// Validate credentials
if(empty($username_err) && empty($password_err)){
    // Prepare a select statement
    $sql = "SELECT id,username,password FROM accounts WHERE username = ?";

    if($stmt = mysqli_prepare($link,$sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt,"s",$param_username);

        // Set parameters
        $param_username = $username;

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Store result
            mysqli_stmt_store_result($stmt);

            // Check if username exists,if yes then verify password
            if(mysqli_stmt_num_rows($stmt) == 1){                    
                // Bind result variables
                mysqli_stmt_bind_result($stmt,$id,$username,$hashed_password);
                if(mysqli_stmt_fetch($stmt)){
                    if(password_verify($password,$hashed_password)){
                        // Password is correct,so start a new session
                        session_start();

                        // Store data in session variables
                        $_SESSION["loggedin"] = true;
                        $_SESSION["id"] = $id;
                        $_SESSION["username"] = $username;                            

                        // Redirect user to welcome page
                        header("location: index.php");
                    } else{
                        // Display an error message if password is not valid
                        $password_err = "The password you entered was not valid.";
                    }
                }
            } else{
                // Display an error message if username doesn't exist
                $username_err = "No account found with that username.";
            }
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

// Close connection
mysqli_close($link);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<center>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
    body{ font: 14px sans-serif; }
    .wrapper{ width: 350px; padding: 20px; }
</style>
</head>
<body>
<div class="wrapper">
    <h2>Login</h2>
    <p>Please fill in your credentials to login.</p>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
            <label>username</label>
            <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
            <span class="help-block"><?php echo $username_err; ?></span>
        </div>    
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <label>Password</label>
            <input type="password" name="password" class="form-control">
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Login">
        </div>
        <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
    </form>
</div>    
</body>
</html>

无需多说,但会话已在页面中初始化。 我只有一页,它是index.php。 每次使用会话时都会初始化该会话-register.php,login.php,index.php。 如果用户登录,导航栏中也有一个条件可以显示特殊的导航选项。

 if(array_key_exists('loggedin',$_SESSION) && $_SESSION['loggedin'] == 1){
 echo "<script> document.getElementById('log').hide(); </script>";
 echo "<li id='out'><a href='logout.php'>Logout</a></li>";
 echo "<li id='cata' class='bg'><a href='catalog.php'><p class='colorinio thic'>Catalog (Wish Premium)</p></a></li>";
 }
 else{
 echo "<li id='log'><a href='login.php'>Login</a></li>";
 echo "<script> document.getElementById('out').hide(); </script>";
 echo "<script>document.getElementById('cata').hide(); </script>";
 }

尽管会话中存储了一些内容,但这仍然行不通。 注册页面功能很好,站点与sql server之间的连接工作正常。 我可以插入用户,它会告诉我登录页面中的登录是否错误。

可能是什么问题?

l464547980 回答:收到通知:未定义变量:_SESSION,将本地站点上传到虚拟主机后

我本可以删除它,但是我想对自己回答,并作为php未来初学者的解决方案,而不仅仅是删除它,从而完全浪费时间。 解决方案是我放

  

session_start();

启动HTML代码后。 当我放置session_start();在文档的开头,一切正常。

谢谢读者的阅读,谢谢...。

本文链接:https://www.f2er.com/3150019.html

大家都在问