I have this simple code and I want it running all the time on the server,AND NOT ONLY WHEN IT IS OPENED IN BROWSER TAB.

I dont know exactly what is happening in http header but i guess that there is a way to keep it running non stop.

Is there some header() directive?

<?php
session_start();
$i=0;

if(!empty($_SESSION["i"]))
{
	$i=$_SESSION["i"];
}	
	if($i==1)
	{
		rename("php.gif","pom.gif");
		rename("logo.gif","php.gif");
		rename("pom.gif","logo.gif");

		$_SESSION["i"]=0;
		print("txt1 u txt2");
		header('Refresh: 5');
	}
	if($i==0)
	{
		rename("logo.gif","pom.gif");
		rename("php.gif","logo.gif");
		rename("pom.gif","php.gif");
		
		$_SESSION["i"]=1;
		print("txt2 u txt1");
		header('Refresh: 5');
	}

?>

Recommended Answers

All 9 Replies

Member Avatar for nevvermind

Read this continuously (like your PHP does):

... if $i = 1, change the value to "0" and restart, then, if $i = 0, change the value to "1" and restart, then, ...

someone serious to help?

You want this to run on your server infinitely, without anybody having to actually execute it?

I would suggest one of two things:

  1. Use your Operating-System's tasl scheduler to execute it at startup. For Linux servers that would be Cron. For Windows it would be the Task Scheduler.

    For example, using Cron, you could add this to have your server execute a script when it boots:

    @reboot /var/scripts/myscrip.php

    Given that the script itself does not get cut off, and that it runs in an infinite loop, it should always be running.

  2. Create a PHP page to be executed via a browser (like usual) that triggers your script in the background. You should be able to use proc_open() to do that. Something like this might work:
    proc_close(proc_open ("/var/scripts/myscript.php &", array(), $f));

You would have to change your script, however. It would have to be able to run in the background infinitely without using the header function. (It wouldn't be running inside a HTTP server, so there would be no headers to set...)

For example, your script should look more like:

#!/usr/bin/php

<?php
	// The "#!/usr/bin/php" line points to the PHP
	// executable on your server. It is required for
	// the script to be executed as PHP. You may have
	// to alter the path to point to wherever your PHP
	// executable is.

	// Make sure the script does not time out.
	set_time_limit(0);

	$i = true;
	
	// Start an infinite loop.
	while(true) 
	{
		// Do something based on the $i switch.
		if($i) {
			rename("php.gif","pom.gif");
		}
		else {
			rename("logo.gif","pom.gif");
		}
		
		// Flip the switch
		$i = !$i;
		
		// Halt the script for 5 seconds.
		sleep(5);
	}
?>

P.S.
Just out of interest, why are you renaming these files every five seconds?

Just out of interest, why are you renaming these files every five seconds?

to tell that code is alive and working

for start lets say I have working server 0/24 and skip that boot part

and I will manually start script via browser,so I need just endless running

I guess I could use step 2 to achieve this?

Yes, you could. But there is a better way. Given that you can use cron on your server. (Most decent hosts allow this, and if you have your own server this is no problem.)

What I would do in your situation is to create two scripts; the main script that runs the loop and does whatever it is you want the loop to do, and a second script that triggers the main script when it is not already running. Then I would have Cron execute the second script every few minutes, so that if my main script is not running for some reason, it will start it. That will ensure your script is up no matter what.

As an example, consider these two scripts:

infinite.php
Runs an infinite loop, dumping the current timestamp into a text file every one second.

#!/usr/bin/php

<?php
	set_time_limit(0);
	$outFile = '/home/user/test/output.txt';
	while(true) {
		$fh = fopen($outFile, 'wb');
		if($fh) {
			fwrite($fh, time());
			fclose($fh);
			sleep(1);
		}
		else {
			break;
		}
	}
?>

run.php
Checks to see if the text file created by infinite.php exits, and whether it is updated within a three second period. If not, it executes the infinite.php file in a new background process.

#!/usr/bin/php

<?php
$isRunning = false;
$outFile = '/home/user/test/output.txt';

if(file_exists($outFile)) {
	$originalStamp = file_get_contents($outFile);
	for($i = 0; $i < 3; $i++)
	{
		sleep(1);
		$current = file_get_contents($outFile);
		if($current > $originalStamp) {
			$isRunning = true;
			break;
		}
	}
}
if(!$isRunning) {
	shell_exec("/home/user/test/infinity.php >/dev/null &");
}
?>

And to have the run.php file executed automatically, I would set a Cron job to execute every few minutes. On a standard Linux install, you can use the following command to edit the Cron jobs for your user:

$ crontab -e

That would bring up a text file in your preferred editor, where each line represents a single Cron job. You simply add a new line:

*/1 * * * * php /home/user/test/run.php

This one executes my run.php file every one minute. (See Wikipedia for details on the cron format.)

Note! It is important that you make sure there is an empty line at the bottom of the crontab file. If there is not, the last cron job will not work.


If you do not have access to Cron, you can do the same thing over HTTP. You would simply have to modify the run.php code so that it can work via a browser. Also keep in mind that code executed via a HTTP server is executed as the user running the HTTP server, so you may find yourself having permission problems. (Which is why I strongly recommend the Cron method over this.)

thank you very much!

solved

hm,it seems that my php hosting doesn't allow sleep() function

while(1) - it runs to fast :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.