You have to make use of the parts.length to determine if you have a middle name to set or not. The length -1 will be the last entry regardless of if a middle name exists.
These comparisons aren't right. This lets you use the first name and last name in any position in the line. You should probably be only checking the first entry in the line for the first name and the last entry in the line for the last. A simple && check between them is all that's required, you don't need to waste memory on additional variables.
header('HTTP/1.1 420 Enhance Your Calm');