processForm.php 为 Session Id 提供零值

processForm.php gives me a Zero value for Session Id

我想存储来自另一个 table 的数据(id,profile_image,标题) (我只是下载上传图片的代码) 问题是当我要保存数据时,processForm.php 总是给我 Session ID 的零值,即我当前的 ID 是“1”。 我是这里的新手。

(login.php)

    <?php
    // Initialize the session
    session_start();
    // Check if the user is already logged in, if yes then redirect him to 
    welcome page
    if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
    header("location: welcome.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 users 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: welcome.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">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="icon" href="logo2.png" type="image">
    <head>
    <title>Fox - Log In | Sign Up</title>
    <meta charset="windows-1252">
    </head>
    <body>
    <div class="header" id="myHeader" >
    <img src="logo.png" alt="Fox Logo" width="5%" height="20%">
    <div class="tooltip">
    <img src="text.png" alt="Fox text" width="50%" height="15%" usemap="#foxlogo">
    <span class="tooltiptext">Go To Fox Home</span>
    </div>
    <map name="foxlogo">
    <area shape="rect" coords="0,0,133,126" href="login.php">
    </map>
    </div>
    <div class="container">
    <img class="img" src="bg2.jpg">
    </div>

    <div class="signup"><br><br><br><br><br>
    <h1 style="text-align:center; font-size:12;">Log In</h1><br>
    <form style="margin-left:25px; margin-top: -20px;" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
    <p style="font-size: 14px; color: white;margin-left: 0px;width: 980px;">Username</p>
    <input placeholder="Enter Username" type="text" name="username" class="form-control" value="<?php echo $username; ?>">
    <br><span class="help-block"><?php echo $username_err; ?></span>
        </div>    
    <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <p style="font-size: 14px; color: white;margin-left: 0px;width: 980px;">Password</p>
            <input placeholder="Enter Password" type="password" name="password" class="form-control" >
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group">
           <input style="margin-top:20px;" type="submit" class="button" value="Login">
        </div>
        <p2><a href="register.php" type="link">Sign up now</a></p2>
    </form>
    </div>
    <div> 
    <img class="user" src="user.png">
    </div>
    <a type="link2" style="margin:30px; text-decoration: none;" href="#">Terms & Policies</a>
    <a type="link2" style="text-decoration: none;" href="#">Help</a>
    </div>
    </body>
    </html>

(welcome.php)

 <?php
 require_once "config.php";

 // Initialize the session
 session_start();

 // Check if the user is logged in, if not then redirect him to login page
 if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){

header("location: login.php");
exit;
 }

 ?>

 <!DOCTYPE html>
 <html lang="en">
 <link rel="stylesheet" type="text/css" href="home.css">
 <link rel="icon" href="logo2.png" type="image">
 <head>
<meta charset="UTF-8">
<title>Fox | Home</title>
<style type="text/css">
body{ font: 14px sans-serif; text-align: center; }
</style>
</head>

 <body>
<br>
<a style="margin-left:90%;position: relative;" href="logout.php" name="signout" class="btn btn-danger">Sign Out</a>
<div style="margin-right:96%"class="tooltip">
<img style="margin-right: 96%;margin-top:0%;" src="logo.png" alt="Fox Logo" width="100%" usemap="#foxlogo">
<span class="tooltiptext">Go To Fox Home</span>
</div>

<map name="foxlogo">
<area shape="rect" coords="0,0,133,126" href="welcome.php">
</map>

<hr>
<div class="profile">
<div class="page-header">
<h1><b><?php echo htmlspecialchars($_SESSION["username"]); ?></b></h1>

 <div class="container">
 <img name="profile" class="image" src="images/placeholder.png" id="profileDisplay" style="display: block;width: 45%;margin: 10px auto;border-radius:50%;"><br>
<input type="file" name="profileImage" id="profileImage" style="display: none;">
 <div class="overlay">
 <div class="text"><br>
 <a href="updateprofile.php" name="update" class="btn btn-danger">Update Profile</a><br><br><br>
 <a href="#" name="View" class="btn btn-danger">View profile</a>
 </div>
 </div>
 <?php
 $con = mysqli_connect("localhost","root","","demo");
 $q = mysqli_query($con,"SELECT * FROM users WHERE id ='{$_SESSION["id"]}'");
 while($row = mysqli_fetch_assoc($q)){
 echo $row['created_at'];
 }
 ?>
 </div>
 <p>
 <a href="reset-password.php" class="btn btn-warning">Reset Your Password</a><br><br><br>

 </p>
 </div>
 </body>
 </html>

