# exec() in php to fork process



## fharkness (Oct 28, 2008)

Hi All,
I'm having problems getting exec() to run a script, here is the scenario:

I have a ticket sales system, a user chooses the tickets they want and then have 20mins to complete the payment transaction or else the tickets are released back for sale. 

I therefore am attempting to use exec() to start the background timer which looks like this - 


```
require_once("../includes/functions.php")

$id = $_GET['id'];

sleep(1200);

if(!completeTransaction($id))
{
    deleteFromDB($id);
}
```
so i try and call this using:


```
exec("/usr/local/php /kunden/homepages/41/d2??????/htdocs/Paypal/timelimit.php?id=38> /dev/null &");
```
at the moment it basically just doesn't do anything that i can tell.
i'm thinking i don't have the right path for php. phpinfo() says that the path
is 
/usr/local/php/bin
and i've tried everything from that to 
php
/usr/php/bin
/usr/php/bin/php
/usr/local/php
/usr/local/php/bin
/usr/local/php/bin/php
none work

i'm with 1and1 hosting so don't really know what the deal is with all configurations. and lets just say their tech support are in no hurry!

Any help would be greatly appreciated, its driving me mad!!!
Thanks for your time,
Felix


----------



## fharkness (Oct 28, 2008)

p.s. it is a linux host, and if anyone knows of any easier way to run a background process i'm all ears.
Cheers,
Felix


----------



## jamiemac2005 (Jul 5, 2007)

Hey, sounds a little mountain out of mole-hill... Why not try using javascript as your timer?

If you had this in the head of your page it would work:

```
<script language="javascript">

//a function to change to a "sorry you lost your tickets" page
function cancelTransaction(){
  window.location.href = "cancelTrans.php";
}

//set a timer for 20 minutes
//convert 20 minutes to milliseconds(i may have miscalculated this)
var time = 20*60*60*1000;
//the actual interval
var interval = setInterval("cancelTransaction();", time);

</script>
```
Then cancelTrans.php would contain a sorry message and the code to cancel the transaction. It avoids overloading your server (multiple timers could make things slow[JS is run on the user's pc])...

If you do use this method you should probably also save the time of the start of the transaction to the database you're using to then compare with the end time when the transaction is completed. (Because JS can be overridden very easily).

Anyway if that's not ideal i can read into your exec problem.

Cheers,
Jamey


----------



## jamiemac2005 (Jul 5, 2007)

Sorry the code should be:


```
var time = 20*60*1000;
```
i did get it wrong, that would have been 20 hours. Should work now.


----------



## fharkness (Oct 28, 2008)

Hi Jamey,
Thanks for your reply. That is definately a good idea using JS, i am getting a bit worried about the server load. 
However, the problem is that the user doesn't actually pay on that page, they are redirected to paypal at the moment. So i really need to do the timer server side to ensure that it keeps ticking away. 
do you know of anyway other way of doing it that might not be as heavy?

Thanks again for your time. 1and1 are absolutely rubbish still haven't replied to my support request! Next time i host myself.

Cheers,
Felix


----------



## jamiemac2005 (Jul 5, 2007)

Okay, you could just add the time that they started the transaction to a database and compare it on each page with the current time and act accordingly(e.g. if it's later than 20 minutes added to the time they started, cancel the transaction)... It's not ideal because it won't be instantanious but it would still work.

Cheers,
Jamey


----------



## fharkness (Oct 28, 2008)

yeah i suppose i could just check when a new customer comes along, if the transaction has expired in the Database then the new customer can select those tickets, that would work. and it would also mean that the old customer could still complete the transaction if noone else shows interest in them.
Thats not a bad solution actually. Nice and light weight. 

If i had infinite processing power i would prefer to have a timer process, but we can't have everthing eh?

Cheers,
Felix


----------



## jamiemac2005 (Jul 5, 2007)

Haha, yeah, i understand, the timing process would be preferable, but the solution suggested would work.

Cheers,
Jamey


----------



## Gorge (Nov 10, 2008)

how about

```
<meta http-equiv="refresh" content="1200; URL=cancelTrans.php">
```
?


----------

