简体   繁体   English

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

[英]PHP session ends when submit button is clicked

I want to make profile upload image for users so they can upload their own avatars on their profile...So the issue here is whenever i click the upload button session gets destroyed.我想为用户制作个人资料上传图片,以便他们可以在他们的个人资料中上传自己的头像......所以这里的问题是每当我点击上传按钮会话被破坏。

Here is the form:这是表格:

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>";
}
?>

partial code of upload.php file: 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**

Code if user is successfully logged in, inside loginCheck.php:如果用户成功登录,则在 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();
}

And the last code section that is related to the problem is located to index.php:与问题相关的最后一段代码位于 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**

If i remove the 'profileimgID' to nothing ('') everything works fine but isset method doesnt hide-show the content.如果我将 'profileimgID' 删除为空(''),一切正常,但 isset 方法不会隐藏显示内容。 If i keep it as it's isset method works fine but upload button destroys the session and user is logged out.如果我保留它,因为它的 isset 方法工作正常,但上传按钮会破坏会话并且用户已注销。

print_r($_SESSION) results in both index.php and upload.php if user is successfully logged in: for user #2如果用户成功登录,print_r($_SESSION) 会同时生成 index.php 和 upload.php:对于用户 #2

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

I checked the console for requests , when i click the upload button i get this message:我检查了控制台的请求,当我单击上传按钮时,我收到以下消息:

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

This part (isset($_SESSION['profileimgID'])) is interfering somehow with this process.这部分 (isset($_SESSION['profileimgID'])) 以某种方式干扰了这个过程。 When i remove it, session is maintained and it works fine upload works too.当我删除它时,会话被维护并且它可以正常工作,上传也可以工作。

UPDATE: this is what i get when i click the upload-button: https://i.stack.imgur.com/So7OD.png更新:这是我点击上传按钮时得到的: https : //i.stack.imgur.com/So7OD.png

this is i guess the right one ?: https://i.stack.imgur.com/HcBqz.png这是我猜对的吗?: https : //i.stack.imgur.com/HcBqz.png

Im new to php so... sorry for my mistakes.我是 php 新手,所以...抱歉我的错误。

Exactly how are you maintaining the session-identifier now?您现在如何维护会话标识符? "Sessions" rely upon a "session-id" being somehow sent from the client to the host with each exchange: normally, this is done using a cookie, but it could be done using a GET parameter (eg &sessionid=XXXX ) It sounds to me like this information isn't being sent: the session hasn't been "destroyed," actually, but you can't find it. “会话”依赖于每次交换时从客户端以某种方式发送到主机的“会话 ID”:通常,这是使用 cookie 完成的,但可以使用GET参数完成(例如&sessionid=XXXX )听起来对我来说,这个信息没有被发送:会话实际上并没有被“破坏”,但你找不到它。

Probably the fastest way to solve this is to use the network debugging features of your browser: look at the complete packet of data that's being sent, including the HTML headers (which is where cookies will be).解决此问题的最快方法可能是使用浏览器的网络调试功能:查看正在发送的完整数据包,包括 HTML 标头(即 cookie 所在的位置)。 First, look at "normal" exchanges.首先,看看“正常”的交流。 Then, look at the one that happens when you click that button.然后,查看单击该按钮时发生的情况。 "Cookies" will be sent every time since they live in the header. “Cookies”将每次发送,因为它们位于标题中。 But, if you're actually using a GET parameter to send the session-info, you'll have to do it.但是,如果您实际上使用GET参数来发送会话信息,则必须这样做。

Found the problem it seems like i didnt close the form on the index isset condition where logout form was located, my bad because i didnt show u guys the code :D so the problem was </form> ... pff sorry发现问题似乎我没有在注销表单所在的索引 isset 条件下关闭表单,我的不好,因为我没有向你们展示代码 :D 所以问题是</form> ... pff 抱歉

Lesson of the day , guys always close ur </...> :)当天的课程,伙计们总是关闭你</...> :)

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

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