Solved

php using a cookie to do a task every 30 minutes

Posted on 2013-11-18
5
209 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 109

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 109

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 109

Accepted Solution

by:
Ray Paseur earned 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHPStorm debugging issues 1 45
Advice and best practice  SQLite3 Database using PHP 2 47
Loop through multiple arrays 13 28
How do I fix this UPDATE error? 7 22
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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