I created an application that allows users to register for a service where they get assigned an email address as part of their registration/account setup. I wanted to allow the user to login to the application by using their newly created email address or simply by using the username that was used to generate that email address).
For example:
- User chooses to register as "johnsmith"
- Email account is automatically created by my application called "johnsmith@myapp.com"
- I want to allow user to login to my web app by typing either "johnsmith" and/or "johnsmith@myapp.com" (just like Gmail allows you to do).
Here is the code to add to the "Before Login" event of the Login page:
//Allow user to login whether he/she inputs username as email or simply using all characters to left of @ symbol in the email
//Ex: Username stored in database is "johnsmith", but you also want to allow user to login as "johnsmith@myapp.com" if he/she chooses to do so.
$temp_value = substr($username, 0, stripos($username, "@") ); //Strip username entered in login of all characters to the right of @ symbol (including the @ symbol itself).
$sql="select User_Password, User_Login, UserID, LinkGroupID from users_tbl where User_Login ='".$temp_value."' AND User_Password = '".$password."'"; //Extract info from database that matches that username
$rs=CustomQuery($sql);
$data=db_fetch_array($rs);
//Use IF statement to compare the data that was extracted from database (which would only have a result if username and password matched)
if ($data["User_Password"]==$password)
{
$_SESSION["UserID"] = $data["User_Login"]; //Set the $_SESSION variables to allow user to remain logged into the application
$_SESSION["GroupID"] = $data["LinkGroupID"];
header("Location: menu.php"); //redirect user beyond the login page if they successfully login!
}
Credit for pointing me in the right direction should go to Ann from support who provided me with some code that inspired this remodeld version I've just posted here.