# SEND OUT THE AUTHENTICATION DATA
echo "<input type='hidden' name='user' value='$user'>\n";
echo "<input type='hidden' name='passwd' value='$passwd'>\n";
store some value into your session instead, so that you know the guy logged on successfully like so:
$_SESSION['loggedon'] = $user
What you need to add now is a) a check to the SESSION array if the user logged on correctly previously. If so: run the code. If not: present the loginform, set the session variable.
Check the login credentials only after a login, i.e. after your user was presented the login form. You can check this by adding if (isset($_POST['login']){...}.
Hope that's enough info to point you in the right direction.