Jump to content

What Direction Should I take to Research This?


soma56

Recommended Posts

Let's say we have a script the does some echoing to the user through a loop. What happens if the users Internet connection goes down? Once it does come back up is there any way to resume or otherwise continue the script from where the user left off? If anyone has a specific page that I can read into this more (if it's even possible) then that would be great.

You could use sessions. When the user makes the page request you can start the looping process and use a sessions variable to store which iteration the loop is at. If there Internet connection drops, the session variable would stay the same until the user comes back.

 

This is a very very basic way of doing it:

 

<?php
session_start();

$start = isset($_SESSION['iteration']) ? $_SESSION['iteration'] : 0;

for ($i = $start; $i <= 200000; $i++) {
  echo $i;
  $_SESSION['iteration'] = $i;
}
?>

Actually, you might want to use cookies if you're worried about the internet connection going away.  Because usually, if the internet connection is gone for a user, you might as well assume they closed their browser too.

Well if that's the case let me give you guys a more specific example. Let's assume that the user is receiving mass amounts of data over a couple of hours to their browser but for my purposes we'll use this code:

 

<?php
$i = 0;

while ($i < 20){
    $i++;
echo $i;
ob_flush();
flush();
usleep (1000000);
}
?>

 

Should the connection become lost from the user to the server would it be more wise to use cookies or sessions?

Should the connection become lost from the user to the server would it be more wise to use cookies or sessions?

 

Cookies, because they stay on the client's computer; while Sessions stay on the server.  They're both pretty much the same thing.

 

 

Also,

It's pretty common to store a Session key into a cookie, which is probably what you're wanting to accomplish.... well, at least I think it's common; it might not be.  :shrug: But, it seems pretty logical.

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.