Forum Moderators: coopster
// allow sessions to be passed so we can see if the user is logged in.
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// process login or logout request.
switch (CheckField($_POST['Submit'])) {
case 'login':
// check form fields to prevent sql database injection attack.
$username = CheckField($_POST['username']);
$password = CheckField($_POST['password']);
// did user fill the username and password fields?
if($username && $password){
//query the database
$sql = 'SELECT ur.URID, ur.UserName, ur.UTID, ut.UserType, ur.MEID, me.FirstName, me.LastName, ur.FRID, fr.FranchiseAbbrev, fr.FranchiseName';
$sql .= ' FROM '.$crudTable['user-ur'].' AS ur';
$sql .= ' LEFT JOIN '.$crudTable['franchise-fr'].' AS fr ON ur.FRID = fr.FRID';
$sql .= ' LEFT JOIN '.$crudTable['member-me'].' AS me ON ur.MEID = me.MEID';
$sql .= ' LEFT JOIN '.$crudTable['ur-type-ut'].' AS ut ON ur.UTID = ut.UTID';
$sql .= ' WHERE ur.UserName = "'.$username.'" AND ur.PassWord = "'.$password.'"';
$result = mysql_query($sql);
// did query succeed?
if($result){
// was the username and password found?
if(mysql_num_rows($result) == 1){
// put all fields of the record into an associative array.
$row = mysql_fetch_assoc($result);
// set the login session by storing the user's user id and login datatime in the session table. we use this to see if they are logged in or not.
if(!isset($_SESSION['Login'])){
$_SESSION['Login'] = true;
$_SESSION['URID'] = $row['URID'];// user id
$_SESSION['UserName'] = $row['UserName'];// user's login name
$_SESSION['UTID'] = $row['UTID'];// user's type id
$_SESSION['UserType'] = $row['UserType'];// user type
$_SESSION['MEID'] = $row['MEID'];// user's member id
$_SESSION['FirstName'] = $row['FirstName'];// user's member first name
$_SESSION['LastName'] = $row['LastName'];// user's member last name
$_SESSION['FRID'] = $row['FRID'];// user's franchise id
$_SESSION['FranchiseAbbrev'] = $row['FranchiseAbbrev'];// user's franchise code
$_SESSION['FranchiseName'] = $row['FranchiseName'];// user's franchise name
$_SESSION['LoginDate'] = date('M dS, Y h:ia');// user's login date & time
// construct login status message.
$response = status('You have successfully logged in! ','','Login Status: '.$_SESSION,true);
logMsg(__LINE__,'I',$response['message']);
}else{
// construct login status message.
$response = status('You are already logged in! ','','Login Status: '.$_SESSION,false);
logMsg(__LINE__,'I',$response['message']);
}
}else{
// construct user status message.
$response = status('Invalid Username and/or Password. ','','',false);
logMsg(__LINE__,'I',$response['message']);
}
.
.
.
break;
.
.
.
case 'logout':
$_SESSION['LogoutDate'] = date('M dS, Y h:ia');// user's logout date & time
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,$params["path"], $params["domain"], $params["secure"], $params["httponly"]);
}
// Finally, destroy the session.
session_destroy();
// construct user status message.
$response = status('You have successfully logged out! ','',$_SESSION['LogoutDate'],true);
logMsg(__LINE__,'I',$response['message']);
// Unset all of the session variables.
$_SESSION = array();
break;
.
.
.
}else{
// construct user status message.
$response = status('Invalid request method ',$_SERVER["REQUEST_METHOD"],'Login not processed.'.mysql_error(),false);
logMsg(__LINE__,'I',$response['message']);
}
// encode to json format and send output back to client side.
echo json_encode($response);
logMsg(__LINE__,'I','Send output back to client login javascript');
// close the connection to the MySQL server.
mysql_close($open);
// Flush (send) the output buffer and turn off output buffering.
ob_end_flush();
The client has three monitors attached to one pc and wants to be able to log into the site under different user names to view and manage different aspects of the site from each monitor at the same time.
Q1. With regard to the first question: "How to keep the user logged in when the page is refreshed (reloaded)", is this a session issue or a login javascript/phpscript issue?
Q2. As a point of interest, when i log into my newegg.com user account in one tab , it shows my name and a logout button in a box in the upper right hand corner of the page. If i enter the same url in another tab of the same browser, the second tab instance of newegg.com diosplays the same login box indicating that i'm logged in. Is newegg.com checking its browser cookie to see if i'm currently logged in and if so, sends me back a page with the same logged in box in the upper right hand corner?
if($_SESSION['loggedin'])
{
//show anything you want involved with being logged in
}
else
{
//show an error or redirect
echo 'You must be logged in to view this page';
//or redirect
header('Location: http://www.example.com/login.php');
}
Is newegg.com checking its browser cookie to see if i'm currently logged in and if so, sends me back a page with the same logged in box in the upper right hand corner?