Forum Moderators: coopster

Message Too Old, No Replies

Cookie problem i think...?

         

sn202

12:08 am on Dec 20, 2004 (gmt 0)

10+ Year Member



Hi,

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:
<?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", "user", "pass" )
or die( mysql_error() );
#select the specified database
$rs = @mysql_select_db ( "somedb", $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:
<?php $auth = $_COOKIE['auth'];
header( "Cache-Control:no-cache" );
if(! $auth == "ok" )
{ header( "location:siteaccess.htm" ); exit(); }
?>

[edited by: jatar_k at 5:36 am (utc) on Dec. 20, 2004]
[edit reason] generalized login info [/edit]

dreamcatcher

2:55 am on Dec 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Simon,

Have you tried adding an expiry time to the cookie? If you don`t specify a cookie expiry time, the cookie will expire at the end of the session. ie, when the browser closes.

setcookie( "auth", "ok", time()+60*60*24*30);

This example is for 1 month.

dc :)