gpt4 book ai didi

php - 如何让数组和 session 变量在浏览器刷新后恢复为空白?

转载 作者:行者123 更新时间:2023-11-28 21:03:48 25 4
gpt4 key购买 nike

我有一个问题,每次刷新浏览器时,我希望包含 $_SESSION 变量的数组恢复为空白。现在假设我上传了 2 个文件,然后刷新浏览器,当我上传另一个文件时,它会显示之前上传的文件的名称,而实际上不应该。如果刷新浏览器,如何使数组和 session 变量恢复为空白?

下面是代码:

  function stopImageUpload(success){

var imageNameArray = new Array();
// WHEN PAGE IS REFRESH, ARRAY SHOULD GO BACK TO BEING BLANK

imageNameArray = <?php echo json_encode(isset($_FILES ['fileImage']['name']) ? $_FILES ['fileImage']['name'] : null); ?>;
//RETRIEVES THE SESSION VARIABLE FROM THE PHP SCRIPT OF THE FILE NAMES WHICH HAVE BEEN UPLOADED

var result = '';



if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';

for(var i=0;i<imageNameArray.length;i++) //LOOP THROUGH ALL UPLOADED FILE NAMES
{
$('.listImage').append(imageNameArray[i]+ '<br/>');//APPEND FILE NAME

}

}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}

return true;

}

下面是 php 脚本,它通过上面的 javascript 函数上传位于另一个页面上的文件:

<?php

session_start();

$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";


if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {

$fileName = $_FILES ['fileImage'] ['name'];

$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );


$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;

if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;


}
}

}

?>

<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>

最佳答案

提交表单并且 PHP 完成工作后,您需要执行 303 重定向到您希望显示的页面。然后,当用户刷新页面时,您将不会提交第二个表单。

例如:

header('Location: ' . $_SERVER['PHP_SELF'], true, 303);

关于php - 如何让数组和 session 变量在浏览器刷新后恢复为空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10355049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com