/    Sign up×
Community /Pin to ProfileBookmark

Hey!

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?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ssdogMar 21.2013 — Can you more specific? What that loop should do?
Copy linkTweet thisAlerts:
@ssdogMar 21.2013 — try something like this:

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]
Copy linkTweet thisAlerts:
@bigzeeauthorMar 21.2013 — try something like this:

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?
Copy linkTweet thisAlerts:
@bigzeeauthorMar 21.2013 — I mean would it continue to echo the variable?
Copy linkTweet thisAlerts:
@ssdogMar 21.2013 — Yes, it sends until someone stops it, but be carefull: [B]IF YOU CLOSE THE BROWSER, YOU DON'T STOP THE PROCESS[/B]

As about delays, it doesn't have any delay...send the email, then runs the process again.

If you want to add a delay, you can put the script to sleep by doing this:
[code=php]else

{

$i = 0;
echo $i;
sleep([int_value]);
proc_close(proc_open("php [script_file_name] 201 &", array(), $foo));//run the process in background

} [/code]
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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"
Copy linkTweet thisAlerts:
@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

}
}
?>[/code]
Copy linkTweet thisAlerts:
@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

if(preg_match_all('/([0-9]+)(.*?)php (201)/', $running_ps, $a_matches))
{
echo "running";
}
else {
echo "not running";
}
} ?>
<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]


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()
×

Success!

Help @bigzee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...