如何为短信网关PHP示例脚本制作HTML输入框?
Got it, let's walk through how to build an HTML input form that works with your SMS gateway PHP script step by step.
Create HTML Input Form for SMS PHP Script
First, we'll build a simple, user-friendly HTML form that captures all the details your PHP script needs. We'll use the POST method (way more secure than GET for sensitive info like passwords):
<!DOCTYPE html> <html> <head> <title>SMS Sender Tool</title> </head> <body> <h2>Send SMS via Gateway</h2> <form action="send_sms.php" method="POST"> <div style="margin: 10px 0;"> <label for="username">Gateway Username:</label> <input type="text" id="username" name="username" required> </div> <div style="margin: 10px 0;"> <label for="password">Gateway Password:</label> <input type="password" id="password" name="password" required> </div> <div style="margin: 10px 0;"> <label for="sender">Sender ID:</label> <input type="text" id="sender" name="sender" required> </div> <div style="margin: 10px 0;"> <label for="to">Recipient Number(s):</label> <input type="text" id="to" name="to" placeholder="e.g., 1234567890,9876543210" required> </div> <div style="margin: 10px 0;"> <label for="message">SMS Message:</label> <textarea id="message" name="message" rows="5" cols="50" required></textarea> </div> <button type="submit">Send SMS</button> </form> </body> </html>
Quick Form Notes:
- The
requiredattribute makes sure users can't skip essential fields. - We use a
<textarea>for the message since it's better suited for longer text than a single-line input. - The
actionattribute points to your PHP script file (save the form assms_form.htmland the PHP assend_sms.phpto match this setup).
Update Your PHP Script to Handle Form Data
Next, we'll modify your original PHP script to pull data from the form instead of using hardcoded values. We'll also fix a small typo in your original URL and add basic error handling:
<?php // Only run the script if the form was submitted via POST if ($_SERVER["REQUEST_METHOD"] == "POST") { // Sanitize input to avoid issues with special characters $username = htmlspecialchars($_POST['username']); $password = htmlspecialchars($_POST['password']); $sender = htmlspecialchars($_POST['sender']); $to = htmlspecialchars($_POST['to']); $message = urlencode(htmlspecialchars($_POST['message'])); // URL-encode for the API $baseurl = "http://www.google.net/SMSC/API"; // Fixed the URL typo (you had a space and wrong parameter separator for the 'to' field) $url = "$baseurl/?action=compose&username=$username&password=$password&sender=$sender&to=$to&message=$message"; // Make the API call (suppress warnings temporarily with @) $response = @file($url); // Show feedback to the user if ($response) { echo "<h3>SMS Sent Successfully!</h3>"; echo "<p>Gateway Response:</p>"; echo "<pre>" . print_r($response, true) . "</pre>"; } else { echo "<h3>Error Sending SMS</h3>"; echo "<p>Couldn't connect to the SMS gateway. Double-check your credentials or try again later.</p>"; } } else { // If someone tries to access the PHP script directly echo "<p>Please use the <a href='sms_form.html'>SMS Form</a> to send messages.</p>"; } ?>
Key Improvements:
- Input Sanitization:
htmlspecialchars()prevents cross-site scripting (XSS) attacks and ensures special characters don't break the script. - URL Fix: Corrected the typo in your original URL (you had
$to=$toinstead of&to=$to, plus an extra space). - User Feedback: Clear success/error messages help users understand what happened.
Critical Security Tips
- Don't let users input gateway credentials: For production use, hardcode your username/password in the PHP script (store it outside the web root if possible) instead of letting users enter them.
- Use HTTPS: If your SMS gateway supports it, switch
http://tohttps://inbaseurlto encrypt data between your server and the gateway. - Validate phone numbers: Add client-side or server-side checks to make sure recipient numbers are in the correct format.
内容的提问来源于stack exchange,提问作者umer khan
相关产品推荐
相关产品推荐

