简体   繁体   中英

session lost after form submitted - php

Everything works fine, except after I submit data, session data disappears. I already have session start at the top of the page (in useracc-test.php) which is the userpage. In the upload.php, I don't need to put session start, because it is already link (require) in the useracc-test.php page. If I do so, put session start in upload.php, it will prompt error, session already started. I browsed everywhere on the Internet, Everything I tried didn't work for me.I realise different problems require different approaches. Some people have the same problem, but the method to solve each problem is not the same as I read all through various cases on the net. I'm stuck with this several days. It's really banging my head. pls help.tq. Any help would be very appreciated.

Below is the userpage (useracc-test.php) after the user log in.

<?php
//useracc-test.php
//start session
session_start();
//error_reporting(E_ALL);
//ini_set("display_errors",1);
//run the db connection
require 'connect-test.php';
//run the upload images etc script
require 'upload.php';

//if true, execute below
if(isset($_POST['username'])){

    //define variable
    $userName = $_POST['username'];

    //fetch data from table users
    $query = "SELECT id, name, username, telno FROM users WHERE username = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param('s', $userName);
    $stmt->execute();
    $res = $stmt->get_result(); 
    $row = $res->fetch_array();

    // place in session to echo in html later
    $_SESSION['id'] = $row['id'];
    $_SESSION['name'] = $row['name'];
    $_SESSION['username'] = $row['username'];
    $_SESSION['telno'] = $row['telno'];

//*******************************************************   

 //create session for id field
  $id=($_SESSION['id']);

  //joined table- fetch all rows based on same id in child table useradvert  
 $query="SELECT ua.* FROM useradvert ua INNER JOIN users u ON  ua.id =u.id
 WHERE ua.id='".$id."'";

    $stmt = $conn->prepare($query); 
    $stmt->execute();
    $res2 = $stmt->get_result(); 
}

?>

<html>
<head>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>
</head>
<body>
<div id="apDiv3">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span class="TabbedPanelsContent">

  <?php
  //display record from table- users (parent table)
    echo $_SESSION['id']."<br/>";
    echo $_SESSION['name']."<br/>";
    echo $_SESSION['username']."<br/>";
    echo $_SESSION['telno']."<br/>";
?>
</p>

<p>&nbsp;</p>
<form name="form2" 
      action="useracc-test.php" method="post" enctype="multipart/form-data">
  <p>&nbsp;</p>
  <table width="500" border="0">
    <tr>
      <td>category</td>
      <td><select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
        <option value="useracc-test.php" selected>Category</option>
        <option value="useracc-test.php">Members</option>
        <option value="useracc-test2-jumpmenu.php">Non-members</option>
      </select></td>
    </tr>
    <tr>
      <td>ID:</td>
      <td><input name="id" type="text" id="id" value="<?php echo $_SESSION['id']; ?>" ></td>
    </tr>
    <tr>
      <td>Name:</td>
      <td><input type="text" name="name2" id="name2"></td>
    </tr>
    <tr>
      <td>Color</td>
      <td><input type="text" name="color2" id="color2"></td>
    </tr>
    <tr>
      <td>Hobby</td>
      <td><input type="text" name="hobby2" id="hobby2"></td>
    </tr>
    <tr>
      <td>Sex</td>
      <td>male
        <input type="radio" name="radiobtn" id="radio" value="male">
        female
        <input type="radio" name="radiobtn" id="radio2" value="female"></td>
    </tr>
    <tr>
      <td>Image</td>
      <td><input type="file" name="image" id="image"></td>
    </tr>
    <tr>
      <td>Image2</td>
      <td><input type="file" name="image2" id="image2"></td>
    </tr>

    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" id="submit" value="submit"></td>
    </tr>
  </table>
  <div align="center"></div>
  <p>&nbsp;</p>
</form>
<p>&nbsp;</p>
<p>


<ul>
  <?php