(config.php)

    <?php
    /* Database credentials. Assuming you are running MySQL
    server with default setting (user 'root' with no password) */
    define('DB_SERVER', 'localhost');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', '');
    define('DB_NAME', 'demo');

    /* Attempt to connect to MySQL database */
    $link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

    // Check connection
    if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
    }
    ?>

(updateprofile.php)

我尝试以这种形式回应我当前的 id(1),没问题。但是在 XAMPP php 中保存 id 时,我的管理员给了我零。 processForm 为我的 SESSION ID

提供了零值

picture1:I try to echo my ID and its fine

Picture2:After I upload a picture. it gives me a zero value for ID

    <?php
    require_once "config.php";
    include 'processForm.php';

    // Initialize the session
    session_start();

    // Check if the user is logged in, if not then redirect him to login page
    if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){

    header("location: login.php");
    exit;
    }

    ?>

    <!DOCTYPE html>
    <html lang="en">
    <link rel="stylesheet" type="text/css" href="home.css">
    <link rel="icon" href="logo2.png" type="image">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <head>
    <meta charset="UTF-8">
    <title>Fox | Home</title>
    <style type="text/css">
    body, html{ font: 14px sans-serif; text-align: center;height: 100%; width: 100%; }
    </style>

    </head>
    <body>
    <div style="margin-left:90%"class="tooltip">
    <span class="tooltiptext">Go To Fox Home</span>
    </div>
    <map name="foxlogo">
    <area shape="rect" coords="0,0,133,126" href="welcome.php">
    </map>
    </div>
    <img style="margin-right: 90%;  margin-top:2%;" src="logo.png" alt="Fox Logo" width="5%" usemap="#foxlogo">
    <a style="margin-left:90%; margin-top:-5%; position: relative;" href="logout.php" name="signout" class="btn btn-danger">Sign Out</a>
    <hr>
    <div style="margin-left: 35%"class="container">
<div class="row">
    <div class="col-4 offset-m-d4 form-div">
    <form  enctype="multipart/form-data" action="index.php" method="post" >
    <h3 class="text-center">Upload Profile</h3>
    <?php //echo htmlspecialchars($_SESSION["username"]); 
    echo " My id is {$_SESSION["id"]}"; ?>
    <div class="form-group text-center">
    <img src="images/placeholder.png" id="profileDisplay" onclick="triggerClick()" style="display: block;width: 60%;margin: 10px auto;border-radius:50%;">
    <input type="file" name="profileImage" onchange="displayImage(this)" id="profileImage" style="display: none;">
    </div>
    <div clas="form-group">
    <label for="caption">Caption</label>
    <textarea name="caption" id="caption" class="form-control"></textarea>
    </div>
    <div class="form-group">
    <br>
    <button type="submit" name="save-user" class="btn btn-primary btn-block">Upload</button> 
    </div>
    </form>
    </div>
</div>
    </div>
    <br><br><br><br>
    <script src="scripts.js"></script>
    </body>
    </body>
    </html>

(processForm.php)

    <?php
    require_once "config.php";
    //connect db
    $conn = mysqli_connect('localhost','root','','demo');

    if(isset($_POST['save-user'])){

    $caption = $_POST['caption'];
    $profileImageName =time() . '_' .  $_FILES['profileImage']['name'];
    $target = 'images/' . $profileImageName;

    if(move_uploaded_file($_FILES['profileImage']['tmp_name'], $target)){
//* */


    $sql = "INSERT INTO photos (id,profile_image,caption) VALUES          ('{$_SESSION['id']}','$profileImageName','$caption')";
    if (mysqli_query($conn, $sql)){
    $msg="Image uploaded and saved to database";
$css_class = "alert-success";

    }else{

    $msg="Database error: Failed to save user";
$css_class = "alert-danger";
    }

    //
    $msg="Image uploaded";
$css_class = "alert-success";
header("location: welcome.php");
    }else{
$msg="Failed to upload";
$css_class = "alert-danger";
header("location: updateprofile.php");
    }


    }

您可以在 $_SESSION 变量的输出中添加一个 var_dump(); 登录后和另一个在 mysql 中保存照片的同一时刻,并进行比较???我认为你的 (processForm.php) 开头也不见了...... session_start ();