I'm trying to build a login system based on cookies that sets a users level and stores the users name. It is written as a class (which I'm new to) and works fine if I specify the url of the page specifically e.g. header("Location: index.php")
However I would really like the users to be able to login from any page on the site and then be redirected back to the same page. I tried using : header($_SERVER['PHP_SELF']);
which fails to log the user either in, or out(I have the same header info to kill the cookie)
on the first submission of the form. HOWEVER if I resubmit the page I then am logged in??
Can anyone explain this for me as I'm clueless as to what to do to remedy this
The code for the class is:
require_once("pathfinder.php");
class Login extends pathfinder{
//Set Vars
var $login_html;
var $cooky;
var $testusername;
var $errorMessage;
var $numRecords;
var $loggedin;
var $usertype;
var $location = "(\"Location: indexOOPCookies.php\")";
var $prepend;
// Constructor
function Login(){
$this->KillCookie();
$this->LoginLink = $this->DB_databaseConnect();
$this->TestVars();
$this->GetUserType();
$this->CreateLoginArray();
$this->Mkloginhtml();
} //Close Constructor
// Methods
// Kill the cookie when the user logs out
function KillCookie(){
if ($_POST['kill_cookie'] == "kill"){
setCookie("BCEPS_cookie","",time()-86400,"/");
setCookie("BCEPS_level","",time()-86400,"/");
// and then redirect back to itself
//header($_SERVER['PHP_SELF']);
header("Location: index.php");
}
} //Close the KillCookie class
// check username and passy are correct from the db entry.
function TestVars(){
if (isset($_POST['username'])) {
$this->testusername = $_POST['username'];
}
else{$this->testusername = "Username was not passed to the class";}
}
#####################################################################
# #
# Check Login, Set Cookie and Redirect else show Error Message #
# #
#####################################################################
function CreateLoginArray(){
$myUsername_rsLogin = "0";
if (isset($_POST['username'])) {
$myUsername_rsLogin = (get_magic_quotes_gpc()) ? $_POST['username'] : addslashes($_POST['username']);
}
$myPassword_rsLogin = "0";
if (isset($_POST['password'])) {
$myPassword_rsLogin = (get_magic_quotes_gpc()) ? $_POST['password'] : addslashes($_POST['password']);
$query_rsLogin = sprintf("SELECT username, password, userlevel FROM pusers WHERE username= '%s' AND password = '%s'", $myUsername_rsLogin,$myPassword_rsLogin);
$rsLogin = $this->DB_executeQuery($query_rsLogin,$this->LoginLink);
$row_rsLogin = mysql_fetch_assoc($rsLogin);
$this->numRecords = $this->DB_getrecords($rsLogin);
// Check Login, Set Cookie and Redirect else show Error Message
if($_POST['action']=="login"){
if($this->numRecords==0){
$this->$errorMessage = "Sorry we were unable to log you in please check your username and password details and try again. If the problem persists please report it";
mysql_free_result($rsLogin);
}
else {
mysql_free_result($rsLogin);
setCookie("BCEPS_cookie",$_POST['username'],'0',"/");
setCookie("BCEPS_level",$row_rsLogin['userlevel'],'0',"/");
$this->userlevel = $row_rsLogin['userlevel'];
// Expire Date: 0
// Expire Time: 0
// this code could be changed in order that it will login from any page and directory and go back in?
// and then redirect back to itself
//header($_SERVER['PHP_SELF']);
header("Location: index.php");
}
}
}
} //Close CreateLoginArray function
#############################################
# #
# Make desisions based on usertype here #
# #
#############################################
function GetUserType(){
switch($_COOKIE['BCEPS_level']){
case "pupil":
$this->loggedin = "<a href=\"classroom/index.php\"><img src=\"images/classroom_but.gif\" alt=\"Click here to enter the online classroom\" name=\"classup\" width=\"120\" height=\"17\" border=\"0\" id=\"classup\" onMouseOver=\"MM_swapImage('classup','','images/classroom_over.gif',1)\" onMouseOut=\"MM_swapImgRestore()\"></a>";
break;
case "parent":
$this->loggedin = "";
break;
case "governor":
$this->loggedin = "";
break;
case "admin":
$this->loggedin = "<a href=\"admin/index.php\"><img src=\"images/admin_but.gif\" alt=\"Click here to enter the admin section\" name=\"adminup\" width=\"80\" height=\"17\" border=\"0\" id=\"adminup\" onMouseOver=\"MM_swapImage('adminup','','images/admin_over.gif',1)\" onMouseOut=\"MM_swapImgRestore()\"></a>";
break;
case "superadmin":
$this->loggedin = "<a href=\"admin/index.php\"><img src=\"images/admin_but.gif\" alt=\"Click here to enter the admin section\" name=\"adminup\" width=\"80\" height=\"17\" border=\"0\" id=\"adminup\" onMouseOver=\"MM_swapImage('adminup','','images/admin_over.gif',1)\" onMouseOut=\"MM_swapImgRestore()\"></a>";
break;
default:
$this->loggedin = "";
} // Closes the switch statement
} //close GetUserType function
// this makes the login html once the user has logged in - simple atm and needs a switch dependant on usertype
function Mkloginhtml(){
if (isset($_COOKIE['BCEPS_cookie'])){
$login_html = $_COOKIE['BCEPS_cookie'] . " you are logged in.";
// add the code to give the option of logging out once logged in
$login_html .= "</td>\n";
$login_html .= "</tr>\n";
$login_html .= "<tr>\n";
$login_html .= "<td><div align=\"center\">\n";
$login_html .= "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" name=\"cookkill\" id=\"cookkill\">";
$login_html .= "<input name=\"Submit2\" type=\"image\" value=\"Logout\" src=\"images/logout.gif\" alt=\"Log Out of the System\">\n";
$login_html .= "<input name=\"kill_cookie\" type=\"hidden\" id=\"kill_cookie\" value=\"kill\">\n";
$login_html .= "</form>\n";
$login_html .= $this->loggedin;
//Close first tr tags
$login_html .= " </div>\n </td>
</tr>\n";
//Output all the html
$this->login_html = $login_html;
return $this->login_html;
}
//If the user isnt logged in show the not logged in info
else {
$login_html = "You are not currently logged in";
$login_html .= "</td>\n";
$login_html .= "</tr>\n";
$login_html .= "<tr>\n";
$login_html .= "<td>\n<div align=\"center\"><img src=\"images/login_now.gif\" ";
// add the alt text
$login_html .= "alt=\"To Login to the Website CLICK HERE\" name=\"log_up\" width=\"80\" height=\"17\" class=\"cursor\" id=\"log_up\" ";
//add the onClick Stuff
$login_html .= "onClick=\"MM_showHideLayers('login','','show','notlogin','','hide')\" onMouseOver=\"MM_swapImage('log_up','','images/login_over.gif',1)\" onMouseOut=\"MM_swapImgRestore()\">";
//Close our tags
$login_html .= " </div>\n</td>
</tr>\n";
if(isset($this->$errorMessage)){
$login_html .= "<tr>\n";
$login_html .= "<td class=\"logintd\"\n>";
$login_html .= $this->$errorMessage;
$login_html .= "</td>\n";
$login_html .= "</tr>\n";
}
//Output all the html
$this->login_html = $login_html;
return $this->login_html;
}
} //Close the MkLoginhtml function
} // Close the login class