How to refresh only a part of the script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

How to refresh only a part of the script

Post by dream2rule »

Hello All,

I have written a script that sends a mail to the administrator of the website at the end of the day. To do this i am checking whether system time is equal to the set time (this is the time at which i want to send the mail to the admin), and if the condition satisfies, i am sending the mail.

But i am stuck with this. To send a mail, it should check each time whether the system time equals set time or not, if it equals send a mail or else do not send a mail to the admin. For this, each time the page needs to be refreshed.

But if i refresh it each time, the index.php of the site also gets refreshed as the mailing script is contained in it. I do not want that to happen as this should not be known to the user. (hidden from the user).

So, can anyone help me out with this?

Here's my code

Code: Select all

<?php
      ob_start();
      //header("refresh:01;");
      require "Mail.php";
     
      // get the current timestamp into an array
      $timestamp = time();
      //echo strftime('%H:%M %A %d %b',$timestamp)."<br>";
      
      $date_time_array = getdate($timestamp);
      $hours = $date_time_array['hours'];
      $minutes = $date_time_array['minutes'];
      $seconds = $date_time_array['seconds'];
      $month = $date_time_array['mon'];
      $day = $date_time_array['mday'];
      $year = $date_time_array['year'];
       
      // use mktime to recreate the unix timestamp
      // adding 5 hours to $hours and 30 minutes to $minutes
      $systimestamp = mktime($hours + 5,$minutes+30,$seconds,$month,$day,$year);
      $systime=date("h:i:s A",$systimestamp);
      echo "SYS TIME - ".$systime."<br>";
      echo strftime('%H:%M %A %d %b',$timestamp)."<br>";
       
      $settimestamp=mktime(13,32,0,date("m"),date("d"),date("Y"));
      $settime=date("h:i:s A",$settimestamp);
      echo "SET TIME - ".$settime."<br>";
       
      //REFRESH HERE EACH SECOND TO CHECK WHETHER THE SYSTEM TIME EQUALS SET TIME
       
      if($systime==$settime)
      {
          //echo $systime."<br>";
          //echo $settime;
          $from = "Admin <some_name@some_site.com>";
          $to = "Admin <some_name@some_site.com>";
          $subject = "Test Mail";
          $body = "Hi,\n\nHow are you?";
        
          $host = ""; //smtp host address
          $username = "some_name@somesite.com";
          $password = "password";
         
          $headers = array ('From' => $from,  'To' => $to,  'Subject' => $subject);
          $smtp = Mail::factory('smtp',  array ('host' => $host,  'auth' => true, 'username' => $username, 'password' => $password));

          $mail = $smtp->send($to, $headers, $body);
         
          if (PEAR::isError($mail))
           {
               echo("<p>" . $mail->getMessage() . "</p>");
           }
           else
           {
              echo("<p>Message successfully sent!</p>");
           }
      }
      ?>

Regards,
Dream2rule
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Since you do not want the script to restart the probably you could use the JavaScript timer to call a function once a minute to check the time -- and then do whatever is needed when the time check succeeds.
(#10850)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Is this what you really want to do???? Every time some user asks for this page, you want your server to go into an endless loop, checking the time every second?? What happens when you have multiple users requesting the page?? Sounds like a bad idea to me. Have you considered a cron job that simply sends the email once every day? That would be the normal way to do it.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

@califdon

yes true, I too don't like the idea but again the system time that i get through php is static so to obtain the current time, i need to refresh the page each second.

Is Cron Job a better idea?

I have never worked on that before.. so can anyone here let me know how to go about it and also whether it runs on a windows environment or not?

That would be a great help.

Thanks and Regards,
Dream2rule
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Most of the windows hosts I have used have had some variant of Cron running, sometimes even with a web interface for it. I even managed to get one host to use Windows scheduled tasks to get the job done.

You just create your php file to do the task, set the script to run at a specific time and interval, and sit back and let it go.

Have a chat to your host and find out if you can set up Cron Jobs. Even if they want to set it up for you from their end, it's usually pretty painless.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Exactly, you have to use cronjob for this task.
This is example of use:

Code: Select all

Minute (0-59)  Hour (0-23)  Day of Month (1-31)  Month (1-12 or Jan-Dec)  Day of Week (0-6 or Sun-Sat)  Command 

      0             2                12                     *                          0,6                 /usr/bin/find
This line executes the "find" command at 2AM on the 12th of every month that a Sunday or Saturday falls on.

Start googling by crontab examples.
Post Reply