Jump to content

Fantast

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Fantast's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I see. I will need to do some reading about onjects and attributes indeed. However, I find it more effective to learn by doing than by just reading. So after i modified a piece of code which uses pear for logging in ([url=http://mosaic.stud.tue.nl/]http://mosaic.stud.tue.nl/[/url]), I'd like now to understand exactly how it works. I have done some oop in the past in a few simple java programs, but have never spent enough time in it to learn the structre well enough. But as far as i remember, in Java the "." was used instead of the ->, right? anyway, i was trying to use the pear connection once again for entering the data in the db. I'm not sure yet how $this works in that class, but i was just about to try figuring that out~ these are parts of the code which i use for login: [code] class User { var $db = null; // PEAR::DB pointer var $failed = false; // failed login attempt var $date; // current date GMT var $personalID = 0; // the current user's id function User(&$db) { $this->db = $db; $this->date = $GLOBALS['date']; if ($_SESSION['logged']) { $this->_checkSession(); } elseif ( isset($_COOKIE['mtwebLogin']) ) { $this->_checkRemembered($_COOKIE['mtwebLogin']); } } function _logout() { $_SESSION['logged'] = false; [...] } function _checkLogin($email, $password, $remember) { $email = $this->db->quote($email); $password = $this->db->quote(md5($password)); $sql = "SELECT * FROM login_info WHERE " . "email = $email AND " . "password = $password "; $result = $this->db->getRow($sql); if ( is_object($result) ) { $this->_setSession($result, $remember); return true; } else { $this->failed = true; $this->_logout(); return false; } } function _setSession(&$values, $remember, $init = true) { $this->personalID = $values->personalID; $_SESSION['personalID'] = $this->personalID; [...] if ($remember) { $this->updateCookie($values->cookie, true); } if ($init) { $session = $this->db->quote(session_id()); $ip = $this->db->quote($_SERVER['REMOTE_ADDR']); $sql = "UPDATE login_info SET session = $session, ip = $ip WHERE " . "personalID = $this->personalID"; $this->db->query($sql); } } function updateCookie($cookie, $save) { $_SESSION['cookie'] = $cookie; if ($save) { $cookie = serialize(array($_SESSION['email'], $cookie) ); setcookie('mtwebLogin', $cookie, time() + 31104000, '/directory/'); } } function _checkRemembered($cookie) { list($email, $cookie) = @unserialize($cookie); if (!$email or !$cookie) return; $email = $this->db->quote($email); $cookie = $this->db->quote($cookie); $sql = "SELECT * FROM login_info WHERE " . "(email = $email) AND (cookie = $cookie)"; $result = $this->db->getRow($sql); if (is_object($result) ) { $this->_setSession($result, true); } } function _checkSession() { $email = $this->db->quote($_SESSION['email']); $cookie = $this->db->quote($_SESSION['cookie']); $session = $this->db->quote(session_id()); $ip = $this->db->quote($_SERVER['REMOTE_ADDR']); $sql = "SELECT * FROM login_info WHERE " . "(email = $email) AND (cookie = $cookie) AND " . "(session = $session) AND (ip = $ip)"; $result = $this->db->getRow($sql); if (is_object($result) ) { $this->_setSession($result, false, false); } else { $this->_logout(); } } } $date = gmdate("'Y-m-d'"); $db = db_connect(); $user = new User($db); $user->_checkLogin($_POST['email'], $_POST['password'], $_POST['remember']); [/code]
  2. Hey. Since short im using pear to connect to the MySql databse, simply because i heard it is safer to connect this way. So, being totally new to PEAR, i had two questions: 1) is connecting to a MySQL database through pear indeed the safest way to do so? 2) i found that "->" is used constantly and throughout the script, as in [code]$this->db->query($sql);[/code] by example. Could anyone please try to explain me how the "->" signs work? many thanks in advance, Fantast~
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.