繁体   English   中英

提交表单后会话丢失-PHP

[英]session lost after form submitted - php

一切正常,除了我提交数据后,会话数据消失。 我已经在用户页面的页面顶部(在useracc-test.php中)启动了会话。 在upload.php中,我不需要将会话开始,因为它已经是useracc-test.php页面中的链接(必需)。 如果这样做,则将session start放在upload.php中,它将提示错误,表明session已经启动。 我浏览了互联网上的无处不在,我尝试的所有内容都对我不起作用。我意识到不同的问题需要不同的方法。 有些人有相同的问题,但是解决每个问题的方法与我在网络上阅读各种情况的方法不同。 我被这几天困住了。 真是在撞我的头。 请help.tq。 任何帮助将不胜感激。

以下是用户登录后的用户页面(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>

当我按提交时,下面的这一部分消失了

 $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']);

以下是运行上传脚本的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']);

我不确定为什么要这样编写代码。 将圆括号放在变量上以返回值与引用,但这仅对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

其次,我想您是说该值从数据库中消失了吗? 因为你这样做

$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);

但是您上面的代码执行以下操作

$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()将返回true或false,而不是值。 因此,当您最终使用bind_param()时,请使用true / false与值进行绑定,您应该对此做得更好

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

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

对于ml2_1jzsinglecam,这是我用来测试的代码链接

我已经解决了我的问题。 对于那些100%不使用像我这样的cookie的用户,当会话丢失时,解决方案是重新声明变量(这大部分是某些脚本的一部分)。 在同一页面上重新声明。 实际上,它与标题和出口或白线无关,我已经研究了一个多月。 我已经在所有终端和远程终端上完全禁用了cookie,无论有无cookie,它都能100%正常运行。 最后但并非最不重要的一点是,您必须定义一个会话ID,并在页面上方具有开始会话。 就这样。 与标题,出口或白线无关。 (此方法仅适用于不使用cookie的用户-两种方法都可以使用)。 Cheerio。

暂无
暂无

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

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