Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.