Solved

php using a cookie to do a task every 30 minutes

Posted on 2013-11-18
5
201 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 108

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 108

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 108

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now