Regex works on localhost but not on live website?

I’m trying to check some user input using preg_match and it’s working just fine with XAMPP but when I upload it on my website the code suddenly doesn’t work?

Here’s the code:

if (empty($firstName) || empty($lastName)) {
    header("Location: ../index.php?error=emptyFields&firstName=".$firstName."&lastName=".$lastName);
    exit();
  } else if (!preg_match("/^[a-zA-Z\s-åäöÅÄÖ]*$/", $firstName) || !preg_match("/^[a-zA-Z\s-åäöÅÄÖ]*$/", $lastName)) {
    header("Location: ../index.php?error=invalidChars");
    exit();
  } else { (code continues)

The first part (checking for empty fields) works just fine but the else if statement always gets run on infinityFree for some reason. I’m just trying to check if the user has input characters that shouldn’t belong in a name and I don’t understand why uploading it on my website breaks the check.

My guess would be that it has something to do with the usage of special characters in the second regular expression. Due to different encoding on the host OS, the webserver or the PHP server, coding actual special characters in programming code can lead to issues.

I can help you write a regex which achieves the same but is more portable, but I don’t really understand what the current regex is supposed to do.

2 Likes