简体   繁体   English

提交表单后会话丢失-PHP

[英]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. 我已经在用户页面的页面顶部(在useracc-test.php中)启动了会话。 In the upload.php, I don't need to put session start, because it is already link (require) in the useracc-test.php page. 在upload.php中,我不需要将会话开始,因为它已经是useracc-test.php页面中的链接(必需)。 If I do so, put session start in upload.php, it will prompt error, session already started. 如果这样做,则将session start放在upload.php中,它将提示错误,表明session已经启动。 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. 请help.tq。 Any help would be very appreciated. 任何帮助将不胜感激。

Below is the userpage (useracc-test.php) after the user log in. 以下是用户登录后的用户页面(useracc-test.php)。

<?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. 以下是运行上传脚本的upload.php页面。

<?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']); $ 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) 将圆括号放在变量上以返回值与引用,但这仅对return语句重要,即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. isset()将返回true或false,而不是值。 So when you end-up to bind_param(), you bind with true/false vs the values You should better to this 因此,当您最终使用bind_param()时,请使用true / false与值进行绑定,您应该对此做得更好

$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. 最后一个“在upload.php中,我不需要将会话开始,因为它已经是useracc-test.php页面中的链接(要求)。”:仅当您不直接调用该脚本时,才是正确的。

For ml2_1jzsinglecam Here is a link to the code I used to test 对于ml2_1jzsinglecam,这是我用来测试的代码链接

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). 对于那些100%不使用像我这样的cookie的用户,当会话丢失时,解决方案是重新声明变量(这大部分是某些脚本的一部分)。 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. 我已经在所有终端和远程终端上完全禁用了cookie,无论有无cookie,它都能100%正常运行。 Last but no least, you must define a session id, and have start session above the page. 最后但并非最不重要的一点是,您必须定义一个会话ID,并在页面上方具有开始会话。 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). (此方法仅适用于不使用cookie的用户-两种方法都可以使用)。 Cheerio. Cheerio。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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