I don't know why this errors is popping out. This occur when I switch to PDO note: I'm still new to PDO.
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\AR NEW\includes\functions.inc.php on line 283
Warning: Trying to access array offset on value of type bool in C:\xampp\htdocs\AR NEW\includes\nav.php on line 29
This is the code for functions.inc.php line 283:
function isAdmin(){
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' ) {
return true;
}else{
return false;
}
}
and this is the code for nav.php line 29
<?php if (isset($_SESSION['user'])) : ?>
<style>nav .logedin{display:block}nav .navbar-animate li a{
padding: 5px 0 0 45px;}
</style>
<! --This is the line 29 --!><li class="username" ><p>Welcome, <?
php echo
$_SESSION['user']
['username']; ?>
</p></li>
<li><a class="logout" href="index.php?logout='1'">logout</a></li>
<?php elseif($page=='about'): ?>
<li><a href="signup.php" class="lgin">Signup</a></li>
<?php else: ?>
<li><a href="index.php" class="lgin">Login</a></li>
<?php endif ?>
Here's the whole code for reggister:
// register user if there are no errors in the form
if (count($errors) == 0) {
if (isset($_POST['user_type'])) {
$user_type = e($_POST['user_type']);
$query = $pdo->prepare("INSERT INTO accounts (username, email,
user_type, password) VALUES(:username,:email,:user_type
,:password)");
$query->execute(array(':username' =>$username,
':email' =>$email,
':user_type' =>$user_type,
':password' =>$hashedpwd, ));
$_SESSION['add'] = "Added successfully";
header('location: users.php');
exit(0);
}else{
$user_type = 'users';
$query = $pdo->prepare("INSERT INTO accounts (username, email,
user_type, password) VALUES(:username,:email,:user_type
,:password)");
$query->execute(array(':username' =>$username,
':email' =>$email,
':user_type' =>$user_type,
':password' =>$hashedpwd, ));
// get id of the created user
$logged_in_user_id = $pdo->lastInsertId();
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged
in user in session
$_SESSION['add'] = "You are now logged in and thank you!";
header('location: index.php');
exit(0);
}
}
}
This is for the login:
// LOGIN USER
function login(){
global $pdo, $errors, $hashedpwd;
// grab form values
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
// make sure form is filled properly
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// attempt login if no errors on form
if (count($errors) == 0) {
$query = $pdo->prepare("SELECT * FROM accounts WHERE email=:email
LIMIT 1");
$query->execute(array(':email'=>$email));
$row=$query->fetch();
if ($query->rowCount() > 0) { // check condition database if greater
than zero
$logged_in_user = $query->fetch();
if ($email==$row['email']) {
if (password_verify($password, $row['password'])) {
// check if user is admin or user
if ($logged_in_user['user_type'] == 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: admin/admin.php');
exit(0);
}else {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
exit(0);
}
}else{
array_push($errors, "Wrong password");
}
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
}