简体   繁体   中英

Php session global variables

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.

You need to suppress the error reporting:

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

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

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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM