undefined变量:登录时的$ username [重复]

I have error when a user login

whats wrong?

in first time tell me welcome but after refresh page tell me error:

Notice: Undefined variable : username in index.php on line 175

<?php
$fn=2;
if(isset($_POST["smbit"])){
$fn=1;
$username = $_POST['username'];
$password = md5($_POST['password']);
$res=mysql_query("SELECT userid,isadmin FROM users WHERE user='$_POST[username]' and pass='$_POST[password]'",$cn);
   if($row=mysql_fetch_object($res)){
    $fn=0;
    mysql_query("UPDATE session SET islogin=1,isadmin=$row->isadmin,uid='$row->userid' where sid='$sid'");
}
}
if($islogin==1) $fn=0;
if($fn==1) echo "<b>Login Not Found!</b>";  
if($fn==1 || $fn==2){
?>
    <p></p>
    <form action="index.php" method="post">
    <i>Username:</i><input type="text" name="username"/>
    <i>Password:</i><input type="password" name="password">
    <input class="buttom" type="submit" name="smbit" value="Login">
    </form>
    <a href="register.php">Not Register?</a>
    <p></p>
<?php
}
if($fn==0)   echo "<h3 align=center>Welcome $username</h3><h4><a href=logout.php>Log out</a> </h4>";
?>
</div>

After refresh if(isset($_POST["smbit"])) returns false, $fn is equal to 2.

Then, if condition if($islogin==1) is true (probably is true), $fn is set to 0.

And in the last row, $fn == 0, but $username is undefined (because you set this variable in condition testing existence of $_POST['smbit']).

Possible solution: after succesfully login set $_SESSION['username'] = $_POST['username'], and in this line work with session instead of undefined variable.