Hi, I don't know much about PHP. I have a program that I bought that requires a cron job to run it. I have been running it at 3am every day, but I don't want it to run at exactly 3am. I want to add a random time feature that will delay the execution of the php script for up to 30 minutes.

I combined a couple of scripts to make a random.php file, which if I type the URL directly into my browser, works fine. When I set the cron job to run the random.php file, it doesn't seem to work.

Here is the code:

<?php
srand(time());
$random = (rand()%5);
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="refresh"
content="<?=$random;?>;URL=http://www.mywebsite.com/blog/post.php">
</head>

<body bgcolor="#FFFFFF" text="#000000">

</body>
</html>

Any ideas as to how to execute the post.php with the cron job and create a random post time?

Thanks.

    if you want to delay it a random amount of time, i would have the script post.php just sleep a random amount of time on the startup.

    i.e.

    <?php
    
    $sleeptime = rand(1, 30) * 60;
    sleep($sleeptime);
    
    //rest of post.php script here
    ?>
    

      Wow, what an amazingly simple answer to what I thought was a complex problem. Thanks, I will try it and let you know how it goes.

        Drew,

        Unfortunately that did not work for me. The script is encoded, so when I added the sleep function, it just stopped working. I guess I am back at square one. I need a seperate page to point the cron job to that will delay then call the post.php script.

        Any ideas how I can accomplish this?

          ok try this out. have the cronjob call a script called mypost.php
          in mypost put this:

          <?php
          
          $sleeptime = rand(1,30) * 60;
          sleep($sleeptime);
          
          include 'post.php';  //call up the real post script after sleeping
          
          ?>
          

          see if that does the trick
          for debugging you could just set the sleep time to something like 5 or 10 seconds so you can ensure its sleeping, but not have to wait a long time.

            Drew, you are the man!

            It works like a charm. Thanks so much!

              Drew, your idea for the sleep function worked like a charm. Now I have another problem I hope you can help me with.

              I am trying to run a query from a variable passed from the URL and get this error:
              Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

              This is the page I am sending from.
              http://www.barstool-directory.com/test/cat.php

              <?php
              $login = "netshops";
              $password = "feedme";
              $table = "hammocks";
              $aid = "cd1";
              $database = "netshopsaffiliates_com_-_datafeeds";
              $connection = mysql_connect("207.234.208.224","$login","$password")
              	or die ("Unable to connect.");
              mysql_select_db($database)
              	or die ("Unable to open database.");
              $sql = "SELECT DISTINCT category FROM $table ORDER BY category";
              $result = mysql_query($sql);
              $row = mysql_fetch_array($result);
              while ($row = mysql_fetch_array($result))
              	{
              		extract($row);
              		echo "<table width='600' border='0' cellspacing='0' cellpadding='3'> \n";
              		echo "<tr>\n";
              		echo "<td valign='top'>\n<a href='list.php?cat=$category'> \n<b> $category </b> </a> \n<br>\n";
              		echo "</tr>\n";
              		echo "</table>\n";
              	}
              mysql_close($connection);
              ?>
              

              This is where I am trying to send it to:
              http://www.barstool-directory.com/test/list.php

              <?php
              $login = "netshops";
              $password = "feedme";
              $table = "hammocks";
              $aid = "cd1";
              $database = "netshopsaffiliates_com_-_datafeeds";
              $connection = mysql_connect("207.234.208.224","$login","$password")
              	or die ("Unable to connect.");
              mysql_select_db($database)
              	or die ("Unable to open database.");
              
              $sql = "SELECT * FROM $table WHERE category = <?php echo $_REQUEST['cat']?>"; 
              $result = mysql_query($sql);
              $row = mysql_fetch_array($result);
              while ($row = mysql_fetch_array($result))
              	{
              		extract($row);
              		echo "<table width='600' border='0' cellspacing='0' cellpadding='3'> \n";
              		echo "<tr>\n";
              		echo "<td valign='top'>\n<a href='$link$aid'> <img src='$image' border=0> </a> \n <br> <b> Price: </b> $$price\n </td> \n";
              		echo "<td valign='top'>\n<a href='$link$aid'> \n<b> $product </b> </a> \n<br> $description\n <br></td>\n";
              		echo "</tr>\n";
              		echo "</table>\n";
              		echo "<hr size='1'>\n";
              	}
              mysql_close($connection);
              ?>
              

              Could you help me one last time?

              Thanks,
              Thomas

                im guessing that error is coming from this
                $sql = "SELECT FROM $table WHERE category = <?php echo $REQUEST['cat']?>";
                you are already in <?php ?> tags so you cant have them again.
                try
                $sql = "SELECT
                FROM $table WHERE category = '{$REQUEST['cat']}'";

                  Drew, you've been great. I don't want to push my luck, this is the last question for you.

                  If you go to http://www.barstool-directory.com/test/cat.php you will see how ugly the category listing is.

                  Is there an easy way that I can turn:

                  Home > Accessories > Accomplish Nothing Gear
                  Home > Accessories > Hammock Add-Ons
                  Home > Accessories > Hammock Blankets
                  Home > Accessories > Hammock Pillows
                  Home > Accessories > Hanging Kits
                  Home > Accessories > Relaxation Essentials

                  into:

                  Accessories

                  click 'Accessories' and get:

                  Accomplish Nothing Gear
                  Hammock Add-Ons
                  Hammock Blankets
                  Hammock Pillows
                  Hanging Kits
                  Relaxation Essentials

                  Thanks,
                  Thomas

                    Write a Reply...