954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Run PHP script every 10 seconds

Hello together...

hope someone can help me:-)

I want to to run a php file every ten seconds on serverside,
how can I do this?

CronJobs are not really working; Server = Apache, PHP 4 or 5.

Greetings & Thanks

maninweb
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Why do you need a script to run every 10 seconds? There has to be a more efficient way to do whatever it is you are trying to do.

stymiee
He's No Good To Me Dead
Moderator
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
 

Cron jobs are your easiest solution.
Another method would be making 2 PHP scripts to bounce off of each other.

File 1: Run the target script wait 10 seconds and call File 2.

File 2: Run target scritpt wait 10 seconds call File 1.

It would be advisable to make a script take in a $_GET value to 'exit' the scripts.

So once you go to file 1: in your browser, it will create a continuous loop which will bounce form one file to the next. Until you type in this exit command.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Hi...

thanks for your responses. Well, I need it because I must update
database entries and perform new calculations every ten seconds.

File 1: Run the target script wait 10 seconds and call File 2. File 2: Run target scritpt wait 10 seconds call File 1.

Seems a good idea, but note the call must be don on the server.
I'll try it:-) As I know, CronJob's can't be executed regulary each
ten second or may be my Information false?

Greetings

maninweb
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Or you can simply ad
[PHP]header('refresh:10; url=same_page.php');[/PHP]

johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

Hi...

Thanks, good idea, can I use a timevalue, like refresh 12:00:xx ?

Greetings

maninweb
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 
Or you can simply ad [php]header('refresh:10; url=same_page.php');[/php]



Once this is setup and we go to the script URL.
this header() function will active.

Will i run in a continuous loop forever? or only if the script is being viewed by a user.

Secondly, if this is running in a continuous loop.
how can we stop the execution ?

Drag...

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Ir runs only when viewed by a user and will run in continuous loop until the user closes the browser.

Of course, you can setup a $_GET variable to count the loops and stop at some point
Thanks, good idea, can I use a timevalue, like refresh 12:00:xx
As far as I know, in header(refresh...) you can only specify the number of seconds the script has to wait untill refresh, not a certain hour.

johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

Thanks :icon_wink:

maninweb
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

You're welcome :)

johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

Right. cool.
So, my method would work continuously,
without someone viewing the page.

Yours would only work if someone had the script open.

So it's up to you maninweb. Either will work, it just depends on how you want to do it.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Hi...

well I will try out all ideas. Logically it must be possible to open
a browser on the server (Linux) and run the script all ten seconds
or just once on specified time. As I'm writing now the first call of
the script can be done by a cronjob. Hope I'm right. :icon_smile:

Greetings

maninweb
Newbie Poster
11 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

I am using a script for banner rotation that I want to refresh every 10 seconds. I used this

[php]header('refresh:10; url=filename.php');[/php]

in the file but in the first interval the browser leaves the website and shows just the refreshing banners in a page. How can I refresh and remain in the site?

Thank you

jmag
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Actually I got it done with a frame. :)

jmag
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You