我想在后台启动一个php脚本。我正在做的是运行这段代码
<?php
while(true){
$commandString = 'start /b C:'xampp'php'php.exe "C:'xampp'htdocs'caliban'blobs.php"';
popen($commandString, 'r');
sleep(5);
}
?>
,但页面不会停止加载。如何在不停止在后台完成的任务的情况下执行此代码并阻止 Web 浏览器进一步加载?
斑点.php
<?php
//pdo connect function
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
//third function
$firstname = rand(4,4);
$lastname = rand(4,4);
$city = rand(4,4);
$continent = rand(4,4);
$profile = rand(4,4);
$image = rand(4,4);
$sql = "INSERT INTO mymodels ".
"(firstname,lastname,city,continent,image,profile) ".
"VALUES ".
"('$firstname','$lastname','$city','$continent','$image','$profile')";
mysql_select_db('caliban');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
?>
Sql
create table mymodels(
id INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(100) NOT NULL,
lastname VARCHAR(40) NOT NULL,
city VARCHAR(40) NOT NULL,
continent VARCHAR(40) NOT NULL,
image BLOB,
profile VARCHAR(400) NOT NULL,
PRIMARY KEY ( id )
);
function close_connection(){
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
}
然后
<?php
close_connection();
while(true){
$commandString = 'start /b C:'xampp'php'php.exe "C:'xampp'htdocs'caliban'blobs.php"';
popen($commandString, 'r');
sleep(5);
}
?>
这样做
<?php
function close_connection(){
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
}
close_connection();
exec('c:'WINDOWS'system32'cmd.exe /c START C:'xampp'htdocs'caliban'blobs.bat');
?>
工作。这是我的斑点.bat
PHP C:''xampp''htdocs''caliban''blobs.php
完全隐藏命令行
我创建了这个 in.vbs 文件并将其放入
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
我的 php 文件看起来像
<?php
function close_connection(){
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
}
close_connection();
exec('c:'WINDOWS'system32'cmd.exe /c START C:'xampp'htdocs'caliban'in.vbs C:'xampp'htdocs'caliban'blobs.bat');
?>
希望这对其他人有所帮助。
参考
https://superuser.com/questions/62525/run-a-completely-hidden-batch-file