Hello, I am fairly new at working with sessions, and wondering if you guys could help. I currently have this code:
<?php
// FUNCTION TO DISPLAY LOGIN MENU
function login()
{
echo
'<form class="login" method="post" action="' . $PHP_SELF . '">
<table class="login">
<tr><td class="loginlabel">Username:</td><td> <input class="username" type="text" maxlength="255" name="username"/></td></tr>
<tr><td class="loginlabel">Password:</td><td> <input class="password" type="password" maxlength="255" name="password"/></td></tr>
<tr><td colspan="2"><input class="submit" type="submit" value="Login" name="login"/></td></tr>
</table>
</form>';
}
if ($_SESSION["username"]=="" AND $_POST["username"]!="" and $_POST["login"]!="")
{
$_SESSION["username"] = $_POST["username"];
session_register("username");
}
else
{
$_SESSION["username"] = $_SESSION["username"];
session_register("username");
}
if ($_SESSION["password"]=="" AND $_POST["password"]!="" and $_POST["login"]!="")
{
$_SESSION["password"] = md5($_POST["password"]);
session_register("password");
}
else
{
$_SESSION["password"] = $_SESSION["password"];
session_register("password");
}
// IF SUBMITTED THEN CHECK
if($login!="")
{
$query = "SELECT * FROM users WHERE username = '" . $_SESSION["username"] . "' AND password = '" . $_SESSION["password"] . "'";
$answer = mysql_query($query,$connection);
if(mysql_num_rows($answer)>0)
{
echo "Match";
}
else
{
login();
echo'<table class="login"><tr><td class="loginbad">You have entered a bad password and username combination.</td></tr></table>';
}
}
else
{
$query = "SELECT * FROM users WHERE username = '" . $_SESSION["username"] . "' AND password = '" . $_SESSION["password"] . "'";
$answer = mysql_query($query,$connection);
if(mysql_num_rows($answer)>0)
{
echo "Session login registered...";
}
else
{
login();
}
}
?>
Now this script appears on every page of my site, and I want it so that whenever the user logs on a page it will display stuff (ie "Match") but also when the user goes to another page I want it to pick up that they are already logged in and will therefore display "Session login registered........" I have been tryin to get my head around this for ages, and I can't quite get it to work - regards - Mario.
edit: you can view it in action here.
edit: btw I have session_start(); in a separate file, but that file includes this file. So.. session_start(); is in my template, which then has include("login.php");