Intranet Journal

Go Back   IT Management Forum > Intranet Journal

Intranet Journal The new discussion forum for Intranet Journal readers. Leave comments and questions for IJ authors. Suggest story ideas and provide feedback.

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-23-2009, 05:16 AM
BoBBIDiGITAL BoBBIDiGITAL is offline
Registered User
 
Join Date: Jul 2009
Posts: 1
CMS TUT - Sentry not working?

Hi,

This is my first post as I am fairly new to PHP I have gotten to part 5 of the tutorial and I am having problems. I don't receive any error messages but the Sentry.php script is not doing it's job I can still go directly to the admin pages, without entering a the login details again. I thought sessions were destroyed after the browser was closed so I tried using the session_unset and session_destroy to clear them but these have not changed the situation. See the code for sentry.php and login.php below. I should mention also that the login page works it's just not redirected to this page if I try to access the newarticle page directly. Any help will be appreciated, Thanks in advance!

Ben

Login.php
PHP Code:
<?php
require_once("../includes/Sentry.php");

$sentry = new Sentry();
if (
$_POST['user'] != ''){
    
$sentry->checkLogin($_POST['user'],$_POST['pass'],4,'welcome.php','failed.php');
}

if (
$_GET['action'] == 'logout'){
    if (
$sentry->logout()){
        echo
'<center>You have been logged out</center><br>';
    }
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="25%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000066">
  <tr>
    <td align="center" bgcolor="#000066"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Login</strong></font></td>
  </tr>
  <tr>
    <td bordercolor="#FFFFFF"><form name="form1" method="post" action="login.php">
        <p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br>
&nbsp;User:
          <input type="text" name="user">
        </font></p>
        <p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;Pass:
              <input type="password" name="pass">
        </font></p>
        <p align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
          <input type="submit" name="Submit2" value="Submit">
        </font></p>
      </form>
        <div align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href="login.php?action=logout">Logout</a>&nbsp;</font></div>
    </td>
  </tr>
</table>
</body>
</html>
Sentry.php
PHP Code:
<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: sentry
// Purpose: Control access to pages
///////////////////////////////////////////////////////////////////////////////////////
class sentry {
    
    var
$loggedin = false;    //    Boolean to store whether the user is logged in
    
var $userdata;            //  Array to contain user's data
    
    
function sentry(){
        
session_start();
        
header("Cache-control: private");
    }
    
    
//======================================================================================
    // Log out, destroy session
    
function logout(){
        unset(
$this->userdata);
        
session_destroy();
        return
true;
    }

    
//======================================================================================
    // Log in, and either redirect to goodRedirect or badRedirect depending on success
    
function checkLogin($user = '',$pass = '',$group = 10,$goodRedirect = '',$badRedirect = ''){

        
// Include database and validation classes, and create objects
        
require_once('DbConnector.php');
        require_once(
'Validator.php');
        
$validate = new Validator();
        
$loginConnector = new DbConnector();
        
        
// If user is already logged in then check credentials
        
if ($_SESSION['user'] && $_SESSION['pass']){

            
// Validate session data
            
if (!$validate->validateTextOnly($_SESSION['user'])){return false;}
            if (!
$validate->validateTextOnly($_SESSION['pass'])){return false;}

            
$getUser = $loginConnector->query("SELECT * FROM cmsusers WHERE user = '".$_SESSION['user']."' AND pass = '".$_SESSION['pass']."' AND thegroup <= ".$group.' AND enabled = 1');

            if (
$loginConnector->getNumRows($getUser) > 0){
                
// Existing user ok, continue
                
if ($goodRedirect != '') {
                    
header("Location: ".$goodRedirect."?".strip_tags(session_id())) ;
                }            
                return
true;
            }else{
                
// Existing user not ok, logout
                
$this->logout();
                return
false;
            }
            
        
// User isn't logged in, check credentials
        
}else{    
            
// Validate input
            
if (!$validate->validateTextOnly($user)){return false;}
            if (!
$validate->validateTextOnly($pass)){return false;}

            
// Look up user in DB
            
$getUser = $loginConnector->query("SELECT * FROM cmsusers WHERE user = '$user' AND pass = MD5('$pass') AND thegroup <= $group AND enabled = 1");
            
$this->userdata = $loginConnector->fetchArray($getUser);

            if (
$loginConnector->getNumRows($getUser) > 0){
                
// Login OK, store session details
                // Log in
                
$_SESSION["user"] = $user;
                
$_SESSION["pass"] = $this->userdata['pass'];
                
$_SESSION["thegroup"] = $this->userdata['thegroup'];
                                
                if (
$goodRedirect) {
                    
header("Location: ".$goodRedirect."?".strip_tags(session_id())) ;
                }
                return
true;

            }else{
                
// Login BAD
                
unset($this->userdata);
                if (
$badRedirect) {
                    
header("Location: ".$badRedirect) ;
                }        
                return
false;
            }
        }            
    }
}    
?>
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 10:19 PM.





Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0