user830593
user830593

Reputation: 221

PHP User logged in changes!

Whenever my user goes to the page that's below the session username changes. Can someone tell me why? I would be very grateful. I have a feeling it may have something to do with the while loop I declare after I start my session.

Here is the page where the username changes!!! (leaderboard.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();

if ($_SESSION['username'])

    $user = "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>";
else
    die("You must be logged in!");

$connect = //CONNECT TO DATABASE


$query = "SELECT @rank:= @rank + 1 as rank, s.* FROM ( SELECT username, score, wins, games FROM stats, (SELECT @rank:=0) r ORDER BY score DESC LIMIT 10 ) s";

if ($query_run = mysql_query($query))
{
//begin table
echo "<div class='container'>
        <table class='tableposition' border='1' width='600' cellpadding='3' cellspacing='0'>
        <caption>OVERALL RANKINGS</caption>
        <tr class='tablehead'>
        <th>RANK</th>
        <th> USERNAME</th>
        <th>OVERALL SCORE</th>
        <th>WIN%</th>
        </tr>";
//begin loop
    while ($query_row = mysql_fetch_assoc($query_run))
    {
    $rank = $query_row['rank'];
    $username = $query_row['username'];
    $score = $query_row['score'];
    $wins = $query_row['wins'];
    $games = $query_row['games'];
    $winpere = $wins / $games * 100;
    $winper = number_format($winpere,1);

    echo "<tr class='tablecontents'>
            <td>$rank</td>
            <td>$username</td>
            <td>$score</td>
            <td>$winper %</td>
        </tr>
        ";
    } 
//end loop
//close table   
echo "</table>
        </div>";


}else{
echo mysql_error();
}

//superbox file
include('superbox.php');
?>

Upvotes: 1

Views: 116

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82048

I've immediately noticed one issue which needs fixing. session_start is among one of those methods which must be called before any content which is supposed to be rendered by the browser. This means you need to do:

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php

// everything else.

While I can't guarantee that to be the cause of your problem, it is definitely needed.

Upvotes: 5

Related Questions