Jump to content


Photo

the meaning of "->"


  • Please log in to reply
6 replies to this topic

#1 Fantast

Fantast
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 July 2006 - 08:04 PM

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
$this->db->query($sql);

by example. Could anyone please try to explain me how the "->" signs work?

many thanks in advance,
Fantast~

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 July 2006 - 08:07 PM

1) "safe" is a tentative word at best.  Hackers are always more apt to attack products with a bigger user base.  Pear might be one of these, but I haven't heard of many people having problems with the Pear package.  I personally use my own code to connect to MySQL, but I'm picky about my code and I don't like to use other people's stuff.

2) http://www.phpfreaks...ic,95867.0.html

Info: PHP Manual


#3 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 05 July 2006 - 08:09 PM

I think you should use PEAR, just because it is safer and also easier; Ober aside, I don't believe we should all spend vast amounts of energy solving the same problems over and over, now then to answer your question...

$db->query ();

that would mean that $db is an object and query is a method (function) for that object.  The statement would be saying, execute query which belongs to the object db.  Objects can also have attributes, say "name".  Sometimes you can access the attributes using the -> symbol too. So,

echo $db->name;

That might print the name of the object.  It would be worth reading up on objects if you plan on doing significant programming later on.  By the way the variable $this is a special case used in classes.  Basically it is referring to itself.  So I am not sure where the $this came from, but if you chose to use it I would try another variable name (unless you are creating a class that has the $db as one of its attributes... but then why are you asking about ->?)
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 July 2006 - 08:17 PM

Solving the same problem over and over is one thing.  Doing it once myself so I understand exactly what is going and then re-using my own code is another.  Being able to modify and change and understand those changes to my own code is priceless.

Info: PHP Manual


#5 Fantast

Fantast
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 July 2006 - 09:12 PM

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 (http://mosaic.stud.tue.nl/), 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:

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']);


#6 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 05 July 2006 - 09:47 PM

So, what exactly is your question?  The $this variable just refers to the object itself.  So if you're working with a method like _save () it would be something like this...
public function _save ($data)
{
// data is a parameter that is passed to the method
$_query = // make some SQL query maybe using data
$this->db->query($_query); // ok so I'm using this, it is basically saying "Use the object's "db" 
// attribute to do the following... in this case query
return $something; // that exits the method and returns the value something for use
}

Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 July 2006 - 05:03 AM

Solving the same problem over and over is one thing.  Doing it once myself so I understand exactly what is going and then re-using my own code is another.  Being able to modify and change and understand those changes to my own code is priceless.

:thumbs up:

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users