Skylight_lady Posted January 15, 2011 Share Posted January 15, 2011 Hi, guys. I'm have a website setup on my localhost. It's not yet LIVE. However, i'm setting up a cron job to work with the code below. Will this code work fine? $sql = "SELECT * FROM users WHERE subscription <> 0"; $result = mysql_query($sql); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)){ $subscription = $row['subscription'] - 1; $sql = "UPDATE users SET subscription = '$subscription' WHERE subscription <> 0"; $result=mysql_query($sql) or die(mysql_error()); } } $query = "SELECT * FROM users WHERE subscription = 0"; $update = mysql_query($query); if(mysql_num_rows($update)) { while($row1 = mysql_fetch_assoc($update)){ $subscription = 0; $query = "UPDATE users SET disable = 1 WHERE subscription = '$subscription'"; $update=mysql_query($query) or die(mysql_error()); } } Also, how will i have to write the cron job to meet the needs of this script and update it at 12am every day? Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/ Share on other sites More sharing options...
phpfreak Posted January 15, 2011 Share Posted January 15, 2011 I don't see any reason this code would not work on a cronjob... you should call PHP first then the script: /usr/local/bin/php /path/to/phpfile.php Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1159872 Share on other sites More sharing options...
Skylight_lady Posted January 15, 2011 Author Share Posted January 15, 2011 Thanks, what do you mean by call php first then the script? The script i showed is the full php code for that page. Does it require addition code for a cron job? Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1159879 Share on other sites More sharing options...
phpfreak Posted January 15, 2011 Share Posted January 15, 2011 Unless your system knows to handle PHP files with the php binary on your system, you would need to define that in the cronjob. Can you run this manually from the command line? Or are running it using wget or some other method of accessing it through the web browser? Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1159890 Share on other sites More sharing options...
Skylight_lady Posted January 15, 2011 Author Share Posted January 15, 2011 Actually, i just tested that the easy way without a cron job. If i set some users with the variable's to the below table as its set: name subscription test1 30 test2 20 test3 1 test4 0 and run that php file by clicking it on my web browser then all users "subscription" would be set as "29" and users already set to "0" will stay as "0". Sounds like its over writing all numbers thats not "0" to use the highest number. WEIRD. However, it doesn't disable any users thats on "1" as it should from that code. Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1159918 Share on other sites More sharing options...
Skylight_lady Posted January 16, 2011 Author Share Posted January 16, 2011 No worries. I sorted it. The following code: $subscription = $row['subscription'] - 1; $sql = "UPDATE users SET subscription = '$subscription' WHERE subscription <> 0"; Had to be changed to: $sql = "UPDATE users SET subscription = subscription - 1 WHERE subscription <> 0"; Which obviously makes sence. Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1160031 Share on other sites More sharing options...
Skylight_lady Posted January 16, 2011 Author Share Posted January 16, 2011 One more question. Is it possible to secure a cron jobs file? So nobody can run the job by accessing the file and only the cron job can? Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1160045 Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 If you mean via the web you can put the file outside your public_html or www web directories and just point the cron to that path instead Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1160049 Share on other sites More sharing options...
Skylight_lady Posted January 16, 2011 Author Share Posted January 16, 2011 Perfect. Thank you very much. Sounds just what i need. Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1160054 Share on other sites More sharing options...
phpfreak Posted January 16, 2011 Share Posted January 16, 2011 If you mean via the web you can put the file outside your public_html or www web directories and just point the cron to that path instead I agree with dragon_sa on that - if you don't want it executed via the browser then it's best to move it outside the document root. Alternatively, if you're running the script via CLI / crontab and need to have it in your document root you can check $_SERVER['HTTP_REQUEST'] - if that exists then it's being called via apache and you can simply bail out with an exit(); if($_SERVER['HTTP_REQUEST']) { exit(); } I believe that should prevent it from being run in the browser. Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1160056 Share on other sites More sharing options...
Skylight_lady Posted January 16, 2011 Author Share Posted January 16, 2011 Thank you. Thats 2 ways. Quote Link to comment https://forums.phpfreaks.com/topic/224544-will-this-php-code-work-on-a-cron-job/#findComment-1160065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.