Current Location: Home> Latest Articles> Complete Guide to Implementing SMS Verification and Login Logs with PHP

Complete Guide to Implementing SMS Verification and Login Logs with PHP

M66 2025-07-09

Overview of Implementing SMS Verification and Login Logs in PHP

In modern web systems, enhancing account security is crucial. Many platforms now incorporate SMS verification during registration and login processes, along with login log tracking to monitor user behavior. This article provides practical PHP examples to implement these common features.

Implementing SMS Verification

SMS verification is a widely used two-factor authentication method. Users receive a verification code via SMS and input it for validation. Here's a PHP example of how to send such a code:

function sendSMS($phoneNumber, $code) {
    // Use an SMS platform API to send the code
    $apiUrl = 'http://smsapi.example.com/send_sms';
    $postData = array(
        'phone' => $phoneNumber,
        'code' => $code
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

// Generate a random 4-digit code
$code = rand(1000, 9999);

// User's phone number
$phoneNumber = '13800138000';

// Send the code
$response = sendSMS($phoneNumber, $code);

// Handle the response
if ($response === 'success') {
    echo 'The verification code has been sent to your phone.';
} else {
    echo 'Failed to send the code. Please try again later.';
}

This code uses a third-party SMS API to send the verification code. Developers can customize it according to the specifications of their chosen SMS platform.

Recording Login Logs

To improve system security and trace user login activity, logging login attempts is essential. Below is a PHP function to log login events:

function writeLoginLog($userId, $loginStatus) {
    $logFile = 'login.log';
    $loginTime = date('Y-m-d H:i:s');
    $ipAddress = $_SERVER['REMOTE_ADDR'];

    $logMessage = "{$loginTime}    {$userId}    {$ipAddress}    {$loginStatus}\n";
    file_put_contents($logFile, $logMessage, FILE_APPEND);
}

// Call after successful login
$userId = 123456;
$loginStatus = 'SUCCESS';
writeLoginLog($userId, $loginStatus);

This function writes each login attempt to a log file, including the timestamp, user ID, IP address, and status. You can adapt this to store data in a database for better analysis and auditing.

Conclusion

In this guide, we demonstrated how to use PHP to implement SMS verification and login log tracking. SMS codes can enhance user authentication security, while login logs provide a trail for monitoring and analysis. These features not only improve user experience but also help maintain platform integrity. Developers can expand on this foundation with additional features like code expiration, retry limits, or log visualization tools based on specific project needs.