0

I use sessions with php, I use session_start and store some variables that are passed from a login form, the variables are 'user_name' and 'user_role' at the beginning of code I check if those variables are set, and if not, redirect to loggin scree. Now the problem is that I have a include file that has the menu options in it, if the user is a privileged user, it shows more options than if it's not. my problem is that when the include file is processed i get PHP Notice: Undefined index: user_name in C:\inetpub\wwwroot\2StarsGames.com\SomeGame v4.1\interface\HeaderMenu.php on line 20 'user_name' Since i don't get errors in the rest of the files, i just included the menu file in this post. Can someone tell me if there's a special treatment of sessions if are inside a separate file?

<div id="page_header"> 
<img class="logoImage" src="./img/some1.png" align="left" />
SUPER B 
<img class="logoImage" src="./img/some2.png" align="right" />
</div>

<div id="page_menu">
<center>
    <a class="menu" href="./Contest.php">Contest</a>
    <a class="menu" href="./Cards.php">Cards</a>
    <a class="menu" href="./PlayersPoints.php">Players Points</a>   
    <a class="menu" href="./SBCardsCode.php">Card's Code</a> 
    <a class="menu" href="./Avatars.php">Avatars</a>   
    <a class="menu" href="./Sims.php">Sims</a>   
    <a class="menu" href="./Boards.php">Boards</a> <br/>   
    <a class="menu" href="./Charity.php">Charity</a>
    <?php 
        $username = strtolower($_SESSION['user_name']);
        if($username == 'some name1' ||
           $username == 'some name2' ||
           $username == 'some name3' ||
           $username == 'some name4')
        {
            echo "          
                <a class='menu' href='./PayoutsNoWin.php'>Payouts</a>  
                <a class='menu' href='./Payins.php'>Payins|</a>            
                <a class='menu' href='./Payments.php'>Payments</a>
                <a class='menu' href='./Tools.php'>Tools</a>  
                <a class='menu' href='./TransferData.php'>Transfer Data</a>
                <a class='menu' href='./Games.php'>Games</a>
                ";
        }
    ?>
</center>

edit the session_start() is in the file that includes this one.

6
  • 2
    Are you sure that session_start gets called before the include? Commented Nov 22, 2012 at 8:36
  • or you set the user_name in $_session ? Commented Nov 22, 2012 at 8:37
  • session_start gets called before anything in the file that includes this one. I also check for those variables on top and if they are not set I redirect to loggin screen. Commented Nov 22, 2012 at 8:37
  • can you give me print_r() form your session here? Commented Nov 22, 2012 at 8:40
  • this can only happen if you set $_SESSION variable after your menu file is included, therefore menu file cannot yet get the $_SESSION keys. I would suggest you switch $_SESSION setting before inclusion of file Commented Nov 22, 2012 at 8:41

2 Answers 2

1

You need to suppress the error reporting:

$username = strtolower(@$_SESSION['user_name']);

or better, check for isset($_SESSION['user_name']) before using the variable.

Sign up to request clarification or add additional context in comments.

3 Comments

Why suppress it? If it's there it means that something is wrong. Shouldn't I better fix it?
I can't believe I'm doing this... but I have to select an answer, and yours as much hacky as it is, seems to be the best. I suppressed. :( No error, and everything works as they should. I'm sure I will go back to this soon, but I'm too tired with this thing to keep looking. Ty everybody for your help.
Welcome to production code :)... You should rather be using if(!empty($_SESSION['user_name'])) or if(isset($_SESSION['user_name'])). In this case, however - it should be obvious to any experienced programmer why the error is suppressed...as hacky as it is ;)
0

The error seems to be occurring here:

$username = strtolower($_SESSION['user_name']);

"Undefined index" basically means that the 'user_name' index cannot be found in the $_SESSION variable.

Can you post your code where you're setting this?
And ensure this code runs before your check for $_SESSION['user_name'];

4 Comments

after session_start in ALL scripts i includ another file called Lock.php which contains the following lines. if(!isset($_SESSION['logged_in'], $_SESSION['user_name'], $_SESSION['user_role'])) { header('Location: Login.php'); } so since I reach the point where it uses the session variables, it means that they are there. At least that's what I thought when I wrote the code.
The weird thing is that the code runs fine... I just get that error. It's weird.
If the Payment, Tools menu items are being echoed out, can you include an "echo $username" to see what this variable contains? You may need to step down your error reporting level to do this.
I noticed something, that error occurs only when I first loggin but it doesn't cause any damage, and the print_r shows that session data are set. So something has to be wrong in the code indeed. I'll do more search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.