簡體   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