0

I have a piece of code on a website I am building that allows for a link to an administator page to be accessed when an admin logs in. I have this piece of code:

<?php
$arrayAdmin = array(6, 12);
if (in_array($_SESSION['userId'], $arrayAdmin)) 
{echo '<a href="./addstuff.php">Admin</a>';}
if (isset($_SESSION['userId'])) 
{echo '<p>You are logged in!</p>'; }
else {      }

?>

When a non admin account logs in it works and when an admin logs in it shows the link correctly but when no user is logged in it states:

Notice : Undefined index: userId in /home/mahs_rgeyer/www/index.php on line 63

If I remove the inarray no error message is shown, and if i remove just the isset the error message is shown. I have attempted to move the code around but to no avail. I just don't understand why it always shows this error message when no user is logged in but works exactly as intended when someone logs in.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – El_Vanja Nov 19 '20 at 18:58
  • While `$_SESSION` is a special variable, it pretty much behaves like a regular array. And of course it doesn't make much sense to verify whether a key exists after trying to read from it. – Álvaro González Nov 19 '20 at 19:05
  • Add isset to the 1st if header – adampweb Nov 19 '20 at 19:08

0 Answers0