Austin Hardesty Austin Hardesty - 1 month ago 4
PHP Question

Undefined index: error in php

I've been getting an unidentified index error on one of the files. I use XAMPP to run locally and am working on this site with another developer who uses MAMP on a MAC who does not receive this error. I assume the code is wrong and I am supposed to see this error and that it does not on his because of different php permissions on mamp, but do not know where to start in resolving it. Keep in mind I am still very new to php.

I am getting this error:

Notice: Undefined index: uidentifier in C:\xampp\htdocs\boone-web\templates\basic\index.php on line 38

Here is what it's referring to:

<ul>
<?php if($_SESSION['uidentifier'] == 1) : ?>
<li><a href="<?php echo BASE; ?>">Home</a></li>
<li><a href="classJournals.php">Student Journals</a></li>
<li><a href="logout.php">Logout</a></li>

<?php else : ?>
<li><a href="<?php echo BASE; ?>">Home</a></li>
<li><a href="profile.php">My Profile</a></li>
<li><a href="map.php">Game Map</a></li>
<li><a href="journal.php">MyJournal</a></li>
<li><a href="highScores.php">High Scores</a></li>
<li><a href="logout.php">Logout</a></li>
<?php endif; ?>
</ul>

Answer

First is just a warning message...you should check for the $_SESSION['uidentifier'] first:

<ul>
   <?php if(isset($_SESSION['uidentifier'])) : ?> 
   <?php if($_SESSION['uidentifier'] == 1) : ?>
      <li><a href="<?php echo BASE; ?>">Home</a></li>
      <li><a href="classJournals.php">Student Journals</a></li>
      <li><a href="logout.php">Logout</a></li>
   <?php else : ?>
       <li><a href="<?php echo BASE; ?>">Home</a></li>
       <li><a href="profile.php">My Profile</a></li>
       <li><a href="map.php">Game Map</a></li>
       <li><a href="journal.php">MyJournal</a></li>
       <li><a href="highScores.php">High Scores</a></li>
       <li><a href="logout.php">Logout</a></li>
   <?php endif; ?>
   <?php endif; ?>
</ul>