繁体   English   中英

单击提交按钮时,PHP 会话结束

[英]PHP session ends when submit button is clicked

我想为用户制作个人资料上传图片,以便他们可以在他们的个人资料中上传自己的头像......所以这里的问题是每当我点击上传按钮会话被破坏。

这是表格:

if(isset($_SESSION['profileimgID'])){
echo "<form action='upload.php' method='POST' enctype='multipart/form-data'>
<input type='file' name='file'>             
<button type='submit' name='uploadimgsubmt' class='button1'>upload</button></form>";
}
?>

upload.php 文件的部分代码:

<?php
session_start();
include_once 'includes/dbh.inc.php';

$id = $_SESSION['profileimgID'];
if(isset($_POST['uploadimgsubmt'])){

**code code code**

if($fileError === 0){
if($filesize < 1000000){
$fileNameNew = "profile".$id.".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($filetmpname, $fileDestination);
$sql = "UPDATE profileimg SET STATUS=0 WHERE userid='$id';";
$result = mysqli_query($conn, $sql);    
header("Location: index.php?upload=success");
}
}

**code code code**

如果用户成功登录,则在 loginCheck.php 中的代码:

session_start();
$_SESSION['userID'] = $row['idusers'];
$_SESSION['username'] = $row['uidusers'];                         
$cmpor = $row['idusers'];
$sql = "SELECT * FROM profileimg WHERE id";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
if($row['id'] == $cmpor){   
$_SESSION['profileimgID'] = $row['id'];
}
}
header("Location: ../index.php?login=success");
exit();
}

与问题相关的最后一段代码位于 index.php:

<?php
session_start();
include_once 'includes/dbh.inc.php';
?>
**code code**
<?php
if(isset($_SESSION['profileimgID'])){
echo 'Show this content';
}else{
echo 'Show this content';
}
?>
**code code**

如果我将 'profileimgID' 删除为空(''),一切正常,但 isset 方法不会隐藏显示内容。 如果我保留它,因为它的 isset 方法工作正常,但上传按钮会破坏会话并且用户已注销。

如果用户成功登录,print_r($_SESSION) 会同时生成 index.php 和 upload.php:对于用户 #2

Array ( [userID] => 2 [username] => popa [profileimgID] => 2 ) 

我检查了控制台的请求,当我单击上传按钮时,我收到以下消息:

Form contains a file input, 
but is missing method=POST and 
enctype=multipart/form-data on the form. 
The file will not be sent.

这部分 (isset($_SESSION['profileimgID'])) 以某种方式干扰了这个过程。 当我删除它时,会话被维护并且它可以正常工作,上传也可以工作。

更新:这是我点击上传按钮时得到的: https : //i.stack.imgur.com/So7OD.png

这是我猜对的吗?: https : //i.stack.imgur.com/HcBqz.png

我是 php 新手,所以...抱歉我的错误。

您现在如何维护会话标识符? “会话”依赖于每次交换时从客户端以某种方式发送到主机的“会话 ID”:通常,这是使用 cookie 完成的,但可以使用GET参数完成(例如&sessionid=XXXX )听起来对我来说,这个信息没有被发送:会话实际上并没有被“破坏”,但你找不到它。

解决此问题的最快方法可能是使用浏览器的网络调试功能:查看正在发送的完整数据包,包括 HTML 标头(即 cookie 所在的位置)。 首先,看看“正常”的交流。 然后,查看单击该按钮时发生的情况。 “Cookies”将每次发送,因为它们位于标题中。 但是,如果您实际上使用GET参数来发送会话信息,则必须这样做。

发现问题似乎我没有在注销表单所在的索引 isset 条件下关闭表单,我的不好,因为我没有向你们展示代码 :D 所以问题是</form> ... pff 抱歉

当天的课程,伙计们总是关闭你</...> :)

暂无
暂无

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

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