hi everyone,
I'm totally new to PHP and have just installed a Login script on my site, however..... it was working fine on its own, as soon as I integrated it in the already existing HTML page <?php include("login_script.php");?> it gives errors. I'm hoping its a small issue for you expert, thanks a lot in advance!¨

Link:
http://www.esmertec.com/investor_relations/secure/login.php

You can login in using User:rana, pw:rana

The script used:

<?php

// Variables

// Path to the config file.

$config = "conf.php";

// End Variables

// Load config file.

require($config);

// Initial File checks to make sure the files exist before using them.

if(!file_exists($user_data)) {
echo "<h1 align=\"center\">$user_data_file_not_exist</h1>";
exit;
}
elseif(!is_readable($user_data)) {
echo "<h1 align=\"center\">$user_data_file_not_readable</h1>";
exit;
}
elseif($log_login) {
if(!file_exists($log_file)) {
echo "<h1 align=\"center\">$log_file_not_exist</h1>";
exit;
}
elseif(!is_writable($log_file)) {
echo "<h1 align=\"center\">$log_file_not_writable</h1>";
exit;
}
}

// Perform operation according to command:

switch ($cmd) {
case "login":
if(check_user($username, $password, $name, $log_login)) {
session_start("pageprotect");
session_register('valid');
session_register('username');
session_register('name');
// Uses MD5 and soundex to encode "valid" var to avoid easy access.
$valid = strrev(soundex(name).md5($username));
if ($log_login) {
$fd = @fopen($log_file, "a");
fputs($fd, time()."|$name|$username\n");
fclose($fd);
}
header("Location: $first_page?".SID);
exit;

   } else {
     header("location: $login_page?cmd=invalid");
     exit;
   }
   break;
case "unauth":
 session_start("pageprotect");
 session_destroy();
 print_header();
 echo "<h3 align=\"center\">$unauthorised</i></h3>";
 print_login();
 print_footer();
 break;
case "invalid":
 print_header();
 echo "<h3 align=\"center\">$invalid_username_password</i></h3>";
 print_login($username);
 print_footer();
 break;
case "logout":
 session_start("pageprotect");
 session_destroy();
 print_header();
 echo "<h3 align=\"center\">$logged_out</i></h3>";
 print_login($username);
 print_footer();
 break;
default:
 print_header();
 print_login($username);
 print_footer();
 break;

}

function print_header() {
// Prints the header of the page.
echo "<html><head><title>Login</title></head><body style=\"font-family:verdana;\">";
}

function print_footer() {
// Prints the footer of the page.
echo "<div align=\"center\"><font size=\"-9\">This is a Secure Login Site</font></div></body></html>";
}

function check_user($username, $password, &$name, &$log) {
// Checks to see the user is valid.
if(($username == $GLOBALS[admin_username]) && ($password == $GLOBALS[admin_password])) {
$name = "Administrator";
$log = false;
return true;
}
else {
$file = @file($GLOBALS[user_data]);
$pw = md5($password);
$valid = false;
if(is_array($file)) {
foreach($file as $user) {
$dat = explode("|", $user);
if($username==$dat[0] && $pw == trim($dat[2])) {
$valid = true;
$name = $dat[1];
}
}
}
return $valid;
}
}

function print_login($username='') {
// Prints the login form.
echo "<h2 align=\"center\"><i>$GLOBALS[user_login]</i></h2>
<form method=\"post\" action=\"$GLOBALS[SCRIPT_NAME]\">
<input type=\"hidden\" name=\"cmd\" value=\"login\">
<table align=\"center\">
<tr><td>Username:</td><td><input type=\"text\" name=\"username\" value=\"$username\"></td></tr>
<tr><td>Password:</td><td><input type=\"password\" name=\"password\"></td></tr>
<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Login\"></td></tr></table></form>";
}

