dink87522 Posted August 12, 2009 Share Posted August 12, 2009 This code gives the maximum 30 second execution error. I am confused as to why, however one possible solution may be if the cookie does not exist if the user had not visited the site before (however I have the cookie and still get this same error). How do I fix the below code so it works and how do I check for a cookie on a user's computer (I know how to call a cookie, although how do you check if it does or does not exist? ) .Thanks. <?php $rnd = rand(1, 203); //recentQuestionsPop($rnd); $checked = ""; $recentlyAnswered = $_COOKIE["Recently_answered"]; $recentQuestions = explode("|", $recentlyAnswered) while ($checked != "1"){ // $rnd = rand(1, 203); recentQuestionsPop($rnd); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function recentQuestionsPop($rnd){ $checked = "1"; switch ($rnd){ case "$recentQuestions[0]"; if ($rnd == $recentQuestions[0]){ $checked = "0"; } break; case "$recentQuestions[1]"; if ($rnd == $recentQuestions[1]){ $checked = "0"; } break; case "$recentQuestions[2]"; if ($rnd == $recentQuestions[2]){ $checked = "0"; } break; } return $checked; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/169898-solved-my-loopfunction-times-out-the-page-cookies/ Share on other sites More sharing options...
JonnoTheDev Posted August 12, 2009 Share Posted August 12, 2009 You are returning the checked variable from the function so you should use it in the global scope. Your script times out because it is a never ending loop as checked is always empty. $recentQuestions = explode("|", $_COOKIE["Recently_answered"]); while(!$checked) { $rnd = rand(1, 203); $checked = recentQuestionsPop($rnd); } Also, are you sure you want to be treating integers as strings? // strings $checked = "0"; $checked = "1"; // integers $checked = 0; $checked = 1; Quote Link to comment https://forums.phpfreaks.com/topic/169898-solved-my-loopfunction-times-out-the-page-cookies/#findComment-896302 Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 The problem is you have a infinity loop, you need to change this $rnd = rand(1, 203); recentQuestionsPop($rnd); To $rnd = rand(1, 203); $checked = recentQuestionsPop($rnd); return does not return the variable you pass it but the value of the variable. You need to capture the returned value when you call the function. and how do I check for a cookie on a user's computer (I know how to call a cookie, although how do you check if it does or does not exist? ) To see if a cookie is set you use if(isset($_COOKIE['name_here'])) { // code for when the cookie is set } else { // code for when the cookie is not set } Quote Link to comment https://forums.phpfreaks.com/topic/169898-solved-my-loopfunction-times-out-the-page-cookies/#findComment-896304 Share on other sites More sharing options...
dink87522 Posted August 16, 2009 Author Share Posted August 16, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169898-solved-my-loopfunction-times-out-the-page-cookies/#findComment-899320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.