I have some hardcoded if/else statements to set $page variable – (for later use in header: “.page”) – to a given website based on the $_POST[“username”] input.
CODE:
if ($_POST["username"] == "username1@domain1.com") {
$page = "http://www.google.com";
}
else if ($_POST["username"] == "username2@domain1.com"){
$page = "http://www.yahoo.com";
}
else if ($_POST["username"] == "username1@domain2.com"){
$page = "http://www.stackoverflow.com";
}
else if ($_POST["username"] == "username2@domain2.com"){
$page = "http://www.serverfault.com";
}
else if ($_POST["username"] == "username@domain3.com"){
$page = "http://www.superuser.com";
}
else if (!preg_match($domain2.com, $_POST["username"])) { //THIS IS VERY WRONG
$page = "http://www.backblaze.com";
}
else{
$page = "DefaultBackupPage.php";
}
I am trying to say if your username has “@domain.com” at the end of it, set the $page to, in this case backblaze.com, but could be anything.
I am aware it is messy, I don’t actually like this implementation. It just has to fit in this schema and I need a quick fix!
The current error I am receiving is that the regular expression is empty. I am hoping this is a no brainer for someone who knows PHP – which I have been hastily trying to learn!
will catch username ending with
domain2.com. Please note the escaping backward slash for the dot. If you want to test against the opposite (aka. does NOT end withdomain2.com) then use an exclamation mark before thepreg_match()function.Is this what you were asking?
EDIT 1: I added the
iflag to the pattern to make it look for a case-insensitive match.EDIT 2: For the sake of readability and control I would wrap this in a function instead, but that’s my own preference so it’s not the suggested approach or anything. In case your code is long and complicated…