Jump to content

How to retain value of variable in the event of Internet loss


soma56

Recommended Posts

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.

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.

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.  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.