Question

I need your help to run following php script automatically on 05th day of the every month. Currently I run this manually. Is this possible using schedule task? Also I don't have admin access to the web server. Therefore cannot use third party automation tools.

PS: Is it possible to schedule this in a local machine with windows 7 xampp environment and then later update remote db?

session_start();

if($_SESSION['log'] != "log" || $_SESSION['type'] != "***"){
    header("Location: ***.php");
}

require_once('conf.php');

date_default_timezone_set('Asia/Kolkata');
$date = date("j"); //get current date
$today = date("d-m-Y");  
$now = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm

if ($date === 5){
    //get interest rate
    $sql = mysql_query("SELECT Rate FROM Interest WHERE Ref = 'Loan' ORDER BY Date DESC LIMIT 1");
    $r = mysql_fetch_assoc($sql);
    $rate = $r['Rate'];

    //check for not settled loans
    $check = mysql_query("SELECT LID FROM Registry WHERE Status = 0");
    if (mysql_num_rows($check) > 0){
        while ($list = mysql_fetch_assoc($check)) {     // while there are rows to be fetched...
        //*** Start Transaction ***//  
            mysql_query("BEGIN");  

            $loanID = $list['LID'];

            //get loan data
            $sql = mysql_query("SELECT Registry.Amount, SUM(Account.Total) AS Paid, SUM(Account.Interest) AS intPaid FROM Registry LEFT JOIN Account ON Registry.LID = Account.LID WHERE Registry.Status = 0 AND Account.Auto = 0 AND (Account.LID = '$loanID') GROUP BY Account.LID");
            $r = mysql_fetch_assoc($sql);
            $amount = $r['Amount']; //loan amount
            $paid = $r['Paid'];     //sum of paid
            $intPaid = $r['intPaid'];   //sum of interest paid

            //get sum of monthly automatically updated interest
            $sql = mysql_query("SELECT SUM(Interest) AS Interest FROM Account WHERE Payment = 0 AND Auto = 1 AND LID = '$loanID'");
            $r = mysql_fetch_assoc($sql);
            $autoInt = $r['Interest'];  

            $total = ($amount + $autoInt);  //total to be paid
            $balance = ($total - $paid);    //with no interest

            if ($paid >= $balance){
//              echo "Loan completed <br/>";
            }else{
                $int = ($balance * $rate) / 100;
                $update = mysql_query("INSERT INTO Account (LID, Date, Interest, Total, Auto) VALUES ('$loanID', NOW(), '$int', '$int', 1)") or die(mysql_error());

                if (! $update){
                //*** RollBack Transaction ***//  
                mysql_query("ROLLBACK");  
//                  $_SESSION['error'] = "Interest saving failed.!";
                    echo "Loan ID: " . $loanID . ", Interest: Rs. " . $int . "/= - Update Failed.!<br>";
                }else{
                //*** Commit Transaction ***//  
                    mysql_query("COMMIT");  
//                  $_SESSION['error'] = "Interest saved successfully";
                    echo "Loan ID: " . $loanID . ", Interest: Rs. " . $int . "/= - Update Succeeded.!<br>";
                }
            }
        }
    }
}else{
    echo "Monthly Interest can be update only by 05th day of the month.!";
}
Était-ce utile?

La solution

I dont think at needs admin permissions

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/at.mspx?mfr=true

EDIT:

or even try MySQL Events

http://dev.mysql.com/doc/refman/5.1/en/events.html

It looks all database work which should be easily do able

Autres conseils

If it is a publicly accesseable web server, you can use a cronjob service like https://www.cronjobservice.net/. After registration you enter an url and the time they shall call it. I advice using multiple of those services for the case one fails. (your script must execute on first call and exit on subsequent calls from other services.)

Please be sure to properly deal with .htaccess and make sure to have no security relevant output.

Have you asked the peoples that are hosting your site if they have a solution? Most hosting companies have solutions for this (even on Windows servers)

If the hosting company has no solution for this (what I seriously doubt) you can call the script externally from another server that has cron possibilities or have it called by the first visitor on that day.

I any case you will want to include a check in your script that your script only runs on the set date (you already have that) and also make sure that calling the script more than once on that day is no problem.

PS : if the script is called by the first visitor you will have to consider the fact that you could not have any visitor that particular day, so the script will have to run whenever the next visitor comes along.

If you don't have administrative access to Windows, you won't be able to create a scheduled task. Your only alternative is to create some sort of custom task component in your application that checks / stores the last time a script was run. The problem with this is that it relies on user activity and it may not run on the exact date that you want it to.

The other alternative is to find Linux hosting so that you can avail of Cron.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top