?>

    The problem is you're trying to send headers after you've outputed data to the browser, this is not allowed. Where you're using the functions session_start() session_register() etc these must be before any output has been sent to the browser. If nothing else is wrong then all you need to do is re-jig the code so you carry out all the dealings with the headers at the start of the script.

    HTH
    Bubble

      Hi there,

      Your script is failing because it's trying to initialise a 'session' but unless this is done before any other code is run, it won't work.

      you need to put the <?php include("login_script.php");?> at the very beginning of your html file so that it can initialise it's session before the html etc is sent.

      give that a try....Andy.

        Hi Andy,
        Thanks so much for your reply, it works! Of course the only issue I have now is that since the include is all the way on top it is also placed wrongly on the page. How can I solve this? Thanks again..!!

          You need to split your login processing script from your HTML form perhaps putting the login form into another include that is executed further down the page.

          Mark.

            thanks marky2coats...i split the code but the form is still in the wrong position. since i have no php knowledge at all i guess i overlooked something. this is the current code on the above mentioned link, maybe you can help re-shuffle the code???

            <?php
            // Variables

            // Path to the config file.

            $config = "conf.php";

            // End Variables

            // Load config file.

            require($config);

            // Initial File checks to make sure the files exist before using them.

            if(!file_exists($user_data)) {
            echo "<h1 align=\"center\">$user_data_file_not_exist</h1>";
            exit;
            }
            elseif(!is_readable($user_data)) {
            echo "<h1 align=\"center\">$user_data_file_not_readable</h1>";
            exit;
            }
            elseif($log_login) {
            if(!file_exists($log_file)) {
            echo "<h1 align=\"center\">$log_file_not_exist</h1>";
            exit;
            }
            elseif(!is_writable($log_file)) {
            echo "<h1 align=\"center\">$log_file_not_writable</h1>";
            exit;
            }
            }

            // Perform operation according to command:

            switch ($cmd) {
            case "login":
            if(check_user($username, $password, $name, $log_login)) {
            session_start("pageprotect");
            session_register('valid');
            session_register('username');
            session_register('name');
            // Uses MD5 and soundex to encode "valid" var to avoid easy access.
            $valid = strrev(soundex(name).md5($username));
            if ($log_login) {
            $fd = @fopen($log_file, "a");
            fputs($fd, time()."|$name|$username\n");
            fclose($fd);
            }
            header("Location: $first_page?".SID);
            exit;

               } else {
                 header("location: $login_page?cmd=invalid");
                 exit;
               }
               break;
            case "unauth":
             session_start("pageprotect");
             session_destroy();
             print_header();
             echo "<h3 align=\"center\">$unauthorised</i></h3>";
             print_login();
             print_footer();
             break;
            case "invalid":
             print_header();
             echo "<h3 align=\"center\">$invalid_username_password</i></h3>";
             print_login($username);
             print_footer();
             break;
            case "logout":
             session_start("pageprotect");
             session_destroy();
             print_header();
             echo "<h3 align=\"center\">$logged_out</i></h3>";
             print_login($username);
             print_footer();
             break;
            default:
             print_header();
             print_login($username);
             print_footer();
             break;

            }

            function print_header() {
            // Prints the header of the page.
            echo "<html><head><title>Login</title></head><body style=\"font-family:verdana;\">";
            }

            ?>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <html>
            <head>
            <title>esmertec : wireless software solutions for mass market mobile multimedia phones and embedded devices</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

            <meta name="description" content="Esmertec is the leading provider of wireless software solutions for mass market mobile multimedia phones and embedded devices">
            <meta name="keywords" content="Java wireless mobile virtual machine Sun handset Smart phone Jbed Jeco cellular phone">
            <meta name="author" content="Rana Doustdar">
            <meta name="robots" content="index,follow">
            <meta name="revisit-after" content="20 days">
            <meta name="Content-Language" Content="en">

            <link rel="stylesheet" href="/main.css" type="text/css">

            </head>

            <body bgcolor="#0066cc" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td height="70" bgcolor="#6699FF">&nbsp;</td>
            <td bgcolor="#6699FF">&nbsp;</td>
            <td bgcolor="#6699FF">&nbsp;</td>
            <td bgcolor="#6699FF">&nbsp;</td>
            </tr>
            <tr>
            <td height="46" bgcolor="#6699FF">&nbsp;</td>
            <td width="208" bgcolor="#6699FF"><a href="http://www.esmertec.com"><img src="/img/logo.gif" alt="Esmertec.com" width="208" height="46" border="0"></a></td>
            <td bgcolor="#6699FF">&nbsp;</td>
            <td bgcolor="#6699FF">&nbsp;</td>
            </tr>
            <tr>
            <td bgcolor="#6699FF" height="12">&nbsp;</td>
            <td bgcolor="#6699FF">&nbsp;</td>
            <td valign="bottom" width="781" bgcolor="#6699FF"><img src="/nav/1.gif" width="781" height="12"></td>
            <td bgcolor="#6699FF">&nbsp;</td>
            </tr>
            <tr>
            <td height="24" bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            <td height="24" bgcolor="#FFFFFF">
            <?php virtual ("/nav/nav.php");?>
            </td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            </tr>
            <tr>
            <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF"><table height="20" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td width="470">&nbsp;</td>
            <td width="100">&nbsp;</td>
            <td width="120" valign="bottom"><img src="/img/arrow.gif" width="12" height="17"><a class="menu" href="http://www.esmertec.com/investor_relations/secure/login.php">Login</a></td>
            </tr>
            </table></td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            </tr>
            <tr>
            <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
            <td rowspan="2" bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            </tr>
            <tr>
            <td height="300" bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF"><p>&nbsp;</p>
            <table width="600" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td valign="top">
            <?php

            	  function check_user($username, $password, &$name, &$log) {

            // Checks to see the user is valid.
            if(($username == $GLOBALS[admin_username]) && ($password == $GLOBALS[admin_password])) {
            $name = "Administrator";
            $log = false;
            return true;
            }
            else {
            $file = @file($GLOBALS[user_data]);
            $pw = md5($password);
            $valid = false;
            if(is_array($file)) {
            foreach($file as $user) {
            $dat = explode("|", $user);
            if($username==$dat[0] && $pw == trim($dat[2])) {
            $valid = true;
            $name = $dat[1];
            }
            }
            }
            return $valid;
            }
            }

            	  function print_login($username='') {

            // Prints the login form.
            echo "<h2 align=\"center\"><i>$GLOBALS[user_login]</i></h2>
            <form method=\"post\" action=\"$GLOBALS[SCRIPT_NAME]\">
            <input type=\"hidden\" name=\"cmd\" value=\"login\">
            <table align=\"center\">
            <tr><td>Username:</td><td><input type=\"text\" name=\"username\" value=\"$username\"></td></tr>
            <tr><td>Password:</td><td><input type=\"password\" name=\"password\"></td></tr>
            <tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Login\"></td></tr></table></form>";
            }

            	 function print_footer() {

            // Prints the footer of the page.
            echo "<div align=\"center\"><font size=\"-9\">This is a Secure Login Site</font></div></body></html>";
            }

            	 ?> 
            	  </td>
                </tr>
              </table>			
              <p>&nbsp;</p>    </td>
            <td bgcolor="#FFFFFF">&nbsp;</td>

            </tr>
            <tr>
            <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            <td bgcolor="#FFFFFF">&nbsp;</td>
            </tr>
            <tr>
            <td height="5">&nbsp;</td>
            <td valign="bottom" colspan="2">&nbsp;</td>
            <td>&nbsp;</td>
            </tr>
            <tr>
            <td height="10">
            <div align="center"></div></td>
            <td height="10"><div align="left"><span class="copyright">
            <?php virtual ("/copyright.txt");?>
            </span></div></td>
            <td height="10">&nbsp;</td>
            <td height="10">&nbsp;</td>
            </tr>
            <tr>
            <td colspan="4">&nbsp;</td>
            </tr>
            </table>
            </body>
            </html>

              Firstly sort the order of your code out!. You have function definitions intermingled with your HTML content which is making it very difficult to understand the flow of the script.

              I define all my PHP at the top of the page and all the HTML at the bottom.

              Anway the reson your login script is apearing at the to pall the time is because of this bit:

              default: 
              	print_header(); 
              	print_login($username); 
              	print_footer(); 
              	break; 

              When you load the page the switch drops through to the default case. So it prints the header,the login form and the footer all before any other HTML has been sent to the client, hence it being right at the top.

              Mark.

                thanks for this info....I have now put all the php for the login first then follows the html etc. So which bit of this php code do I need to take out/modify or move down in order for it to appear in the cell where it says "LOGIN FORM SHOULD APPEAR HERE!!!" and in which way exactly do I need to do this? If you could give me specifics please, since I know nothing about php, I would REALLY appreciate it. Thank you for all your help!¨

                <?php

                // Variables

                // Path to the config file.

                $config = "conf.php";

                // End Variables

                // Load config file.

                require($config);

                // Initial File checks to make sure the files exist before using them.

                if(!file_exists($user_data)) {
                echo "<h1 align=\"center\">$user_data_file_not_exist</h1>";
                exit;
                }
                elseif(!is_readable($user_data)) {
                echo "<h1 align=\"center\">$user_data_file_not_readable</h1>";
                exit;
                }
                elseif($log_login) {
                if(!file_exists($log_file)) {
                echo "<h1 align=\"center\">$log_file_not_exist</h1>";
                exit;
                }
                elseif(!is_writable($log_file)) {
                echo "<h1 align=\"center\">$log_file_not_writable</h1>";
                exit;
                }
                }

                // Perform operation according to command:

                switch ($cmd) {
                case "login":
                if(check_user($username, $password, $name, $log_login)) {
                session_start("pageprotect");
                session_register('valid');
                session_register('username');
                session_register('name');
                // Uses MD5 and soundex to encode "valid" var to avoid easy access.
                $valid = strrev(soundex(name).md5($username));
                if ($log_login) {
                $fd = @fopen($log_file, "a");
                fputs($fd, time()."|$name|$username\n");
                fclose($fd);
                }
                header("Location: $first_page?".SID);
                exit;

                   } else {
                     header("location: $login_page?cmd=invalid");
                     exit;
                   }
                   break;
                case "unauth":
                 session_start("pageprotect");
                 session_destroy();
                 print_header();
                 echo "<h3 align=\"center\">$unauthorised</i></h3>";
                 print_login();
                 print_footer();
                 break;
                case "invalid":
                 print_header();
                 echo "<h3 align=\"center\">$invalid_username_password</i></h3>";
                 print_login($username);
                 print_footer();
                 break;
                case "logout":
                 session_start("pageprotect");
                 session_destroy();
                 print_header();
                 echo "<h3 align=\"center\">$logged_out</i></h3>";
                 print_login($username);
                 print_footer();
                 break;
                default:
                 print_header();
                 print_login($username);
                 print_footer();
                 break;

                }

                function print_header() {
                // Prints the header of the page.
                echo "<html><head><title>Login</title></head><body style=\"font-family:verdana;\">";
                }

                function print_footer() {
                // Prints the footer of the page.
                echo "<div align=\"center\"><font size=\"-9\">This is a Secure Login Site</font></div></body></html>";
                }

                function check_user($username, $password, &$name, &$log) {
                // Checks to see the user is valid.
                if(($username == $GLOBALS[admin_username]) && ($password == $GLOBALS[admin_password])) {
                $name = "Administrator";
                $log = false;
                return true;
                }
                else {
                $file = @file($GLOBALS[user_data]);
                $pw = md5($password);
                $valid = false;
                if(is_array($file)) {
                foreach($file as $user) {
                $dat = explode("|", $user);
                if($username==$dat[0] && $pw == trim($dat[2])) {
                $valid = true;
                $name = $dat[1];
                }
                }
                }
                return $valid;
                }
                }

                function print_login($username='') {
                // Prints the login form.
                echo "<h2 align=\"center\"><i>$GLOBALS[user_login]</i></h2>
                <form method=\"post\" action=\"$GLOBALS[SCRIPT_NAME]\">
                <input type=\"hidden\" name=\"cmd\" value=\"login\">
                <table align=\"center\">
                <tr><td>Username:</td><td><input type=\"text\" name=\"username\" value=\"$username\"></td></tr>
                <tr><td>Password:</td><td><input type=\"password\" name=\"password\"></td></tr>
                <tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Login\"></td></tr></table></form>";
                }

                ?>

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                <html>
                <head>
                <title>esmertec : wireless software solutions for mass market mobile multimedia phones and embedded devices</title>
                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

                <meta name="description" content="Esmertec is the leading provider of wireless software solutions for mass market mobile multimedia phones and embedded devices">
                <meta name="keywords" content="Java wireless mobile virtual machine Sun handset Smart phone Jbed Jeco cellular phone">
                <meta name="author" content="Rana Doustdar">
                <meta name="robots" content="index,follow">
                <meta name="revisit-after" content="20 days">
                <meta name="Content-Language" Content="en">

                <link rel="stylesheet" href="/main.css" type="text/css">

                </head>

                <body bgcolor="#0066cc" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td height="70" bgcolor="#6699FF">&nbsp;</td>
                <td bgcolor="#6699FF">&nbsp;</td>
                <td bgcolor="#6699FF">&nbsp;</td>
                <td bgcolor="#6699FF">&nbsp;</td>
                </tr>
                <tr>
                <td height="46" bgcolor="#6699FF">&nbsp;</td>
                <td width="208" bgcolor="#6699FF"><a href="http://www.esmertec.com"><img src="/img/logo.gif" alt="Esmertec.com" width="208" height="46" border="0"></a></td>
                <td bgcolor="#6699FF">&nbsp;</td>
                <td bgcolor="#6699FF">&nbsp;</td>
                </tr>
                <tr>
                <td bgcolor="#6699FF" height="12">&nbsp;</td>
                <td bgcolor="#6699FF">&nbsp;</td>
                <td valign="bottom" width="781" bgcolor="#6699FF"><img src="/nav/1.gif" width="781" height="12"></td>
                <td bgcolor="#6699FF">&nbsp;</td>
                </tr>
                <tr>
                <td height="24" bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td height="24" bgcolor="#FFFFFF">
                <?php virtual ("/nav/nav.php");?>
                </td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                </tr>
                <tr>
                <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF"><table height="20" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="470">&nbsp;</td>
                <td width="100">&nbsp;</td>
                <td width="120" valign="bottom"><img src="/img/arrow.gif" width="12" height="17"><a class="menu" href="http://www.esmertec.com/investor_relations/secure/login.php">Login</a></td>
                </tr>
                </table></td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                </tr>
                <tr>
                <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
                <td rowspan="2" bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                </tr>
                <tr>
                <td height="300" bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF"><p>&nbsp;</p>
                <table width="600" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td valign="top">

                	  LOGIN FORM SHOULD APPEAR HERE!!!
                
                	  </td>
                    </tr>
                  </table>			
                  <p>&nbsp;</p>    </td>
                <td bgcolor="#FFFFFF">&nbsp;</td>

                </tr>
                <tr>
                <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                <td bgcolor="#FFFFFF">&nbsp;</td>
                </tr>
                <tr>
                <td height="5">&nbsp;</td>
                <td valign="bottom" colspan="2">&nbsp;</td>
                <td>&nbsp;</td>
                </tr>
                <tr>
                <td height="10">
                <div align="center"></div></td>
                <td height="10"><div align="left"><span class="copyright">
                <?php virtual ("/copyright.txt");?>
                </span></div></td>
                <td height="10">&nbsp;</td>
                <td height="10">&nbsp;</td>
                </tr>
                <tr>
                <td colspan="4">&nbsp;</td>
                </tr>
                </table>
                </body>
                </html>

                  well the clue was in my answer! You need to take the default section out of the switch function:

                  default:
                  print_header();
                  print_login($username);
                  print_footer();
                  break;

                  otherwise everytime the page loads it will execute the switches default section and print your login at the top. So get rid of that and then replace "LOGIN FORM SHOULD APPEAR HERE" with <?print_login($username)?>

                  Mark.

                    Thanks Mark...this was the kind of idiot-proof instructions i needed. i hope this will be the last time I bug you with this thing. Please have a look http://www.esmertec.com/investor_relations/secure/login.php and login with anything, you see the invalid (same for "unauth", "logout" ) page appears on top and the login.php is duplicated! Can you instruct what exactly to change in the code to fix this?? THANK YOU!

                    <?php

                    // Variables

                    // Path to the config file.

                    $config = "conf.php";

                    // End Variables

                    // Load config file.

                    require($config);

                    // Initial File checks to make sure the files exist before using them.

                    if(!file_exists($user_data)) {
                    echo "<h1 align=\"center\">$user_data_file_not_exist</h1>";
                    exit;
                    }
                    elseif(!is_readable($user_data)) {
                    echo "<h1 align=\"center\">$user_data_file_not_readable</h1>";
                    exit;
                    }
                    elseif($log_login) {
                    if(!file_exists($log_file)) {
                    echo "<h1 align=\"center\">$log_file_not_exist</h1>";
                    exit;
                    }
                    elseif(!is_writable($log_file)) {
                    echo "<h1 align=\"center\">$log_file_not_writable</h1>";
                    exit;
                    }
                    }

                    // Perform operation according to command:

                    switch ($cmd) {
                    case "login":
                    if(check_user($username, $password, $name, $log_login)) {
                    session_start("pageprotect");
                    session_register('valid');
                    session_register('username');
                    session_register('name');
                    // Uses MD5 and soundex to encode "valid" var to avoid easy access.
                    $valid = strrev(soundex(name).md5($username));
                    if ($log_login) {
                    $fd = @fopen($log_file, "a");
                    fputs($fd, time()."|$name|$username\n");
                    fclose($fd);
                    }
                    header("Location: $first_page?".SID);
                    exit;

                       } else {
                         header("location: $login_page?cmd=invalid");
                         exit;
                       }
                       break;
                    case "unauth":
                     session_start("pageprotect");
                     session_destroy();
                     print_header();
                     echo "<h3 align=\"center\">$unauthorised</i></h3>";
                     print_login();
                     print_footer();
                     break;
                    case "invalid":
                     print_header();
                     echo "<h3 align=\"center\">$invalid_username_password</i></h3>";
                     print_login($username);
                     print_footer();
                     break;
                    case "logout":
                     session_start("pageprotect");
                     session_destroy();
                     print_header();
                     echo "<h3 align=\"center\">$logged_out</i></h3>";
                     print_login($username);
                     print_footer();
                     break;

                    }

                    function print_header() {
                    // Prints the header of the page.
                    echo "<html><head><title>Login</title></head><body style=\"font-family:verdana;\">";
                    }

                    function print_footer() {
                    // Prints the footer of the page.
                    echo "<div align=\"center\"><font size=\"-9\">This is a Secure Login Site</font></div></body></html>";
                    }

                    function check_user($username, $password, &$name, &$log) {
                    // Checks to see the user is valid.
                    if(($username == $GLOBALS[admin_username]) && ($password == $GLOBALS[admin_password])) {
                    $name = "Administrator";
                    $log = false;
                    return true;
                    }
                    else {
                    $file = @file($GLOBALS[user_data]);
                    $pw = md5($password);
                    $valid = false;
                    if(is_array($file)) {
                    foreach($file as $user) {
                    $dat = explode("|", $user);
                    if($username==$dat[0] && $pw == trim($dat[2])) {
                    $valid = true;
                    $name = $dat[1];
                    }
                    }
                    }
                    return $valid;
                    }
                    }

                    function print_login($username='') {
                    // Prints the login form.
                    echo "<h2 align=\"center\"><i>$GLOBALS[user_login]</i></h2>
                    <form method=\"post\" action=\"$GLOBALS[SCRIPT_NAME]\">
                    <input type=\"hidden\" name=\"cmd\" value=\"login\">
                    <table align=\"center\">
                    <tr><td>Username:</td><td><input type=\"text\" name=\"username\" value=\"$username\"></td></tr>
                    <tr><td>Password:</td><td><input type=\"password\" name=\"password\"></td></tr>
                    <tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Login\"></td></tr></table></form>";
                    }

                    ?>

                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                    <html>
                    <head>
                    <title>esmertec : wireless software solutions for mass market mobile multimedia phones and embedded devices</title>
                    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

                    <meta name="description" content="Esmertec is the leading provider of wireless software solutions for mass market mobile multimedia phones and embedded devices">
                    <meta name="keywords" content="Java wireless mobile virtual machine Sun handset Smart phone Jbed Jeco cellular phone">
                    <meta name="author" content="Rana Doustdar">
                    <meta name="robots" content="index,follow">
                    <meta name="revisit-after" content="20 days">
                    <meta name="Content-Language" Content="en">

                    <link rel="stylesheet" href="/main.css" type="text/css">

                    </head>

                    <body bgcolor="#0066cc" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                    <td height="70" bgcolor="#6699FF">&nbsp;</td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="46" bgcolor="#6699FF">&nbsp;</td>
                    <td width="208" bgcolor="#6699FF"><a href="http://www.esmertec.com"><img src="/img/logo.gif" alt="Esmertec.com" width="208" height="46" border="0"></a></td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td bgcolor="#6699FF" height="12">&nbsp;</td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    <td valign="bottom" width="781" bgcolor="#6699FF"><img src="/nav/1.gif" width="781" height="12"></td>
                    <td bgcolor="#6699FF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="24" bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    <td height="24" bgcolor="#FFFFFF">
                    <?php virtual ("/nav/nav.php");?>
                    </td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF"><table height="20" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                    <td width="470">&nbsp;</td>
                    <td width="100">&nbsp;</td>
                    <td width="120" valign="bottom"><img src="/img/arrow.gif" width="12" height="17"><a class="menu" href="http://www.esmertec.com/investor_relations/secure/login.php">Login</a></td>
                    </tr>
                    </table></td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
                    <td rowspan="2" bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="300" bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF"><p>&nbsp;</p>
                    <table width="600" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                    <td valign="top">

                     <?php print_login($username)?> 
                    
                    
                    	  </td>
                        </tr>
                      </table>			
                      <p>&nbsp;</p>    </td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>

                    </tr>
                    <tr>
                    <td height="10" bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="5">&nbsp;</td>
                    <td valign="bottom" colspan="2">&nbsp;</td>
                    <td>&nbsp;</td>
                    </tr>
                    <tr>
                    <td height="10">
                    <div align="center"></div></td>
                    <td height="10"><div align="left"><span class="copyright">
                    <?php virtual ("/copyright.txt");?>
                    </span></div></td>
                    <td height="10">&nbsp;</td>
                    <td height="10">&nbsp;</td>
                    </tr>
                    <tr>
                    <td colspan="4">&nbsp;</td>
                    </tr>
                    </table>
                    </body>
                    </html>

                      Im relectant to just give you the answer as I dont think ppl learn anything.

                      Look through your code. Go through it step by step and understand how it flows. It's a debugging excercise that EVERYONE should know how to do.

                      If your still stuck then look at the switch statment when the user is not authorised:

                      case "unauth":
                      session_start("pageprotect");
                      session_destroy();
                      print_header();
                      echo "<h3 align=\"center\">$unauthorised</i></h3>";
                      print_login();
                      print_footer();
                      break;

                      your doing print_login() and the rest at the top of the page again when you have <? print_login()?> futher down the page.

                      and when your username is invalid:

                      case "invalid":
                      print_header();
                      echo "<h3 align=\"center\">$invalid_username_password</i></h3>";
                      print_login($username);
                      print_footer();
                      break;

                      Mark.

                        hey mark, thanks again for all your efforts, i think i figured it out, or at least its working 🙂

                          Write a Reply...