Pete's Log: iOS Won't Play Videos Without a Content-Length Header

Entry #2100, (Coding, Hacking, & CS stuff)
(posted when I was 43 years old.)

HTML embedded videos that worked fine in Firefox were showing the video controls in Safari on iOS, but not loading the video. The PHP code that serves up video files for Pete's Log effectively boils down to validating the request and then doing

header("Content-type: video/mp4"); readfile($video);

What was missing is the Content-Length header. It's easy enough to set (do this before the readfile):

$size = filesize($video); header("Content-length: $size");

The reason I output videos via PHP code instead of letting the web server just serve them is I make videos available only to "trusted" users. So I need to validate the session and check if the logged in user is a "trusted" user.

I discovered that the video I posted yesterday didn't work on my phone. After wasting too much time mucking around with codecs in ffmpeg, I finally thought to check the previous videos I've posted only to discover they don't work on my phone either. I'm 99% certain they did in the past. So armed with that knowledge, I started looking at other potential factors.

I realized I hadn't posted any videos since my HTTP header hardening, so I figured I should look at the headers I was setting and that's when I realized I wasn't setting Content-length. I added it and suddenly Safari was playing my videos again.

I don't frequently test browsers other than Firefox and Safari, so I'm not sure if this affected anything else. But like I said, I'm fairly certain this used to work. At least it works now...