<?php
$filename = 'switch.php';
$fp = fopen($filename, "w"); //w是写入模式,文件不存在则创建文件写入。
$len = fwrite($fp, '<?php return 1;?>');
fclose($fp);
print $len . '字节被写入了\n'; // 17字节被写入了\n
?>
<?php
ignore_user_abort(); // 即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
set_time_limit(0); // 执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去
/**
* 获取当前访问的url的域名及端口号
* @return string
*/
function GetCurUrl() {
$url = 'http://';
// 判断当前页采用的协议是HTTP还是HTTPS
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$url = 'https://';
}
// 判断端口
/**
* REQUEST_URI:URI用来指定要访问的页面
* SERVER_PORT:Web服务器使用的端口,默认为80
*/
$url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
// 截取'/test'之前的部分
$url = substr($url,0,strrpos($url,'/test'));
return $url;
}
/**
* @param $url 请求地址
* @param $data 要传递的参数
* @return bool|string
*/
function insertData($url, $data) {
// 开启curl_init()
$ch = curl_init();
// 设置网址url
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 设置超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
// 开启post请求
curl_setopt($ch, CURLOPT_POST, 1);
// 开始传递数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // curl post传参
// 在请求页面显示被请求页面的信息
$result = curl_exec($ch);
// 关闭curl_init()
curl_close($ch);
return $result;
}
function getData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// 获取参数id、flag
$id = $_GET['id'];
$flag = $_GET['flag']; // 1开启,0关闭
// 控制“开关”
if($flag == 0) {
$fp = fopen('switch'.$id.'.php',"w"); //w是写入模式,文件不存在则创建文件写入
fwrite($fp, '<?php return 0;?>'); // 覆盖原文件的内容
fclose($fp);
} else {
$fp = fopen('switch'.$id.'.php',"w"); //w是写入模式,文件不存在则创建文件写入
fwrite($fp, '<?php return 1;?>');
fclose($fp);
}
// 获取url
$accessUrl = GetCurUrl();
// 获取执行时间间隔
$interval = json_decode(getData($accessUrl."/test/index.php/Config/selectById?id=".$id))->data->interval;
$switch = include 'switch'.$id.'.php';
while($switch) {
$switch = include 'switch'.$id.'.php';
insertData($accessUrl."/test/index.php/Data/insert", array('id'=>$id)); // 执行数据插入方法
sleep($interval);
}
exit();
?>
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
参考链接:
https://www.cnblogs.com/koala0521/p/7267401.html
https://blog.csdn.net/doubleface999/article/details/75003324