65
716
Chapter 30 Building a Mailing List Manager
$result = $conn->query($query);
if (!$result)
return false;
if ($result->num_rows<1)
return false;
$row = $result->fetch_array();
if($row[0] == 1)
return ‘admin’;
else
return ‘normal’;
}
In previous login functions, you returned
true
if the login was successful and
false
if
it was not. In this case,you still return
false
if the login failed,but if it was successful,
you return the user type,either
‘admin’
or
‘normal’
.You check the user type by
retrieving the value stored in the
admin
column in the
subscribers
table,for a particu-
lar combination of email address and password.If no results are returned,you return
false
.If a user is an administrator,this value will be 1 (
true
),so you return
‘admin’
.
Otherwise,you return
‘normal’
.
Returning to the main line of execution,you register a session variable to keep track
of who the user is. She is either
admin_user
if she is an administrator or
normal_user
if
she is a regular user.Whichever one of these variables you set will contain the email
address of the user.To simplify checking for the email address of a user, you use the
get_email()
function mentioned earlier.This function is shown in Listing 30.6.
Listing 30.6 get_email()function from user_auth_fns.php— This Function
Returns the Email Address of the Logged-In User
function get_email()
{
if (isset($_SESSION[‘normal_user’]))
return $_SESSION[‘normal_user’];
if (isset($_SESSION[‘admin_user’]))
return $_SESSION[‘admin_user’];
return false;
}
Back in the main program, you report to the user whether she was logged in and at
what level.
The output from one login attempt is shown in Figure 30.6.
Listing 30.5 Continued