0

Please bear with me, as I am a bit new to this. I have a page that calls another page after a form is submitted. The new page contains a lengthy php loop which runs before the page is displayed. The problem is, the server sends a 500 error before the loop has time to finish. I have economy shared linux hosting with GoDaddy, and they don't allow me access to error logs. I am pretty sure it is not a memory issue. I suspect that apache is simply timing out. I have shortened the loop and it works fine, so I am sure that there is nothing wrong with the code, but I would like to know if there are any tricks to code the loop better, or trick apache into giving the loop more time, or anything to make this work. Thanks in advance, and here is the code:

Some variables before the loop starts:

$usernum1 = $_POST['num1']; 
$usernum2 = $_POST['num2']; 
$usernum3 = $_POST['num3']; 
$usernum4 = $_POST['num4']; 
$usernum5 = $_POST['num5']; 
$usernum6 = $_POST['num6']; 
$usernum7 = $_POST['num7']; 

$usernumbers = array($usernum1, $usernum2, 
                     $usernum3, $usernum4, 
                     $usernum5, $usernum6, 
                     $usernum7);

sort($usernumbers, SORT_NUMERIC);

$i = 0;
$counter = 0;
set_time_limit(0);
$input = range(1, 49);

Here is the loop:

do   {

$rand_keys = array_rand($input, 7);
sort($rand_keys, SORT_NUMERIC);

if ($input[$rand_keys[0]] == $usernumbers[0] && 
    $input[$rand_keys[1]] == $usernumbers[1] && 
    $input[$rand_keys[2]] == $usernumbers[2] && 
    $input[$rand_keys[3]] == $usernumbers[3] && 
    $input[$rand_keys[4]] == $usernumbers[4] && 
    $input[$rand_keys[5]] == $usernumbers[5] && 
    $input[$rand_keys[6]] == $usernumbers[6]) 
 {   
  $i = 1;
 }

$counter = $counter + 1;

     } while ($i == 0);

You can see the that the loop ends when $i == 1, and $i is assigned 1 when all numbers match. I should also mention that I added max_execution_time = 1000 to my php5.ini file on the server, and when I check phpinfo, it seems to have taken effect. And That's IT! Not very complex. If anyone can help me figure out some trick, or better way, please, please help, as I have busted my brain for 2 days on this.
I just need the loop to have more time to finish. Thanks again in advance :-)

EDIT: For those who want to see the script working, I have it for a lottery that only has SIX numbers, is easier to win, and doesn't timeout. Go here and MAKE SURE TO select the top option (Lotto 649). http://diablogosse.com/test/lottosims.php

EDIT2: Sorry if this comes across badly, but I just wanted to clarify something after quite a few posts. My question is not "Why in God's name would I WANT to do this??". The question is more like: "How can I make what I WANT to do work.". ;-)

Chris

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • Are you sure you are not working with an infinite loop? You can try writing to a .txt log file to help you debug – gosukiwi Feb 13 '13 at 15:10
  • Of course you'll time out, you're asking users to guess 7 different random numbers *in order* that get randomized *again* if you get it wrong. – jeremyharris Feb 13 '13 at 15:11
  • Can you please explain with comments in the code what you intend it to do? – Michael Berkowski Feb 13 '13 at 15:12
  • 1
    Well, as there's only a 1 out of 10000000 chance that the numbers match you should expect a fairly long executing time... Also, what exactly do you want to achieve? It looks like some lotto alike thing to me, but I don't see how looping over an ever-changing random number until it matches fits into that picture. – fvu Feb 13 '13 at 15:15
  • Yes, I am sure it is not an infinite loop. – Christopher Gosse Feb 13 '13 at 15:17
  • Expecting random array generated match with one you've is illogical. Besides are you checking all the values in `$usernumbers[]` are within range `[1..49]`? – जलजनक Feb 13 '13 at 15:26
  • Yes, I am sure it is not an infinite loop. $rand_keys is my own variable, not a function. Jeremyharris it is a lottery simulator not a guessing game... Hmm, could it be that I sorted the usernumbers, but not the drawnumbers?? (Random numbers). Everyone - It takes the users numbers from a form and simulates draw after draw until they win the lottery and there numbers match. Here is an example of when it works (or should) http://diablogosse.com/test/lottosims.php - MAKE SURE to select the top (649) one. That lotto is easier to win than the others, and so it works... – Christopher Gosse Feb 13 '13 at 15:27
  • @DoSparKot - Yes, I did a separate script to test this method to make sure that all numbers are always in range... – Christopher Gosse Feb 13 '13 at 15:30
  • @Michael Berkowski - array_rand — Pick one or more random entries out of an array - So NO, it does not only return 1 value. – Christopher Gosse Feb 13 '13 at 15:38
  • @ChristopherGosse I totally missed the `, 7` on that array_rand call. – Michael Berkowski Feb 13 '13 at 15:47
  • hmmm.. still getting 500 error... I don't get the 500 error if the numbers match sooner... So it must be a time-out on the server end... – Christopher Gosse Feb 13 '13 at 16:12
  • `How can I make what I WANT to work?` Give it sufficient time, be it on server or user's PC. – जलजनक Feb 13 '13 at 16:34
  • @DoSparKot That is precisely my question. How do I give it sufficent time, be it on the server or user's PC? – Christopher Gosse Feb 13 '13 at 16:48
  • Own a server or move the code to javascript and let it run. – जलजनक Feb 13 '13 at 16:52
  • Well, unless I get a better answer, I guess JavaScript is the way to go. I also considered running a server here at home, but I don't have a second system, and don't really like the idea of running it on my personal system. UGghh.. I guess I gotta go figure out how to write my already written php script in JavaScript. Oh well, live and learn... @DoSparKot I was gonna vote your answer as being useful, but apparently I can't do that unless I have at least 15 reputation. I am brand new today on StackOverFlow.. – Christopher Gosse Feb 13 '13 at 16:57
  • If you're on Windows, use WAMP stack to run your PHP code. You however accept an answer. – जलजनक Feb 13 '13 at 17:04

