Her er codene jeg henter session data fra:
Checklogin.php:
<?php
include ("db.php");
$tbl_name="user_login";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header('Location: menu.php');
}
else
{
header('Location: menu.php?error');
}
?>
Menu.php:
<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/menustyle.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div id="language">
<a href="http://www.hdkweb.com/uk" target="_top"><img border="0" src="images/flag_uk.jpg" Alt="Engelsk" onmouseover="src='images/flag_uk_link.jpg'" onmouseout="src='images/flag_uk.jpg'" width="28" height="18"></a>
<a href="http://www.hdkweb.com/" target="_top"><img border="0" src="images/flag_no.jpg" Alt="Norsk" onmouseover="src='images/flag_no_link.jpg'" onmouseout="src='images/flag_no.jpg'" width="28" height="18"></a>
</div>
<div id="menu">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<div><a href="hjem.htm" target="main" class="menu" >Hovedmeny</a></div>
<div><a href="hjem.htm" target="main" class="menutab">Hjem</a></div>
<div><a href="nyheter.htm" target="main" class="menutab">Nyheter</a></div>
<div><a href="prosjekter.htm" target="main" class="menutab">Prosjekter</a></div>
<div><a href="tjenester.htm" target="main" class="menutab">Tjenester</a></div>
<div><a href="omoss.htm" target="main" class="menutab">Om oss</a></div>
<div><a href="kontaktoss.htm" target="main" class="menutab">Kontakt oss</a></div>
</td>
</tr>
</table>
</div>
<br />
<?php
include("useronline.php");
?>
<?php
if(session_is_registered('myusername'))
{
echo "<div id='menu'>";
echo "<table cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td>";
echo "<div><a href='medlemmer.php' target='main' class='menu'>Brukermeny</a></div>";
echo "<div><a href='medlemmer.php' target='main' class='menutab'>Medlemmer</a></div>";
echo "<div><a href='prosjekt.php' target='main' class='menutab'>Prosjekt</a></div>";
echo "<div><a href='nedlastninger.php' target='main' class='menutab'>Nedlastninger</a></div>";
echo "<div><a href='filmer.php' target='main' class='menutab'>Filmer</a></div>";
echo "<div><a href='produkter.php' target='main' class='menutab'>Produkter</a></div>";
echo "<div><a href='kontakt.php' target='main' class='menutab'>Kontakt</a></div>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
if(!session_is_registered('myusername'))
{
echo "<br />";
echo isset($_GET['error'])?'<script type="text/javascript">alert("Feil brukernavn eller passord!");</script>' : '';
echo "<form method='post' action='checklogin.php'>";
echo "<label style='font-size:smaller'>Brukernavn:<br />";
echo "<p style='margin-top: 0; margin-bottom:0'>";
echo "<input size='15' name='myusername' type='text' id='myusername'></label></p>";
echo "<p style='margin-top:0; margin-bottom: 0;'>";
echo "<label style='font-size:smaller'>Passord:<br />";
echo "<input size='15' name='mypassword' type='password' id='mypassword'></label></p>";
echo "<p style='margin-top:0; margin-bottom: 0;'>";
echo "<input type='submit' value='Logg inn'></p><br />";
echo "</form>";
echo "<a href='forgot_password.php' target='main'><font size='2'><i>Glemt passord?</i></font></a>";
}
else
{
echo "<br />";
include("checkuser.php");
echo "<div id='user'>";
echo "Hei, $user";
echo "</div>";
echo "<br />";
echo "<form method='post' action='checklogout.php'>";
echo "<input type='submit' value='Logg ut'>";
echo "</form>";
}
?>
</body>
</html>