Hi Ashlee,
You're getting closer..
session_start() and header() both modify outgoing HTTP headers. The former outputs a COOKIE and the latter, well whatever you're telling it to, in this case the Location header. The HTTP headers have to go out before any content from the body itself. So my guess is you have some output happening before the call to header().
<?php
session_start();
$dbc = mysql_connect('localhost', 'Ashleek007', '') or die ('Could not connect to MySQL :' .mysql_error());
mysql_select_db('login') or die('Could not connect to database :' .mysql_error());
$username = $_POST['username'];
$password = $_POST['password'];
// This creates output before your call to header.
<s>
?>
<br>
<?php
</s>
$query = "Select * From userlogin Where USERNAME = '$username' And PASSWORD = '$password'";
$result = mysql_query($query);
if($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
<s># echo "$row[USERNAME]$row[PASSWORD]";</s>
$_SESSION['logged_in'] = <s>'</s>TRUE<s>'</s>;
header('Location: index.php?sid='.session_id());
}
else
{
$_SESSION['logged_in'] = FALSE;
echo "Incorrect Username/password";
}
mysql_close();
?>
<?php
session_start();
// When you check for TRUE you don't have to quote "TRUE" its a
// reserved word with special meaning.
// Also you don't need the semi-colon in the conditional expression.
if ($_SESSION['logged_in'] == <s>'</s>TRUE<s>';</s>)
{
//users only pages code here?!?!?!
}
else
{
// header call first (cannot have output before it).
header('Location: login.php');
// Since you're redirecting this won't do anything.
<s>echo 'not logged in';</s>
exit;
}
?>
Right you would have to use it on every page that requires protection. I just talked a bit on this too.. are you using Apache? From your post it looks like it.. then can you use .htaccess? I believe you have to enable .htaccess configuration changes in httpd.conf, once you do that you can auto prepend and append authentication to files you want protected, whereas you won't have to write the check in every file.
This thread talks about configuring using .htaccess
http://p2p.wrox.com/topic.asp?TOPIC_ID=10392
You would use the regular unset() function to destroy a $_SESSION variable. If you want to get rid of a whole session use the session_destroy() function.
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::