在后台启动程序并停止进一步加载脚本

时间:2013-06-10 07:58:47

标签: php

我想在后台启动一个php脚本。我正在做的是运行此代码

<?php
while(true){
$commandString = 'start /b C:\xampp\php\php.exe  "C:\xampp\htdocs\caliban\blobs.php"'; 
popen($commandString, 'r'); 
sleep(5);
}
?>

在网络浏览器中,但页面不会停止加载。如何执行此代码并停止Web浏览器进一步加载而不停止在后台完成任务?。

blobs.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 )
);

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这样做

<?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');

?>

working.This是我的blobs.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-completly-hidden-batch-file