I'm rewriting some of my scripts using sessions and removing the need for global variables. However, since I've never done this before, I'm having some concerns about whether or not I'm actually doing this right. Here is my code:
This is the main page, where you actually type in your login information. I'm looking for a way to slightly simplify this:
<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
require("header.php");
require("functions.php");
if (isset($_POST['user'])) {
if (authenticate($_POST['user'],$_POST['pass'])) {
dispmenu();
}
}
elseif (isset($_SESSION['username'])) {
if (authenticate($_SESSION['username'],$_SESSION['password'])) {
dispmenu();
}
}
else {
login();
}
?>
Here is the function authenticate. This is where the security comes into play, and where I hope most of the suggestions refer to:
function authenticate($usernew, $passnew) {
global $myrow;
$cryptpass=crypt($passnew,$usernew);
if (isset($usernew) and isset($passnew)) {
$result = mysql_query("SELECT * FROM members WHERE username = \"$usernew\"");
$myrow = mysql_fetch_array($result);
if ($myrow != NULL) {
extract($myrow);
}
if ($usernew == $myrow['username'] and $cryptpass == $myrow['password']) {
$_SESSION['username'] = $usernew;
$_SESSION['password'] = $passnew;
return 1;
}
}
if ($passnew != $myrow['password']) {
echo("Password incorrect.");
}
}
Thanks in advance.