LOGIN.PHP
<?php include('header.php'); ?>
<tr valign="top" height="20">
<td valign="top" height="20">
<?php
$path = $_SERVER['PHP_SELF'];
$title = basename($path); // $file is set to "index.php"
$title = basename($path, ".php"); // $file is set to "index"
echo "<h1>$title</h1>";
?>
</td>
</tr>
<tr>
<td valign="top" width="580">
<?php
if ($_POST) { //If user has submitted data from form
if ($_POST['username'] == "" || $_POST['password'] == "") { //If any of the fields are empty
$error = "One or more fields are empty, please fill in all the fields";
} else { //Fields are not empty
session_start(); // Needed to initialise session variables (i.e. start session :D)
$_SESSION['username'] == $_POST['username'];
$_SESSION['password'] == $_POST['password'];
header("location: admin.php?sid=".strip_tags(session_id())."); //Redirect to protected.php, but can be whatever you want (your staff index page)
}
}
if (isset($_GET['id'])) {//if the id var exists in the url
$error = "One or more fields you have entered are incorrect, please try again";
}
?>
<form method="POST" action="login.php">
<p>Username:<input type="text" name="username" size="20"></p>
<p>Password: <input type="password" name="password" size="20"></p>
<p><input type="submit" value="Login" name="Submit"></p>
</form>
<?php
echo $error;
?>
</td>
<td valign="top" width="136"><h3>Affiliates:</h3><br /><?php include('affiliates.php'); ?></td>
</tr>
<?php include('footer.php'); ?>
FUNCTIONS.PHP
<?php
$database = "REMOVED";
$dbusername = "REMOVED";
$dbpassword = "REMOVED";
$dbhost = "localhost";
function connectToDatabase() {
global $database, $dbusername, $dbpassword, $dbhost; //make the vars accessible from inside the function
$link = mysql_connect($dbhost, $dbusername, $dbpassword) or die('Could not connect: ' . mysql_error()); //connect
mysql_select_db($database) or die('Could not select database'); //select the db to use
}
?>
<?php
function authenticateUser() {
session_start();
$query = "SELECT username FROM users WHERE username = '".$_SESSION['username']."' AND password = '".md5($_SESSION[password])."';"; //Select from database the username & password the user entered
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); //Query the db
if (!isset($_GET['sid'])) { // If the sid URL var exists in the url
header("location: login.php?id=1"); //does not exists
} else { //sid exists
if ($_GET['sid'] != session_id()) { //check URL sid with current sid
header("location: login.php?id=1"); //does not match
}
}
if (!mysql_num_rows($result)) { //If no match
header("location: login.php?id=0"); //redirect to login page with username/password error
}
}
?>
ANY PAGE THAT IS PROTECTED (MUST LOGIN FIRST)
<?php
include_once("functions.php");
connectToDatabase();
authenticateUser();
?>
<!-- continue page here -->
There errors I get are the following:
When attempting to load the page login.php:
Quote
When attempting to load a protected page (eg admin.php):
Quote
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/djill/public_html/v2/functions.php:14) in /home/djill/public_html/v2/functions.php on line 16
Warning: Cannot modify header information - headers already sent by (output started at /home/djill/public_html/v2/functions.php:14) in /home/djill/public_html/v2/functions.php on line 20
Warning: Cannot modify header information - headers already sent by (output started at /home/djill/public_html/v2/functions.php:14) in /home/djill/public_html/v2/functions.php on line 27
<!-- Entire page shows -->
Thanks very much!
