Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php using a cookie to do a task every 30 minutes

Posted on 2013-11-18
5
Medium Priority
?
229 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 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

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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.

721 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