Reputation:
I have built a simple CAPTCHA PHP script and now I am trying to create a script that generates a mp3 audio for people who can't see well or can't see at all. My goal is only to challenge myself, so I prefer not to use a external library, but I would appreciate some help. I tried using file_put_contents() function to merge the audios (I have one mp3 audio for each letter) and for my surprise, it worked, but it seemed incorrect to me to just join audio files like I would join text files. So my second attempt is to do like so:
<?php
session_start();
$letter_array = str_split($_SESSION["captcha"]); //this is an array with the letters of the captcha
header('Content-Type: audio/mpeg');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Expires: 0');
$audio = '';
foreach ($letter_array as $letter) {
$audio .= file_get_contents("audios/".$letter.".mp3");
}
echo $audio;
?>
and then I can embed this file to an tag in HTML and it also works. I know that essentially both of the solutions are similar, but I find this second way more concise. My question is: although it works fine, is it OK to do such thing this manner, or is it wrong, or not recommended? I don't know much encoding and about the structure of a mp3 file, so I dont' know if this solution would work for everyone. I am also open to suggestions
Thanks
Upvotes: 3
Views: 225
Reputation: 163438
Mostly, yes.
Regular MP3 files are just a series of MPEG frames. You can safely concatenate them as long as they have the same sample rate and channel count. You can mix and match bitrates.
There are some caveats though. Firstly, it's common for MP3 files to have ID3 tags attached to them. You're going to want to throw out those tags since they're expected to be in certain locations.
Also, it's important to understand the bit reservoir. This isn't an issue for you since you're concatenating fully encoded files, but if you ever have to split a stream, it's important to note that data from one from can be placed into other frames. This is to allow for using more bandwidth on more complicated frames, and less where it isn't needed.
At the end of the day though, you really shouldn't build your own CAPTCHA. It's trivial for someone to write maybe 5 lines of code to match your audio samples and pick out the numbers. Not to mention, you're potentially creating accessibility issues. I highly recommend checking out Google reCAPTCHA. Current versions are non-intrusive to your users, and are potentially much more secure than what you're proposing. https://www.google.com/recaptcha/intro/v3.html
Upvotes: 1