Solved

php using a cookie to do a task every 30 minutes

Posted on 2013-11-18
5
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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 110

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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