3 Answers3

0

you could use

set_time_limit ( int $seconds )

at the start of the loop and after each rotation the script time limit will be increased by x

Millard
  • 1,157
  • 1
  • 9
  • 19
  • Actually, setting it to zero is equivalent to setting it "forever". – nicbou Feb 13 '13 at 15:14
  • the execution time thing seems to work... as it was erroring after less than 30 seconds... after changing this, i was no longer erroring.. but not sure about this set_time_limit... – Christopher Gosse Feb 13 '13 at 15:52
  • hmmm.. still getting 500 error... I don't get the 500 error if the numbers match sooner... So it must be a time-out on the server end... – Christopher Gosse Feb 13 '13 at 16:12
  • @Tom Chew-head Millard Thank you, I am going to try this for sure, but I am pretty sure that my set_time_limit(0) disables the time limit, thus giving it as much time as it needs.. I am wondering if it is an apache setting on the server needs to be changed, and not php setting.. – Christopher Gosse Feb 13 '13 at 16:20
  • @ChristopherGosse this should override the script time set in the php.ini file – Millard Feb 14 '13 at 15:47
0

set_time_limit doesn't work when PHP is in safe mode, as it seems to be the case on GoDaddy.

See here: ini_set, set_time_limit, (max_execution_time) - not working.

May I ask you what you are attempting to achieve with that loop?

Community
  • 1
  • 1
nicbou
  • 1,047
  • 11
  • 16
  • It takes the users numbers from a form and simulates draw after draw until they win the lottery and there numbers match. Here is an example of when it works (or should) diablogosse.com/test/lottosims.php - MAKE SURE to select the top (649) one. That lotto is easier to win than the others, and so it works... – Christopher Gosse Feb 13 '13 at 15:30
  • hmmm.. still getting 500 error... I don't get the 500 error if the numbers match sooner... So it must be a time-out on the server end... – Christopher Gosse Feb 13 '13 at 16:14
  • Could it be a CPU usage issue? I could easily see a cheap shared host kill a script that seems to be looping 13 million times. – nicbou Feb 13 '13 at 17:21
0

Just a thought:

App Goals:

  • user enters six favourite numbers between 1 and 49 i.e. [1..49]
  • app generates six random numbers between 1 and 49 i.e. [1..49]
  • if both sets of numbers same, user WINSssss..

Simulation Goals:

  • Let the users know how many days it'll take to win with lucky numbers

Implementing in javascript will save you lot of server resources. Instead of looping so many times, return after every check. If there is match user wins. If user didn't win, respond with taunting messages to provoke user to play again.

You were so close, 4 matching numbers! wanna play again?

जलजनक
  • 3,072
  • 2
  • 24
  • 30
  • I can't do that.. this is a lottery simulator.. simulates how long it takes to win the lottery playing your favourite numbers.. each loop is a day.. or another draw.. without the loop you playing the lottery once.. – Christopher Gosse Feb 13 '13 at 15:46
  • Then give them a lesson on Probability. Try running it on a local server you'll feel the infinity around with that piece of code. – जलजनक Feb 13 '13 at 15:50
  • the seven is the lottomax draw.. throwing that seventh number takes it longer... too long.. The whole point of the loop is to be "Stuck" in the loop until the usernumbers match the draw numbers.. it is not close to infinity... more like 1 or 2 minutes.. the webpage i sent you that takes 6 numbers was an example of the script actually working.. (without seven numbers). – Christopher Gosse Feb 13 '13 at 15:55
  • @ChristopherGosse Hey, you need to move that code to `javascript` to slog the user's PC not precious cycles of server. Even better, instead of telling him how long it'll take just repeat the process with `taunting messages` saying `You were so close!, play again` – जलजनक Feb 13 '13 at 16:13
  • hmmm.. still getting 500 error... I don't get the 500 error if the numbers match sooner... So it must be a time-out on the server end... – Christopher Gosse Feb 13 '13 at 16:13
  • Shall I propose an alternate solution? I'm assuming `frustration` to be a part of user's experience! – जलजनक Feb 13 '13 at 16:14
  • I was seriously considering re-writing the process in javascript to see if it would work better. Do you think that it would succeed faster and more efficently on the client-side?? This was actually my last-resort plan. – Christopher Gosse Feb 13 '13 at 16:23
  • faster? NO. Because the logic is flawed. You can't hit a combination of numbers with randomness. – जलजनक Feb 13 '13 at 16:25
  • Every program is not written to do something logical... Is playing call of duty LOGICAL??? no... but they wrote a program (Call of Duty) so thousands of people can do it.. I edited my main post.. scroll to bottom and read.. – Christopher Gosse Feb 13 '13 at 16:42
  • I accepted this answer, because I believe that "Implementing in javascript will save you lot of server resources." is the best course of action. After I finally finish this, and have a chance to test it, I will give an update on how well this worked. Thanks DoSparKot. – Christopher Gosse Feb 13 '13 at 17:07