Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php using a cookie to do a task every 30 minutes

Posted on 2013-11-18
5
Medium Priority
?
231 Views
Last Modified: 2013-12-03
Looking for a code that allows me to do a small task (for example echo 'hello') every 30 minutes.  I do not have access to a server that has a 30 minute timeout time.

Note:
I want two concurrent tasks to be run using only one script.
not 30 minutes
not 30 minutes
around 30 minutes have passed
not 30 minutes
not 30 minutes
not 30 minutes
not 30 minutes
not 30 minutes
around 30 minutes have passed
not 30 minutes
not 30 minutes
not 30 minutes
around 30 minutes have passed


I want to use only a cookie (no database access).
0
Comment
Question by:rgb192
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39657029
I have no idea where a cookie would appear in this script.  And beware that even if you use flush() after the echo, you may not see any browser output at all until the script ends.  You do not have complete control over all of the server and browser behavior, and buffering may occur that will cause you some trouble.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39657140
This seems to test out OK.  I have not tried it at the 30-minute mark.

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);


// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28296976.html


// DO SOMETHING WITH A DATE/TIME FOR PROOF OF CONCEPT
echo date('c');

// SLEEP
longsleep(3);

// AND DO SOMETHING ELSE
echo date('c');

function longsleep($minutes)
{
    while ($minutes)
    {
        // PROBABLY THIS IS A SMALL-ENOUGH SLEEP VALUE
        sleep(20);
        sleep(20);
        sleep(20);
        $minutes--;
    }
}

Open in new window

0
 

Author Comment

by:rgb192
ID: 39679929
Is there a way to set the original time in the cookie

so I run a cron job (no php code)

and create an if statement or method (better)
that grabs cookie time

if cookie time > current time - 30 minutes

do task 1, set new cookie time using method
echo 'around 30 minutes have passed';

else
do task 2
echo 'not 30 minutes';


because I can not do the sleep due to server timeout restrictions
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39680815
There is no place in the design of a CRON job where a cookie makes sense.

The problem with the cookie is that it's external request data that is sent with the HTTP request from the client to the server.  A CRON job is started on the server by the server, without any request from the client.  So there's no practical design that involves a cookie.  It is quite feasible to use the server file system and your data base to store the same kind of information that you might put into a cookie.

When you set up CRON jobs you tell the server when to run them.  You do not have to guess about whether they were run at any given time.
0
 

Author Closing Comment

by:rgb192
ID: 39692590
okay I will think of a another design

thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question