Can someone fake a $_SESSION variable? is it safe to just store a few variables using $_SESSION to determine if the user is logged in AND which user id he/she has? Could someone try to impersonate someone else through hacking session IDs?
I am retrieving a hashed password from the db and then I store a session to determine if user is logged in:
// Check username or email
$data = mysql_query("SELECT * FROM Users WHERE Username = '$username' OR Email = '$username'");
$num_rows = mysql_num_rows($data);
if($num_rows <= 0){ // Does username exist??
        // ERROR MESSAGES
        echo '<div class="error_messages";>';
        echo 'Login combination is incorrect.';
        echo '</div>';            
}else{ // It exists, now checks password
    while($row = mysql_fetch_array( $data )) {
        $username_id = $row['id'];
        $existingHashFromDb = $row['Password']; // Hash from db
        $first_name = $row['FirstName'];
    }
    $isPasswordCorrect = password_verify($password, $existingHashFromDb);
    if ( $isPasswordCorrect){ // Password is correct, user has logged in successfully!!
        // Create a session saying that we are logged in
        // And another session to store user's id
        $_SESSION['loggedIn'] = true;
        $_SESSION['userId'] = $username_id;
        echo '<div class="success_messages";>';
        echo 'Welcome back, ' . $first_name . '!';
        echo '</div>';                
    }else{
        echo '<div class="error_messages";>';
        echo 'Login combination is incorrect.';
        echo '</div>';                   
    }
Then I would simply check who is logged in by using this function:
//checks if the user is logged in with the cookie on the browser, it returns 1 if logged, otherwise returns 0
function checkslogged()
{
    if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
        return 1;
    }else return 0;
}
Is this the best approach?
 
     
    