Ciao, questo codice fa la stessa cosa, proprio su un forum.
Studialo ed adattalo alle tue esigenze.

Codice PHP:
<?php
include("inc/header.inc.php");

if (
$SGET[step] == "2" AND $_SESSION[username] == "") {
############################
$_POST[username] = str_replace(">""&gt;"$_POST[username]);
$_POST[username] = str_replace("<""&lt;"$_POST[username]);
$_POST[password] = md5($_POST[password]);

$query_user "SELECT username,id,type FROM $table_users WHERE username=\"$_POST[username]\" AND password=\"$_POST[password]\"";
$result_user mysql_query($query_user$db);
$row_user mysql_fetch_array($result_user);


if (
$row_user[username] != "") {



$_SESSION[username] = "$_POST[username]";
$_SESSION[user_id] = "$row_user[id]";
$_SESSION[type] = "$row_user[type]";

$q "UPDATE $table_users SET last_login=\"Date: $date2 \n Ip: $ip\" WHERE id=\"$_SESSION[user_id]\"";
mysql_query($q$db);

#####COOKIE########
if ($_POST[remember] != "") {
$current_time time();
setcookie("username","$_POST[username]",$current_time 31536000);
setcookie("user_id","$row_user[id]",$current_time 31536000);
// Mod By Neoscript
// Possibilità di loggarsi tramite modifica del cookies con account di altri senza password
// Scrivo nel cookie un nuovo campo con un nuovo valore che poi servira nel login automatico
setcookie("password","$_POST[password]",$current_time 31536000); // Mod By Neoscript
}

#################

include("inc/header2.inc.php");

print 
"<span class=\"text1\">$lang[29] ".stripslashes(stripslashes($_SESSION[username])).",$lang[30]</span>

"
;
print 
"<a class=\"a1\" href=\"$_POST[ref]\">$lang[31]</a>";
print 
"<script language=\"Javascript\">\n";
print  
"window.location.href = \"$_POST[ref]\";\n";
print 
"</script>";
print 
"
"
;
include(
"inc/footer.inc.php");
exit;
##############
}
else {
include(
"inc/header2.inc.php");
print_error("$lang[32]");
}

}



if (
$_GET[action] == "logout") {


$q "SELECT user_id FROM $table_users_online WHERE user_id=\"$_SESSION[user_id]\" LIMIT 1";
$r mysql_query($q$db);
$row mysql_fetch_array($r);

if (
$row[user_id] != "") {
$q "DELETE FROM $table_users_online WHERE user_id=\"$_SESSION[user_id]\" LIMIT 1";
$r mysql_query($q$db);
}

###########DESTROYS#############
setcookie("username","",$current_time 31536000);
setcookie("user_id","",$current_time 31536000);
session_unset();
session_destroy();
####################################
include("inc/header2.inc.php");
print 
"<p align=\"center\">
<span class=\"text1\">
$lang[33].</span>

"
;
print 
"<a class=\"a1\" href=\"\">$lang[31]</a>";
print 
"</p>
"
;
print 
"<script language=\"Javascript\">\n";
print  
"window.location.href = \"index.php\";\n";
print 
"</script>";

include(
"inc/footer.inc.php");
exit;
}



include(
"inc/header2.inc.php");
?>



<?php


if ($_SESSION['username'] == "") {

if (
$referer != "" AND substr($referer,-9) != "login.php" AND substr($referer,-9) != "hp?step=2" AND substr($referer,-9) != "ister.php" AND $site_url == substr($referer,0,strlen($site_url)) AND substr($referer,-9) != "on=logout") {
$_SESSION[referer] = $referer;
}
else {
$_SESSION[referer] = "index.php";
}


?>
<form method="POST" action="login.php?step=2">
<table id="logintable" cellpadding="0" cellspacing="0" width="25%" style="border-collapse: collapse" class="table1" border="1">
<tr>
<td width="100%" colspan="2" align="left" class="td1">
<span class="text1" style="font-weight: bold">[img]themes/<?php print [/img]/log.gif" border="0" /> <?php print "$lang[34]"; ?></span>
</td>
</tr>
<tr>
<td width="
40%" class="td3" align="right">
<span class="
text1"><?php print "$lang[35]"; ?></span>
</td>
<td width="
70%" class="td3" align="left">
<input type="
text" class="txtbox" name="username" size="20" style="width140" />
</td>
</tr>
<tr>
<td width="
40%" class="td3" align="right">
<span class="
text1"><?php print "$lang[36]"; ?></span>
</td>
<td width="
70%" align="left" class="td3">
<input type="
password" class="txtbox" name="password" size="20" style="width140" />
</td>
</tr>
<tr>
<td width="
100%" colspan="2" align="center" class="td3">
<span class="
text1"><?php print "$lang[37]"; ?> <input type="checkbox" name="remember" value="yes" checked />
<input type="
hidden" name="ref" value="<?php print "$_SESSION[referer]"?>" />
</span>
</td>
</tr>
<tr>
<td width="100%" colspan="2" align="center" class="td3">
<input class="txtbox" type="submit" value="<?php print "$lang[38]"?>" />
</td>
</tr>
</table>
</form>
<p align="center">
<span class="text1"><?php print "$lang[39]"?> [url="register.php"]<?php print "$lang[40]"?>[/url] !</span>
</p>
<?php
}
else {
print_error("$lang[41]");
}
?>
<?php 
include("inc/footer.inc.php"); ?>