为运行中的PHP批量短信脚本添加Resume/Pause/Stop网页控制界面咨询
Hey there! Let's figure out how to add pause/play/stop controls to your bulk SMS PHP script. The key here is that since PHP runs synchronously by default, we need a way to track the script's state externally and have the sending loop check that state periodically. Here's a practical implementation:
Core Concept
We'll use state marker files (or a database/Redis if you prefer) to signal the running script to pause, resume, or stop. The sending loop will regularly check these markers and adjust its behavior accordingly.
Step 1: Add State Tracking with Marker Files
We'll create three simple marker files to track the script's state:
pause.lock: Exists when the script should pausestop.lock: Exists when the script should terminate- No lock files: The script runs normally
Step 2: Modify Your sendsms.php Script
Update your sending loop to check these marker files on each iteration:
<?php // Clean up old lock files on script start (optional but recommended) foreach (['pause.lock', 'stop.lock'] as $file) { if (file_exists($file)) unlink($file); } // Your existing logic to get phone numbers $phoneNumbers = get1MPhoneNumbers(); // Replace with your actual number retrieval $message = $_POST['message']; // From your original form foreach ($phoneNumbers as $index => $number) { // Check if stop is requested if (file_exists('stop.lock')) { unlink('stop.lock'); // Clean up marker exit("SMS sending stopped by user. Processed {$index} numbers."); } // Check if pause is requested: wait until resume or stop while (file_exists('pause.lock')) { if (file_exists('stop.lock')) { unlink('stop.lock'); unlink('pause.lock'); exit("SMS sending stopped during pause. Processed {$index} numbers."); } sleep(2); // Wait 2 seconds before checking again (reduces resource usage) } // Execute your SMS sending logic if (sendSMS($number, $message)) { // Optional: Log successful send or update progress file_put_contents('progress.log', "Sent to {$number}\n", FILE_APPEND); } // Optional: Add a small delay to avoid hitting SMS platform rate limits sleep(1); } // Clean up on completion if (file_exists('pause.lock')) unlink('pause.lock'); echo "SMS sending completed successfully!"; ?>
Step 3: Build the Control Interface
Create an HTML page with buttons to trigger pause/play/stop actions, using AJAX to avoid page reloads:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Bulk SMS Controller</title> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> </head> <body> <h1>Bulk SMS Sender Controls</h1> <div style="margin: 20px 0;"> <button id="resumeBtn" style="padding: 10px 20px;">Play/Resume</button> <button id="pauseBtn" style="padding: 10px 20px; margin-left: 10px;">Pause</button> <button id="stopBtn" style="padding: 10px 20px; margin-left: 10px;">Stop</button> </div> <div id="status" style="margin-top: 20px; padding: 10px; border: 1px solid #ccc;"></div> <script> // Handle Resume/Play $('#resumeBtn').click(function() { $.post('control.php', { action: 'resume' }, function(response) { $('#status').text(response); }); }); // Handle Pause $('#pauseBtn').click(function() { $.post('control.php', { action: 'pause' }, function(response) { $('#status').text(response); }); }); // Handle Stop $('#stopBtn').click(function() { $.post('control.php', { action: 'stop' }, function(response) { $('#status').text(response); }); }); // Optional: Auto-refresh status (e.g., check progress) setInterval(function() { $.get('progress.php', function(data) { // Update progress display if you implement progress tracking }); }, 5000); </script> </body> </html>
Step 4: Create the Control Handler (control.php)
This script processes AJAX requests to create/remove marker files:
<?php if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo "Invalid request method."; exit; } $action = $_POST['action'] ?? ''; switch ($action) { case 'pause': if (!file_exists('pause.lock')) { file_put_contents('pause.lock', ''); echo "SMS sending paused successfully."; } else { echo "Sending is already paused."; } break; case 'resume': if (file_exists('pause.lock')) { unlink('pause.lock'); echo "SMS sending resumed successfully."; } else { echo "Sending is already running."; } break; case 'stop': file_put_contents('stop.lock', ''); if (file_exists('pause.lock')) unlink('pause.lock'); echo "Stop request sent. Sending will terminate shortly."; break; default: http_response_code(400); echo "Invalid action."; break; } ?>
Key Considerations
- File Permissions: Ensure your PHP process has read/write access to the directory where marker files are stored. If you get permission errors, adjust the directory's chmod or ownership.
- Alternative State Stores: For more reliability (especially in multi-server setups), replace marker files with a Redis key-value store or a database table. For example, use Redis
SETto mark pause/stop andDELto resume. - Progress Tracking: Extend the script to write progress (e.g., number of sent messages) to a file or database, then add a progress bar to the control interface.
- Timeouts: If your script runs longer than PHP's
max_execution_time, you'll need to increase this setting inphp.inior useset_time_limit(0);at the start ofsendsms.php.
内容的提问来源于stack exchange,提问作者Salah Ali

