doudun3040 2019-05-03 17:11
浏览 127
已采纳

processForm.php为Session Id提供了一个零值

I want to store data (id, profile_image, caption) from another table (I just download the code for uploading images) The problem is when I am about to save the data, processForm.php always give me a zero value for Session ID which is my current ID is "1". I'm a newbie here.

(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)

I try to echo my current id(1) in this form and it is fine. but in saving id in XAMPP php my admin gives me ZERO. processForm gives me zero value for my 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");
    }


    }
  • 写回答

1条回答 默认 最新

  • dongmeng1875 2019-05-05 20:18
    关注

    you can add the output of the $_SESSION variable with a var_dump(); After the loggin and another one in the same moment of the saved of the photo in mysql , and compare??? I think that your (processForm.php) is missing at the beginning too ... session_start ();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