Log in

View Full Version : PHP continous loop?



Hitman
10-04-2009, 04:54 PM
Hey, well basically I'm using a function that generates a random string, then in my PHP code it saves that string to a text file. It's only doing one at a time though (one per page load). Sure, if I reload the page it adds another, but one at a time is slow.

I've used
header('refresh:0; url=page.php');, but the problem with that is it keeps reloading the page... I want to just load the page and it creates a new string, saves it, creates a new string, saves it... and continues doing that X amount of time (x being infinite or 100).

Is it possible to do this somehow? I've used google but it got confusing.

Thanks.

Dentafrice
10-04-2009, 05:53 PM
<?php

$times = 100; // times to run the script.

for($i = 0; $i < $times; $i++) {
// number generating code here. //
}

?>

Hitman
10-04-2009, 06:01 PM
Thanks Caleb! :D Works like a charm.

Blob
10-04-2009, 06:33 PM
Or you could do (for unlimited)


<?php
$times = -1; // times to run the script.
for($i = 0; $i != $times; $i++) {
// number generating code here. //
}

?>

should work :)

Dentafrice
10-04-2009, 06:39 PM
Or you could do (for unlimited)


<?php
$times = -1; // times to run the script.
for($i = 0; $i != $times; $i++) {
// number generating code here. //
}

?>

should work :)
Then the PHP script would timeout, and eventually the browser would timeout.

Unless you was running it as a CLI application.

Invent
10-04-2009, 06:40 PM
Or you could do (for unlimited)


<?php
$times = -1; // times to run the script.
for($i = 0; $i != $times; $i++) {
// number generating code here. //
}

?> should work :)

Or just something like


while( true ) {
}

Blob
10-04-2009, 07:08 PM
Or just something like


while( true ) {
}


Yeah but I wasnt that clever to think of a while :P

Hitman
10-04-2009, 07:41 PM
Or just something like


while( true ) {
}

Oh darn, I was going to do that before I asked but I wasn't sure so I left it aha.

So far I'm doing 100,000 working nicely.

If I use what you posted for unlimited, how many do you reckon it'd do before timing out?

Dentafrice
10-04-2009, 07:43 PM
Depends on how you setup your server for max execution time, and the browser you're using.

If you're generating things, and doing this for the hash, let me know.. and I can help you generate a big list.

Tomm
11-04-2009, 07:04 PM
You can override PHP's max execution time but I would strongly recommend against it as it has the potential to overwhelm your server with the endless loop.

PHP is not really the ideal language to be generating md5 hashes like that (I assume you are using this for your project) due to the overhead that comes with it. I'd recommend you use a language like C++ with a highly optimized MD5 hashing code and just dumping to a text file or if you wanted to insert directly into a mysql database you could have have one thread doing the hashing and another moving to MySQL. Although depending on how it may work out a backlog of MD5 hashes may occur and start to eat up your memory.

Want to hide these adverts? Register an account for free!