if (isset($res2)){
while ($row2 = $res2->fetch_array(MYSQLI_ASSOC)){
    echo "<li>".$_SESSION['name2'] = $row2['name2'];
    echo $_SESSION['color2'] = $row2['color2'];
    echo $_SESSION['hobby2'] = $row2['hobby2'];
    echo $_SESSION['radiobtn'] = $row2['radiobtn'];
    echo $_SESSION['kupon'] = $row2['kupon'];
    echo  $_SESSION['image'] = $row2['image'];
    echo $_SESSION['image2'] = $row2['image2'];}
}
?>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>
</body>
</html>

this part below disappears when i press submit

 $id = isset($_POST['id']);
    $name2 = isset($_POST['name2']);
    $color2 = isset($_POST['color2']);
    $hobby2 = isset($_POST['hobby2']);
    $radiobtn = isset($_POST['radiobtn']);
    $image = isset($_FILES['image']);
    $image2 = isset($_FILES['image2']);

below is the upload.php page which runs the uploading script.

<?php
//echo var_dump($_POST);
//echo var_dump($_FILES);
require 'connect-test.php';
if(isset($_POST["submit"])) {
    //define variable 
   $id = $_POST['id'];
$name2 = $_POST['name2'];
$color2 = $_POST['color2'];
$hobby2 = $_POST['hobby2'];
$radiobtn = $_POST['radiobtn'];
$image = $_FILES['image'];
$image2 = $_FILES['image2'];
    //target file for image
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//target file2 for image2
    $target_dir = "uploads/";
$target_file2 = $target_dir . basename($_FILES["image2"]["name"]);
$uploadOk = 1;
$imageFileType2 = pathinfo($target_file2,PATHINFO_EXTENSION);
//script for targetfile -image
    // Check if image or not        
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
//script for targetfile2 -image2
    // Check if image or not        
    $check2 = getimagesize($_FILES["image2"]["tmp_name"]);
    if($check2 !== false) {
        echo "File is an image - " . $check2["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
// Check if file already exists
if (file_exists($target_file2)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["image2"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType2 != "jpg" && $imageFileType2 != "png" && $imageFileType2 != "jpeg"
&& $imageFileType2 != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["image2"]["tmp_name"], $target_file2)) {
        echo "The file ". basename( $_FILES["image2"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
//insert record
    $stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image,image2) VALUES (?,?,?,?,?,?,?)");
    $stmt->bind_param("issssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file,$target_file2);
    $stmt->execute();
}
?>

$id=($_SESSION['id']);

I am not sure why you write your code like this. Putting parentheses around a variable force to return the value vs the reference, but it matters only for the return statement, ie return $var vs return ($var)

<?php
$q=1 ;
$r=$q;
$q=2;
echo $r, "\n"; // prints 1
$t =&$q ;
$q=3;
echo $t, "\n"; // prints 3

Second, I guess you mean that the value disappears from your database? Because you do this

$stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image,image2) VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param("issssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file,$target_file2);

But your code above does the following

$id = isset($_POST['id']);
$name2 = isset($_POST['name2']);
$color2 = isset($_POST['color2']);
$hobby2 = isset($_POST['hobby2']);
$radiobtn = isset($_POST['radiobtn']);
$image = isset($_FILES['image']);
$image2 = isset($_FILES['image2']);

isset() will return true or false, not the value. So when you end-up to bind_param(), you bind with true/false vs the values You should better to this

$id = isset($_POST['id']) ? $_POST['id'] : <a default value> ;
etc...

Last “In the upload.php, I don't need to put session start, because it is already link (require) in the useracc-test.php page.”: this is true only if you are not calling that script directly.

For ml2_1jzsinglecam Here is a link to the code I used to test

I solved my problem already. To those who are 100% not using cookies like me, when the session is lost, the solution is to re-declare the variables(which is mostly parts of certain scripts). re-declare on the same page. It's actually nothing to do with header and exit, or white lines, I have studied this in depth more than a month. I have totally disabled cookies on all my terminals and remote terminals, it works 100% fine with or without cookies. Last but no least, you must define a session id, and have start session above the page. that's all. Nothing to do with headers, exit or white lines. (This method is only suitable for those not using cookies - however is works fine in both). Cheerio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM