0

not sure if I have a unique problem or just haven't been able to hit the right terms on google to find it, but my problem is dealing with an undefined variable and not being able to check it.

Across my site for menus I include a php file with all the menu stuff in it, then echo out the various parts as needed. My problem is that one of the menu options shows the currently logged in user. Of course when not logged in this is not set, which results in the log file being filled with undefined errors.

Here is the structure:

static.php:

<?php
$menu='     
    <ul class="menu">
        <li class="menu"><a href="#">Home</a></li>
        <li class="menu"><a href="#">Option</a></li>
    </ul>';
$usermenu=' 
    <ul class="usermenu">
        <li class="menu"><a href="#">'.$_SESSION['user_name'].'</a></li>
        <li class="menu"><a href="#">Change Pass</a></li>
        <li class="menu"><a href="#">Logout</a></li>
    </ul>';

index.php:

<?php include 'static.php';
    echo "$menu";
    if ($login->isUserLoggedIn() == 'true') {
        echo "$usermenu";
    }
    if ($login->isUserAdmin() == 'true') {
        echo "$usermenuadmin";
    }
?>

As you can see, the $_SESSION['user_name'] is within the static file, so if you aren't logged in it isn't set and produces the error.

Normally I would just use if (!session_status() == PHP_SESSION_NONE) {}, but since it is in the middle, I cant put it in there.

One way to fix it would be to have a separate static file for things like that and only include it if the session is set, but then I would have to go and change the code across many pages and would have to have a separate file for a single entry.

Am I missing something here or is there an easy way to fix this? From what I found online it isn't possible to echo out php code, so that prevents any of the normal ways of fixing it.

EDIT: Solved it, see my answer below!

iamalion
  • 1
  • 3

4 Answers4

0

Please use isset($var) to check if the variable has been declared/set.

if(isset($_SESSION) && isset($_SESSION['user_name']))


So, your static.php would look as follows,

$usermenu='';
if(isset($_SESSION) && isset($_SESSION['user_name']))
{
    $usermenu='<ul class="usermenu">
        <li class="menu"><a href="#">'.$_SESSION['user_name'].'</a></li>
        <li class="menu"><a href="#">Change Pass</a></li>
        <li class="menu"><a href="#">Logout</a></li>
    </ul>';
}

Above,you would have an empty string in $usermenu if $_SESSION and $_SESSION['user_name'] are not set.
As an alternative, you can call @isset to avoid warnings in your PHP error log.
A serious program code would use more complex set of functions to check if the user is logged in.

  • Yeah this isn't something serious, just a hobby really so I'm trying to learn. Will change it thanks! – iamalion Feb 13 '16 at 22:12
0

You didn't use session_start(); in static.php file and use isset() for checking either variable or index set or not:

Modified code:

<?php 
session_start();

$menu=' 
<ul class="menu">
<li class="menu"><a href="#">Home</a></li> 
<li class="menu"><a href="#">Option</a></li> 
</ul>'; 
$usermenu=' 
<ul class="usermenu"> 
<li class="menu">
<a href="#">
'.(isset($_SESSION['user_name']) ? $_SESSION['user_name'] : "Guest") .'
</a></li> 
<li class="menu">
<a href="#">Change Pass</a></li> 
<li class="menu"><a href="#">Logout</a></li> 
</ul>';
devpro
  • 16,184
  • 3
  • 27
  • 38
0

Friend pointed out to me the super easy solution that I was overlooking.

if (isset($_SESSION['user_name'])) {
$usermenu='     <ul class="usermenu">
}

I just didn't think to put it before declaring $usermenu. Mondays...

iamalion
  • 1
  • 3
0

The way your code is structured is not good.

I assume it relies on session autostart or that there's code containing session_start() you've not shown us.

At least the include file does not directly produce output at the time it is included. I would change the include file to...

<?php

function menu($authuser, $isadmin)
{
    print '<ul class="menu">
    <li class="menu"><a href="#">Home</a></li>
    <li class="menu"><a href="#">Option</a></li>
    </ul>';
    if ($authuser) {
        print '<ul class="usermenu">
        <li class="menu"><a href="#">'.$authuser.'</a></li>
        <li class="menu"><a href="#">Change Pass</a></li>
        <li class="menu"><a href="#">Logout</a></li>
        </ul>'; 
     }
     if ($isadmin) {
        ...
     }
}
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • I'm not a developer, just learning PHP for fun really, so I'm sure I have made a looooot of bad choices, but I've been going back and fixing them using the recommended methods and best practices. Correct, page that includes static.php starts the session. I showed a heavily reduced version of the code. – iamalion Feb 13 '16 at 21:54
  • And for the example you gave, calling it would be as simple as `menu( $_SESSION['user_name'], $_SESSION['isadmin] );`? That is actually pretty easy. I'll look into using that instead thanks! – iamalion Feb 13 '16 at 21:57