<?php
// 处理过程:xxx.php
// 获取进度:xxx.php?act=1
$act = isset($_GET['act']) ? $_GET['act'] : '';
switch ($act) {
case '1':
present();
break;
default:
process();
}
/**
* 处理过程
*
* @author gjw
* @created 2023-04-28 11:12:42
*
* @return void
*/
function process()
{
$total = 5;
for ($i = 1; $i <= $total; $i++) {
// todo ...
$data = array(
'current' => $i,
'total' => $total,
'percent' => round(($i / $total) * 100, 2) . ' %'
);
session_start();
$_SESSION['process'] = $data;
session_write_close();
sleep(1);
}
}
/**
* 读取进度
*
* @author gjw
* @created 2023-04-28 11:12:52
*
* @return void
*/
function present()
{
session_start();
$data = $_SESSION['process'];
$refresh = '';
if ($data['current'] < $data['total']) {
$refresh = '<meta http-equiv="refresh" content="1">';
}
echo '
<html>
<head>' . $refresh . '</head>
<body>
<div id="progress">
<div><span id="current">' . $data['current'] . '</span> / <span id="total">' . $data['total'] . '</span></div>
<div id="bar"><span id="percent">' . $data['percent'] . '</span></div>
</div>
</body>
</html>
';
}
PHP使用session实现进度
于 2023-04-28 14:22:50 首次发布