right I’m having a bit of an annoying bug here. Basically I’m using cookies to restrict access to my site (code shown below) when I first uploaded the files it all worked fine, then I went to test it again later and I couldn't access the site, kept redirecting to the login page (siteaccess.php), so I played around with it and ended up uploading it again exactly as it was before and it worked. then once I logged off and tried again I got the same bug... Its obviously something to do with the cookie but I can't c the problem, any help will be much appreciated as I really need to get this sorted!!!
Cheers Simon.
authenticate.php
PHP Code:
Code: Select all
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$self = $_SERVER['PHP_SELF'];
$referer = $_SERVER['HTTP_REFERER'];
#if either form field is empty return to the login page
if( ( !$username ) or ( !$password ))
{
#connect to MYSQL
$conn = @mysql_connect( "linuxproj", "sn202", "e1420518" )
or die( mysql_error() );
#select the specified database
$rs = @mysql_select_db ( "db_sn202", $conn )
or die( mysql_error() );
#create the sql query
$sql="select * from siteaccess where username='$username'
and password = '$password'";
#exercute the query
$rs = mysql_query( $sql, $conn )
or die( mysql_error() );
#get number of rows that match username and password
$num = mysql_numrows( $rs );
#if there is a match the login is authenticated
if( $num > 0 )
{
setcookie( "auth", "ok" );
header( "Location:roles.php" ); exit();
}
else #or return to login page
{header("Location: $referer") ; exit () ;}
}
?>code on all other pages
PHP Code:
Code: Select all
<?php
$auth = $_COOKIE['auth'];
header( "Cache-Control:no-cache" );
if( ! $auth == "ok" )
{ header( "location:siteaccess.htm" ); exit(); }
?>