I’m fairly new to php, and I wanted to create a form where if they pressed a button, a loop would start, and it would only stop when they either exited the page, or hit a stop button. I’m not sure how to go about doing this, any advice/help?
put this in a php file [code=php] <?php $running_ps = shell_exec('ps -afx | grep php');//get the php processes
if(preg_match_all('/([0-9]+)(.*?)php (201)/', $running_ps, $a_matches)) {//identify the proces you need foreach ($a_matches[1] as $key => $value) { $a_running_proc = $value; } } if (isset($_REQUEST['action'])) {
if($_REQUEST['action'] == '0' ) {
//stop the process proc_close(proc_open('kill '. $a_running_proc, array(), $foo));//kill the process } else { //do whatever you want to do //then start the process again proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background
}
?> [/code]
and here is the html [code=html]<html> <body> <a href="[script_file_name]?action=1"><button>Start</button></a> <a href="[script_file_name]?action=0"><button>Stop</button></a> </body> </html>[/code]
put this in a php file [code=php] <?php $running_ps = shell_exec('ps -afx | grep php');//get the php processes
if(preg_match_all('/([0-9]+)(.*?)php (201)/', $running_ps, $a_matches)) {//identify the proces you need foreach ($a_matches[1] as $key => $value) { $a_running_proc = $value; } } if (isset($_REQUEST['action'])) {
if($_REQUEST['action'] == '0' ) {
//stop the process proc_close(proc_open('kill '. $a_running_proc, array(), $foo));//kill the process } else { //do whatever you want to do //then start the process again proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background
}
?> [/code]
and here is the html [code=html]<html> <body> <a href="[script_file_name]?action=1"><button>Start</button></a> <a href="[script_file_name]?action=0"><button>Stop</button></a> </body> </html>[/code][/QUOTE]
If I did this to the else statement: [code=php] else { $i = 0; echo $i;
proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background
} [/code]
Would that continue to send the email until someone hits stop? If so, at what delay? If not, how can I make it do that?
@ssdogMar 21.2013 — #Yet...you won't be able to see that this works by echoing, because it is a process, so it won't refresh your web page...you should write those values into a file or a DB.
@bigzeeauthorMar 21.2013 — #Yet...you won't be able to see that this works by echoing, because it is a process, so it won't refresh your web page...you should write those values into a file or a DB.[/QUOTE]
How would i check if the script is already running? As in if i want a field on.the first webpage that says "Script status: Running/not running"
@ssdogMar 22.2013 — #you could do something like [code=php] <?php
$running_ps = shell_exec('ps -afx | grep php');//get the php processes
if(preg_match_all('/([0-9]+)(.*?)php (201)/', $running_ps, $a_matches)) {//identify the proces you need foreach ($a_matches[1] as $key => $value) { $a_running_proc = $value; } } if (isset($_REQUEST['action'])) {
if($_REQUEST['action'] == '0' ) {
//stop the process proc_close(proc_open('kill '. $a_running_proc, array(), $foo));//kill the process echo "process stopped"; require('html_file_name'); } else { //do whatever you want to do //then start the process again proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background echo "process running";//print this text before showing buttons require('html_file_name');//this will show those buttons
@ssdogMar 22.2013 — #but if you want to do this check when you first enter the page, yoiu should change the html file, make it php and add this: [code=php] <html> <body> <? $running_ps = shell_exec('ps -afx | grep php');//get the php processes
but if you do this, there is no need to change the php as described in the post above...you could just call header('Location:[button_file]'); instead of echo and require()