Jump to content

Will this php code work on a cron job?


Skylight_lady

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.