!!! ajax异步请求php后台处理耗时任务session_write_close !!!

问题:ajax异步请求php后台处理耗时任务之后,ajax之后的js都可以正常运行,但是当前浏览器不能再点击浏览被ajax请求的地址的其他页面(卡死、阻塞)。
解决:
第一、前端正常ajax请求。
第二、 PHP后台方法里面必须加入这三句:
ignore_user_abort();//脱离客户端
set_time_limit(0);//不限时间执行
session_write_close();//session解锁


session_write_close()是重点,后台没有执行完成,session是被锁住的,所以要添加session_write_close — Write session data and end session。
https://blog.csdn.net/u010884130/article/details/50106313

 

session_write_close() 用法

1.需要session控制的大文件下载,防止因为占用session文件时间太久,导致其他页面的session无法执行

session_write_close() worked as a lifesaver for me when automatically uploading files to a user (forcing a download instead of a link). If files are large, and since session_start() does not allow another page using session_start() to proceed until it's done, i was not able to upload more than one file at a time. By using session_write_close() before beginning the file upload, my users can now download as many big files as they like, at the same time. Example:
<?
session_start();
/* Do session stuff here; security; logging; etc. */
session_write_close();
/* NOW write out the requested file. */
header("Content-type: audio/x-mpeg"); /* or whatever type */
header("Content-Disposition: attachment; filename=" . $filename); 
header("Content-Length: " . $filesize);
header("Content-Transfer-Encoding: binary\n\n");
header("Pragma: no-cache"); 
header("Expires: 0"); 
$file_contents = file_get_contents($filepath); 
print($file_contents);
?>

2.保存当前的session变化,防止由于header页面跳转导致修改的session内容没有保存,被丢失

This function is essencial when you change $_SESSION[] variables and then, at some poit in the middle of the script, you send an header("Location: ) function to the browser, because in this case the session variables may not be saved before the browser change to the new page.
To prevent from lossing session data, allways use session_write_close before this header function. session_write_close will force session data to be saved before the browser change to the new page.
Hope this will help you not to loose 1 day wondering why people could not authenticate or make other changes in session vars in your site.

 

https://www.cnblogs.com/jking10/p/3821066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值