Session Var access within functions?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Arocity
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2008 8:30 am
Location: Washington, DC

Session Var access within functions?

Post by Arocity »

I found the solution to this answer while previewing my post, but feedback on it is welcome anyway. And if it looks good, maybe somebody else might find it useful. (The problem was I misspelled $thisSessionID on Line 10)
-----------------------------

I have an external script (functions.enc.php) that has a logged_in() function that I want to check whether a user is logged in. Since I'm using sessions, when a user logs in, the session var "logged_in" is set to "1", "em_id" is set to the user's username, and I set the database value "session_id" equal to md5($session_id) . md5($_SERVER['HTTP_USER_AGENT']).

Inside functions.enc.php, I have the following:

Code: Select all

function logged_in($aEmployee)
{
    global $db;
    $result = 0;
    if($_SESSION['logged_in'] == 1)
    {
        $checkSessionID_q = "SELECT session_id FROM employees WHERE em_id='$aEmployee'";
        $checkSessionID = $db->get_var($checkSessionID_q);
        $session_id = session_id();
        $thissSessionID = md5($session_id) . md5($_SERVER['HTTP_USER_AGENT']);
        
        if($thisSessionID == $checkSessionID)
        {
            $result = 1;
        }
    }
 
    return $result;
}
FYI, $db is for my database class.
Post Reply