I have pages on my website which are intended only for authorised users. So I have created a function which checks a cookie to see if the user is logged in:
function checklogin($successurl) {
if (! $_COOKIE['loggedin'] == "Y" ) {
header("Location:../login.php?gotourl=$successurl"); exit();
} else {
return "Y";
}
}
I realise that this cookie isn't very secure, but that's not the issue just yet.
The login.php script on loading for the first time will display a form asking for User ID and Password. The form is METHOD=POST ACTION=$PHP_SELF so it tests to see if the 'login' button has been pressed and then attempts to login.
The login is simple, it just looks up the user table for any rows matching user ID and password. Then the script checks that something was found, if so it sets the cookie and redirects back to the original intended page:
if ($numrows != 0) {
setcookie("loggedin", "Y", time()+86400);
header("Location:$gotourl");}
else {
echo "Failed to login " . $userid . ".";
echo "<BR>";
echo "Try again.......";}
Obviously once it goes back to the original page it performs the function to check the cookie again which should be successful and return to the page to finish off.
My problem is........ I have to enter the user ID and password twice correctly before it proceeds ! I'm guessing this is something to do with the cookie not being passed correctly when the header() function is called, but I really don't get it.
Any suggestions.
Is there a way I can post my code in full to make it clearer?