php中的页面重定向不一致

I have set the session on all pages in my admin panel like so:

session_start();

if(!isset($_SESSION['username']))
   header("Location: login.php?e=please+login+first");

This seemed to be working properly on all my pages, but now when I refresh my page or click on a link, the page automatically redirects to login.php. This seems to happen infrequently. I don't understand what is causing this problem

As an example, my menu bar is like:

home : doctors: hospitals:  feedback

If I am on hospitals.php, then after a few mins I click on feedback.php (or any other link) I get redirected to login.php. The next time if I again click on the link I will not get redirected

Here is my dologin.php code

$qry = "select * from admin where username='$uname' and password='$pwd'";
//echo $qry;exit;
$rs = mysql_query($qry,$con);
if($rec= mysql_fetch_array($rs))
{
  $_SESSION['username'] = $rec['username'];
    header("Location: index.php");
}
 else if (empty($uname))
{
     header("Location: login.php?e=Please+enter+username");
}
 else
 {
     header("Location: login.php?e=Invalid+username+or+password");
 }

EDIT: Replace your query:

$qry = "select * from admin where username='$uname' and password='$pwd'";

With this:

$qry = "select * from admin where username='" . $uname . "' and password='" . $pwd . "'";

Also replace your:

session_start();

with:

if (!isset($_SESSION)) {
  session_start();
}

Recommendations: I would also recommend placing the content that is being reused from file to file in a PHP include such as header.php.

PS: try accepting more answers, it helps motivate people to answer your questions. Also keeps the community alive.

You could try calling session_write_close() before you redirect with header to make sure the session is closed.