Introduction
Cron jobs are scheduled tasks that run automatically at specified intervals on your hosting server. This guide will help you set up your token reset PHP script as a cron job in Hostinger's control panel.
Note: Your PHP script resets all users' tokens to 0 in the database. Setting it up as a cron job will allow it to run automatically at regular intervals.
Step-by-Step Guide
Step 1: Access Hostinger Control Panel
Log in to your Hostinger account and navigate to the hPanel dashboard.
Step 2: Locate Cron Jobs Section
In your hPanel, look for the Advanced section and click on Cron Jobs.
Step 3: Create a New Cron Job
Click on "Create Cron Job" or similar button to set up a new scheduled task.
Step 4: Configure Cron Job Settings
Set up your cron job with the following parameters:
/usr/bin/php /home/u397706344/domains/yourdomain.com/public_html/reset_tokens.php
Set the schedule according to your needs. For example, to run daily at midnight:
0 0 * * *
Common schedule examples:
- Daily at midnight: 0 0 * * *
- Every Sunday at 2 AM: 0 2 * * 0
- Every 6 hours: 0 */6 * * *
- Every minute (for testing): * * * * *
Step 5: Save the Cron Job
Click the "Create" or "Save" button to activate your cron job.
PHP Script Recommendations
Here's an improved version of your PHP script with better error handling and logging:
// Database connection parameters
$host = 'localhost';
$dbname = 'u397706344_broai';
$username = 'u397706344_broai';
$password = 'Talib@123#2';
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
error_log('[' . date('Y-m-d H:i:s') . '] Database connection failed: ' . $conn->connect_error);
exit();
}
try {
// Update all users' tokens to 0
$sql = "UPDATE users SET tokens = 0, last_updated = NOW()";
$stmt = $conn->prepare($sql);
if (!$stmt) {
throw new Exception("Prepare failed: " . $conn->error);
}
if (!$stmt->execute()) {
throw new Exception("Execute failed: " . $stmt->error);
}
// Check if any row was affected
if ($stmt->affected_rows > 0) {
error_log('[' . date('Y-m-d H:i:s') . '] Tokens reset to 0 for ' . $stmt->affected_rows . ' users');
} else {
error_log('[' . date('Y-m-d H:i:s') . '] No users found or no changes made');
}
$stmt->close();
} catch (Exception $e) {
error_log('[' . date('Y-m-d H:i:s') . '] Error resetting tokens: ' . $e->getMessage());
}
// Close connection
$conn->close();
echo "Token reset process completed at " . date('Y-m-d H:i:s');
?>
Important: Make sure to save this script as a PHP file (e.g., reset_tokens.php) in your public_html directory or another appropriate location.
Troubleshooting
Common Issues:
- Cron job not running at all: Check the command path and permissions
- Database connection errors: Verify your database credentials
- Script not found: Ensure the path to your PHP script is correct
How to Test Your Cron Job
To verify your cron job is working:
- Set the schedule to run every minute temporarily:
* * * * *
- Check your error logs for any messages
- Manually run the script from SSH if you have access:
php /path/to/your/script.php
- Check your database to see if tokens were reset
Tip: Add email notifications to your cron job to receive output:
/usr/bin/php /path/to/script.php >> /path/to/logfile.log 2>&1
Need More Help?
If you're still experiencing issues with your cron job:
- Contact Hostinger support for assistance with cron job setup
- Check Hostinger's knowledge base for updated instructions
- Verify your PHP and database connection details