soma56 Posted July 13, 2010 Share Posted July 13, 2010 I've been researching the use of cookies for a script after discovering that it's the right way to go (http://www.phpfreaks.com/forums/index.php/topic,303849.0.html). I've created this very simple example to highlight what I'm trying to accomplish. <?PHP while ($i <= 100){ if (!isset($_COOKIE["name"])) { $i = 0; setcookie("name", $i, time()+3600); } else { $i++; echo $i // wait for 1 second usleep(1000000); } ?> In the event that the users internet goes down during the script execution the value of $i will be maintained (I hope anyways, I haven't tested it). In some cases my script may run for over a couple of hours at a time. Am I taking the right approach towards retaining the value of a variable in the event that there is a loss of an Internet connection. Link to comment https://forums.phpfreaks.com/topic/207614-how-to-retain-value-of-variable-in-the-event-of-internet-loss/ Share on other sites More sharing options...
Mchl Posted July 13, 2010 Share Posted July 13, 2010 Running script for hours in browser is not a good idea. Something will almost certainly time out sooner or later. Link to comment https://forums.phpfreaks.com/topic/207614-how-to-retain-value-of-variable-in-the-event-of-internet-loss/#findComment-1085425 Share on other sites More sharing options...
soma56 Posted July 13, 2010 Author Share Posted July 13, 2010 Fair enough. But that's only in rare cases. I'm receiving a "Cannot modify header information - headers already sent" error which I have yet to research. I think i've gotten a little further since my first post though. <?PHP if (!isset($_COOKIE['name'])) { $c = 0; setcookie('name', $c, time()+3600); } ?> <?PHP while ($i <= 14){ //echoing lots of different code if (isset($_COOKIE['name'])) { $c = $c +1; setcookie('name', $c, time()+3600); } //echoing more code //end while } ?> I'm trying to figure out a way update the value of a cookie while php is in a loop. Link to comment https://forums.phpfreaks.com/topic/207614-how-to-retain-value-of-variable-in-the-event-of-internet-loss/#findComment-1085474 Share on other sites More sharing options...
Mchl Posted July 13, 2010 Share Posted July 13, 2010 I don't think that'll work... What is your script supposed to do anyway? Link to comment https://forums.phpfreaks.com/topic/207614-how-to-retain-value-of-variable-in-the-event-of-internet-loss/#findComment-1085476 Share on other sites More sharing options...
soma56 Posted July 13, 2010 Author Share Posted July 13, 2010 Hmm, I was told Cookies were the way to go but I took a step back and looks like sessions is what I need. I did some testing and discovered that if the page is stopped (or otherwise the Internet is temporarily dropped) it will pick-up again where it left off. <?php session_start(); if(!isset($_SESSION['test'])) $_SESSION['test'] = $_SESSION['test'] = 0; ?> <?PHP while ($_SESSION['test'] <= 150){ $_SESSION['test'] = $_SESSION['test'] + 1; echo "Total Sessions = ". $_SESSION['test']; ob_flush(); flush(); usleep(1000000); //end while } ?> Thanks. Link to comment https://forums.phpfreaks.com/topic/207614-how-to-retain-value-of-variable-in-the-event-of-internet-loss/#findComment-1085488 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.