2013-10-18 56 views
0

我發現用戶登錄腳本在線這是我後來foundd出被寫在PHP4,我在將其升級到PHP5,並在同一時間:)

的片斷學習OOP的過程我的用戶級別是

<?php 
session_start(); //Tell PHP to start the session 
include("include/database.php"); 
include("include/mailer.php"); 
include("include/form.php"); 

include("constants.php"); 

class user 
{ 
var $username;  //Username given on sign-up 
var $firstname; 
var $lastname; 
var $userid;  //Random value generated on current login 
var $userlevel; //The level to which the user pertains 
var $time;   //Time user was last active (page loaded) 
var $logged_in; //True if user is logged in, false otherwise 
var $userinfo = array(); //The array holding all user info 
var $url;   //The page url current being viewed 
var $referrer;  //Last recorded site page viewed 
var $num_active_users; //Number of active users viewing site 
var $num_active_guests; //Number of active guests viewing site 
var $num_members;  //Number of signed-up users 

/** 
* Note: referrer should really only be considered the actual 
* page referrer in process.php, any other time it may be 
* inaccurate. 
*/ 

public function __construct(db $db, Form $form) 
{ 
    $this->database = $db; 
    $this->form = $form; 
    $this->time = time(); 
    $this->startSession(); 

    $this->num_members = -1; 

    if(TRACK_VISITORS) 
    { 
     /* Calculate number of users at site */ 
     $this->calcNumActiveUsers(); 

     /* Calculate number of guests at site */ 
     $this->calcNumActiveGuests(); 
    } 


}  
/** 
* startSession - Performs all the actions necessary to 
* initialize this session object. Tries to determine if the 
* the user has logged in already, and sets the variables 
* accordingly. Also takes advantage of this page load to 
* update the active visitors tables. 
*/ 
function startSession() 
{ 

    /* Determine if user is logged in */ 
    $this->logged_in = $this->checkLogin(); 

    /** 
    * Set guest value to users not logged in, and update 
    * active guests table accordingly. 
    */ 
    if(!$this->logged_in) 
    { 
     $this->username = $_SESSION['username'] = GUEST_NAME; 
     $this->userlevel = GUEST_LEVEL; 
     $this->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time); 
    } 
    /* Update users last active timestamp */ 
    else 
    { 
     $this->addActiveUser($this->username, $this->time); 
    } 

    /* Remove inactive visitors from database */ 
    $this->removeInactiveUsers(); 
    $this->removeInactiveGuests(); 

    /* Set referrer page */ 
    if(isset($_SESSION['url'])) 
    { 
     $this->referrer = $_SESSION['url']; 
    } 
    else 
    { 
     $this->referrer = "/"; 
    } 
    /* Set current url */ 
    $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF']; 
} 

/** 
* checkLogin - Checks if the user has already previously 
* logged in, and a session with the user has already been 
* established. Also checks to see if user has been remembered. 
* If so, the database is queried to make sure of the user's 
* authenticity. Returns true if the user has logged in. 
*/ 
function checkLogin() 
{ 
    /* Check if user has been remembered */ 
    if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])) 
    { 
     $this->username = $_SESSION['username'] = $_COOKIE['cookname']; 
     $this->userid = $_SESSION['userid'] = $_COOKIE['cookid']; 
    } 

    /* Username and userid have been set and not guest */ 
    if(isset($_SESSION['username']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME) 
    { 
     /* Confirm that username and userid are valid */ 
     if($this->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0) 
     { 
      /* Variables are incorrect, user not logged in */ 
      unset($_SESSION['username']); 
      unset($_SESSION['userid']); 
      return false; 
     } 

     /* User is logged in, set class variables */ 
     $this->userinfo = $this->getUserInfo($_SESSION['username']); 
     $this->username = $this->userinfo['username']; 
     $this->userid = $this->userinfo['userid']; 
     $this->userlevel = $this->userinfo['userlevel']; 
     $this->lastlogin = $this->userinfo['lastlogin']; 
     $this->townid = $this->userinfo['placeID']; 

     return true; 
    } 
    /* User not logged in */ 
    else 
    { 
     return false; 
    } 
} 
} 
$db = new db($config); 
$form = new Form; 
$user = new User($db, $form); 

但我已被告知var $ username;等不是很安全,不應該使用,所以我在這裏問什麼,我應該使用呢?

我是否對每個var都這樣做?

private $username; 

/** 
* @return the $username 
*/ 
public function getUsername() { 
    return $this->username; 
} 

/** 
* @param $newUsername 
* the username to set 
*/ 
public function setUsername($newUsername) { 
    $this->username = $newUsername; 
} 

感謝

+0

檢出http://itsphptutorial.wordpress.com/access-modifiers-in-php/ –

+0

與您的用戶類有關的OOP的問題是它違反了[單一責任原則](http://en.wikipedia .org/wiki/Single_responsibility_principle)(會話處理,數據庫訪問等)。如果你真的想在PHP中學習OOP,我建議你開始使用一個框架,例如[Symfony2](http://symfony.com/),它有一個[出色的文檔](http://symfony.com/doc/current/index.html)。 – nietonfir

回答

0

var相當於public。通過創建所有成員變量private並向其中的每一個添加getter(但不包括setter),您就可以實現這一目標,以便使用API​​的其他開發人員不會[意外]更新這些值。這就是「安全」的含義 - 如果你沒有用正確的隱私級別聲明它,那麼就不會有人能夠侵入你的服務器或訪問數據。

如果你還想添加一個setter,我會說你在浪費你的時間(儘管其他人會不同意我)。無論如何,你已經給他們完整的統治權。唯一的好處是,如果您決定要以不同的方式存儲數值,則可以在路上沿着getter/setter擠出一些其他計算。

*雖然另一名開發人員可能會意外暴露他不應該的信息,例如密碼。

+0

謝謝馬克解釋說,它明確幫助我更清楚地理解它:) – user2886669