<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>designing4u.de &#187; object oriented programming</title>
	<atom:link href="http://www.designing4u.de/tag/object-oriented-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>User registration class &#8211; simple authorization of user</title>
		<link>http://www.designing4u.de/2008/05/registration-class-simple-authorization-of-user/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=registration-class-simple-authorization-of-user</link>
		<comments>http://www.designing4u.de/2008/05/registration-class-simple-authorization-of-user/#comments</comments>
		<pubDate>Fri, 16 May 2008 13:23:32 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[authorization]]></category>
		<category><![CDATA[authorization class]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[registration]]></category>
		<category><![CDATA[registration class]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=14</guid>
		<description><![CDATA[This days almost any site is providing the users a simple registration to provide certain services only for registered users. This way you can collect their e-mails and keep in touch with them. If you planing on developing a social platform or just web 2.0 project you need to authorize your users and ask them [...]]]></description>
			<content:encoded><![CDATA[<p>This days almost any site is providing the users a simple registration to provide certain services only for registered users. This way you can collect their e-mails and keep in touch with them. If you planing on developing a social platform or just web 2.0 project you need to authorize your users and ask them to provide a valid e-mail address. In this simple example I will show you, how you can do that using this simple class.<br />
<span id="more-14"></span><br />
This example doesn't provide out of box solution for your site and you need to have at least a little bit knowledge in object oriented programming to adjust it to your needs. In this example I wont use a configuration file and the configuration will be done in constructor method, however it is highly recommended that you create this file to keep maintaining your project fast and easy.</p>
<p>Let's start with defining our class, some variables and the constructor.<br />
[source:php]<br />
class Register {</p>
<p>	//@string name of table, where the information will be stored<br />
	private $table_name;<br />
	//@array holds user input<br />
	private $input;<br />
	//@string subject of confirmation email<br />
	private $mail_subject;<br />
	//@string body of confirmation email<br />
	private $mail_body;<br />
	//@bool html mail true/false<br />
	private $mail_html;<br />
	//@array displays error messages<br />
	public $error;</p>
<p>	function __construct() {<br />
		$con = mysql_connect('localhost','root','pass') or die(mysql_error());<br />
		if($con) {<br />
			mysql_select_db('test',$con);<br />
		}<br />
		$this->table_name = "test_user";<br />
		$this->mail_subject = "confirmation mail";<br />
		$this->mail_body = "Here comes the text of confirmation e-mail. Click on this link %s to confirm your email.";<br />
		$this->mail_html = true;<br />
	}<br />
}<br />
[/source]<br />
$this->table_name is a string property, which holds information about MySQL table, in which we will save our new registered user. $this->input is an array, in which we will save the user input. As you will see later, we do that, to make the user input safe for MySQL INSERT function. $this->mail_subject and $this->mail_body are two properties, which will be responsible for holding the information we will later send to our new user after successful registration. $this->mail_html is a boolean property, which tells our script to either send HTML or plain text e-mail after successful registration. $this->error property is an array, which we will use to display all errors to the user, which our script will generate. In our constructor method we initialize data base connection and all the properties we will later use in our class. Let's move farther to our registration method.<br />
[source:php]<br />
public function registerUser() {<br />
	foreach($_POST as $k=>$v) {<br />
		$this->input[$k] = mysql_real_escape_string($v);<br />
	}<br />
	//Check users input<br />
	if($this->checkInput()) {<br />
		//Check, if username exists already<br />
		if($this->checkUser('username')) {<br />
			//Check if email exists already<br />
			if($this->checkUser('email')) {<br />
				//save user in database<br />
				if($this->insertUser()) {<br />
					//send mail with confirmation link<br />
					if($this->sendMail()) {<br />
						$this->success = "Thanks for registration. Check your e-mail for further details.";<br />
						return true;<br />
					} else {<br />
						//revert changes in database<br />
						$this->deleteUser();<br />
						$this->error[] = "something went wrong, please try again later send mail";<br />
						return false;<br />
					}<br />
				} else {<br />
					$this->error[] = "something went wrong, please try again later insert user";<br />
					return false;<br />
				}<br />
			} else {<br />
				$this->error[] = "email exists";<br />
				return false;<br />
			}<br />
		} else {<br />
			$this->error[] = "username exists";<br />
			return false;<br />
		}<br />
	} else {<br />
		return false;<br />
	}<br />
}<br />
[/source]<br />
We define this method as public because we will call it after our user clicks the submit button. We than loop through the $_POST variable and make the input safe against MySQL injection. First we check, if all the input is valid and meets our expectations according to password length, valid email etc. After that we provide a user check against the information saved in our database to determine, if an user with provided username or e-mail exists in our database. If it does we display corresponding error message. If our script passes this check we save new user in our database and send an e-mail with activation link. If our script return true after sending an activation e-mail we display a success message for our user with prompt to check his/hers e-mail and activate account. Simple logic, you can probably find in each user authorization. Let's take a closer look at corresponding methods, which I just mentioned:<br />
[source:php]<br />
private function checkInput() {<br />
	if(strlen($this->input['username']) < 5 || $this->input['username'] == '') {<br />
		$this->error[] = "username too short";<br />
	}<br />
	if(strlen($this->input['username']) > 16) {<br />
		$this->error[] = "username too long";<br />
	}<br />
	if($this->input['password'] == '' || $this->input['password1'] == '') {<br />
		$this->error[] = "you need to provide a password";<br />
	}<br />
	if($this->input['password'] != $this->input['password1']) {<br />
		$this->error[] = "password mismatch";<br />
	} else {<br />
		$this->input['password'] = md5($this->input['password']);<br />
	}<br />
	return (is_array($this->error) &#038;& !empty($this->error)) ? false : true;<br />
}<br />
[/source]<br />
This method validates user input. It is just a simple example and it doesn't validate the e-mail address. You should feel free to add any other validations you can imagine to this method. In the last step we check, if provided passwords match and if they do, we encode the password with md5() function. We do that to ensure the user, that his or hers data wont be abused later, when we use it for example to save it in session variables and check against database information to authenticate the user. If $this->error is an array and it's not empty, it means user input is not valid and we return false, otherwise true.<br />
[source:php]<br />
private function checkUser($case='') {<br />
	switch($case) {<br />
		case 'username':<br />
			$sql = mysql_query("SELECT COUNT(*) AS exist FROM ".$this->table_name." WHERE username='".$this->input['username']."';");<br />
			break;<br />
		case 'email':<br />
			$sql = mysql_query("SELECT COUNT(*) AS exist FROM ".$this->table_name." WHERE email='".$this->input['email']."';");<br />
			break;<br />
		default:<br />
			break;<br />
	}<br />
	return (mysql_result($sql,0,'exist') > 0) ? false : true;<br />
}<br />
[/source]<br />
In this method we check, if username or password exists in database. If it does we return false, otherwise true. At this point I should probably mention that AJAX gives you possibility to perform this validation on client side. Server side validation is just another check to ensure that provided data is really that, what we expect to save in our database. If you want to use this class in your AJAX request to perform checks on user input, you would have to change most of the methods to public and probably write another method which will set user input you will later use to perform those checks.<br />
[source:php]<br />
private function insertUser() {<br />
	$sql = mysql_query("INSERT INTO ".$this->table_name." (id, username, password, email, confirmed) VALUES (NULL,'".$this->input['username']."','".$this->input['password']."','".$this->input['email']."','0');") or die(mysql_error());<br />
	return ($sql) ? true : false;<br />
}</p>
<p>private function deleteUser() {<br />
	$sql = mysql_query("DELETE FROM ".$this->table_name." WHERE username='".$this->input['username']."' AND email='".$this->input['email']."';");<br />
}<br />
[/source]<br />
InsertUser and deleteUser are just simple methods to either save of delete a user from the database.<br />
[source:php]<br />
private function sendMail() {<br />
	if($this->email_html) {<br />
		$header = 'MIME-Version: 1.0' . "\r\n";<br />
		$header.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";<br />
		$header.= 'From: "yourmail@domain.com"<yourmail@domain.com>' . "\r\n";<br />
	} else {<br />
		$header =  'From:yourmail@domain.com';<br />
	}</p>
<p>	$body = sprintf($this->mail_body,"www.yourdomain.com?activation=youractivationcode");</p>
<p>	return (@mail($this->input['email'], $this->mail_subject, $body, $header)) ? true : false;<br />
}<br />
[/source]<br />
The last method replaces %s with the confirmation link, sends a confirmation mail and returns true on success and false in case of any errors. That's all. As I already mentioned this class doesn't provide a ready solution for your internet site and it should be adjusted to your needs. It's just a schema you can always start with, when you writing applications, which provide user registration. At the end lets take a closer look, how you should use this class.<br />
[source:php]<br />
if(isset($_POST['submit']) &#038;& $_POST['submit'] == 'register') {<br />
	include('register.class.php');<br />
	$register = new Register;<br />
	if($register->registerUser()) {<br />
		echo (isset($register->success)) ? $register->success : '';<br />
	} else {<br />
		foreach($register->error as $error) {<br />
			echo $error."<br />";<br />
		}<br />
		include('form.php');<br />
	}<br />
} else {<br />
	include('form.php');<br />
}<br />
[/source]<br />
We include and instantiate our class only in case, when our clicks a submit button. We then call the registerUser method and display success or error message depending on the case. You might also provide a redirection after successful registration to prevent the users from reloading the page. However our class would retrieve an error message saying that username already exists, it is just a nice way to do the things. And here the code of form.php in case you need it:<br />
[source:html]</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"><br />
	<label for="username">Username</label></p>
<input type="text" name="username" id="username" value="<? echo (isset($_POST['username'])) ? $_POST['username'] : '';?>" /><br />
	<label for="email">E-Mail</label></p>
<input type="text" name="email" id="email" value="<? echo (isset($_POST['email'])) ? $_POST['email'] : '';?>" /><br />
	<label for="password">Password</label></p>
<input type="password" name="password" id="password" />
	<label for="password1">Repeat password</label></p>
<input type="password" name="password1" id="password1" />
	<label>&nbsp;</label></p>
<input type="submit" name="submit" value="register" />
</form>
<p>[/source]<br />
And the table structure:<br />
[source:sql]<br />
CREATE TABLE IF NOT EXISTS `test_user` (<br />
  `id` int(11) NOT NULL auto_increment,<br />
  `username` varchar(50) collate latin1_general_ci NOT NULL,<br />
  `password` varchar(32) collate latin1_general_ci NOT NULL,<br />
  `email` varchar(255) collate latin1_general_ci NOT NULL,<br />
  `confirmed` enum('0','1') collate latin1_general_ci NOT NULL,<br />
  PRIMARY KEY  (`id`),<br />
  UNIQUE KEY `username` (`username`)<br />
)<br />
[/source]<br />
I'm waiting for your comments about this one. Cheers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/registration-class-simple-authorization-of-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class for picture upload with fix width and height</title>
		<link>http://www.designing4u.de/2008/05/class-for-picture-upload-with-fix-width-and-height/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=class-for-picture-upload-with-fix-width-and-height</link>
		<comments>http://www.designing4u.de/2008/05/class-for-picture-upload-with-fix-width-and-height/#comments</comments>
		<pubDate>Wed, 14 May 2008 12:06:52 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[image upload]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=9</guid>
		<description><![CDATA[For one of my web 2.0 projects I had to develop a solution for users to upload their images with fixed width and height. Basically my customer wanted to display profile images on the start page, but the requirement was to display each of them in a div container with fixed width and height. Because [...]]]></description>
			<content:encoded><![CDATA[<p>For one of my web 2.0 projects I had to develop a solution for users to upload their images with fixed width and height. Basically my customer wanted to display profile images on the start page, but the requirement was to display each of them in a div container with fixed width and height. Because I'm not a CSS guru and I didn't want to set negative background position to display only parts of the images or because of the problems, which CSS has with different browsers, I came up with a solution, which does that pretty good. My class has a simple configuration and according to the width or height of an image uploaded by a user, resizes it to predefined width or height and places it according to the case vertically or horizontally in the center. This description might be confusing right now, but if you read this post to the end I hope you will know, what I meant.<br />
<span id="more-9"></span></p>
<p>(You can find a downloadable archive at the end of this example)</p>
<p>Let's start with defining the class and some configuration variables.<br />
[source:php]<br />
class Upload {<br />
	private $image = array();<br />
	private $image_location;<br />
	private $image_type;<br />
	private $image_height;<br />
	private $image_width;<br />
	private $image_maxheight;<br />
	private $image_maxwidth;<br />
	private $image_box;</p>
<p>	public $error;<br />
	public $success;<br />
}<br />
[/source]<br />
We set all of important variables as private, because we will use them only in the scope of our class. Two public variables will be responsible for displaying the error and success messages. Let's go to our constructor.<br />
[source:php]<br />
function __construct() {<br />
	$this->image_location = 'images/';<br />
	$this->image_height = 60;<br />
	$this->image_width = 60;<br />
	$this->image_maxheight = 1500;<br />
	$this->image_maxwidth = 1500;<br />
	$this->image_box = 60;<br />
}<br />
[/source]<br />
In our constructor method, which will be executed, after we initialize our class, we will define some basic configuration. $this->image_location property will tell our script, where to save the images. Before you start using this class, don't forget to create this directory and give it necessary rights, because otherwise you will get an error message. $this->image_width and $this->image_height tell our script, to which width or height should the image be resized. Both properties should have the same value, which shouldn't be bigger than the value of $this->image_box. Our script will automatically detect, if the picture is horizontal or vertical and will use the right value according to the case. $this->image_box tells our script, which resolution has the div box displayed on the start page. $this->image_maxheight and $this->image_maxwidth is the maximum resolution of the picture, which cannot be exceeded. Let's move to the picture upload.<br />
[source:php]<br />
public function uploadPicture() {<br />
	foreach($_FILES as $file) {<br />
		$this->image['tmp_name'] = $file['tmp_name'];<br />
		$this->image['name'] = $this->image_location.$file['name'];<br />
		$this->image['type'] = $file['type'];<br />
	}<br />
	return ($this->createPicture()) ? true : false;<br />
}<br />
[/source]<br />
In our first public method, which will be called, after a user clicks an upload button, we set some important properties, which we will use later to handle our image upload. $this->image['tmp_name'] will hold for us the temporary location of the file. $this->image['name'] will hold the location, where the uploaded image should be copied, and $this->image['type'] will hold MIME type of the uploaded picture. After we set those properties, we call createPicture method to actually create the pictures for us. Let's take a closer look at two last methods.<br />
[source:php]<br />
private function createPicture() {<br />
	if($this->checkType()) {</p>
<p>		move_uploaded_file($this->image['tmp_name'],$this->image['name']);<br />
		list($width, $height, $type, $attr) = getimagesize($this->image['name']);</p>
<p>		if ($width > $this->image_maxwidth || $height > $this->image_maxheight) {</p>
<p>			$this->error = 'Your picture is too big';<br />
			unlink($this->image['name']);<br />
			return false;</p>
<p>		} else {</p>
<p>			if($height > $width) {<br />
				$this->image_width = round((($this->image_height * $width) / $height));<br />
				$x = round(($this->image_height - $this->image_width) / 2);<br />
				$y = 0;<br />
			} else {<br />
				$this->image_height = round((($this->image_width * $height) / $width));<br />
				$x = 0;<br />
				$y = round(($this->image_width - $this->image_height) / 2);<br />
			}</p>
<p>			if($this->image_type == 'jpg') {<br />
				$galery_image = imagecreatefromjpeg($this->image['name']);<br />
			} elseif($this->image_type == 'gif') {<br />
				$galery_image = imagecreatefromgif($this->image['name']);<br />
			} else {<br />
				$galery_image = imagecreatefrompng($this->image['name']);<br />
			} </p>
<p>			$image = imagecreatetruecolor($this->image_box, $this->image_box);<br />
			$background = imagecolorallocate($image, 255, 0, 0);<br />
			imagefill($image, 0, 0, $background);</p>
<p>			imagecopyresampled($image, $galery_image, $x, $y, 0, 0, $this->image_width, $this->image_height, $width, $height);</p>
<p>			if($this->image_type == 'jpg') {<br />
				imagejpeg($image, $this->image['name']);<br />
			} elseif($this->image_type == 'gif') {<br />
				imagegif($image, $this->image['name']);<br />
			} else {<br />
				imagepng($image, $this->image['name']);<br />
			}</p>
<p>			chmod($this->image['name'], 0644);<br />
			imagedestroy($image);</p>
<p>			$this->success = 'Image uploaded successfully';<br />
			return true;<br />
		}<br />
	} else {<br />
		return false;<br />
	}<br />
}</p>
<p>private function checkType() {<br />
	switch ($this->image['type']) {<br />
		case 'image/jpeg':<br />
		case 'image/jpg':<br />
		case 'image/pjpeg':<br />
			$this->image_type = "jpg";<br />
			return true;<br />
			break;<br />
		case 'image/gif':<br />
			$this->image_type = "gif";<br />
			return true;<br />
			break;<br />
		case 'image/png':<br />
		case 'image/x-png':<br />
			$this->image_type = "png";<br />
			return true;<br />
			break;<br />
		default:<br />
			$this->error = "Wrong file type";<br />
			return false;<br />
			break;<br />
	}<br />
}<br />
[/source]<br />
Because this two method are pretty long I will split them into smaller pieces and explain step by step. We set both of the methods as private, because we will use them only in the scope of our class. It the first method, createPicture, we first check, if the MIME type of the file uploaded by a user is supported by our script. If it is we set $this->image_type and return true, if not we display error message and return false.<br />
[source:php]<br />
move_uploaded_file($this->image['tmp_name'],$this->image['name']);<br />
list($width, $height, $type, $attr) = getimagesize($this->image['name']);</p>
<p>if ($width > $this->image_maxwidth || $height > $this->image_maxheight) {</p>
<p>	$this->error = 'Your picture is too big';<br />
	unlink($this->image['name']);<br />
	return false;</p>
<p>}<br />
[/source]<br />
In this part we actually move the uploaded file from the temporary location to the location we defined earlier in our script. After that we list the width and height from our new uploaded image and check, if they are not bigger, then the values we defined in our constructor. If they are, we display an error message, if not we proceed.<br />
[source:php]<br />
if($height > $width) {<br />
	$this->image_width = round((($this->image_height * $width) / $height));<br />
	$x = round(($this->image_height - $this->image_width) / 2);<br />
	$y = 0;<br />
} else {<br />
	$this->image_height = round((($this->image_width * $height) / $width));<br />
	$x = 0;<br />
	$y = round(($this->image_width - $this->image_height) / 2);<br />
}<br />
[/source]<br />
If the resolution of the image is correct, we check, if the image is vertical or horizontal. According to the case, we calculate new width or new height and the offset, which we will later use to place the image in the middle of our box.<br />
[source:php]<br />
if($this->image_type == 'jpg') {<br />
	$galery_image = imagecreatefromjpeg($this->image['name']);<br />
} elseif($this->image_type == 'gif') {<br />
	$galery_image = imagecreatefromgif($this->image['name']);<br />
} else {<br />
	$galery_image = imagecreatefrompng($this->image['name']);<br />
} </p>
<p>$image = imagecreatetruecolor($this->image_box, $this->image_box);<br />
$background = imagecolorallocate($image, 255, 0, 0);<br />
imagefill($image, 0, 0, $background);</p>
<p>imagecopyresampled($image, $galery_image, $x, $y, 0, 0, $this->image_width, $this->image_height, $width, $height);<br />
[/source]<br />
In this step we return an image identifier according to the MIME type of the picture, which has been uploaded and create an empty image, in which we will place the uploaded picture. You can change the $background and use your own color for the background using the RGB values. Imagecopyresampled uses our identifier and the "picture box" we creted with predefined values and creates a ready image.<br />
[source:php]<br />
if($this->image_type == 'jpg') {<br />
	imagejpeg($image, $this->image['name']);<br />
} elseif($this->image_type == 'gif') {<br />
	imagegif($image, $this->image['name']);<br />
} else {<br />
	imagepng($image, $this->image['name']);<br />
}</p>
<p>chmod($this->image['name'], 0644);<br />
imagedestroy($image);</p>
<p>$this->success = 'Image uploaded successfully';<br />
return true;<br />
[/source]<br />
In the last part, according to MIME type, we output our new created picture to the file, change the permissions and destroy the original image. </p>
<p>Let's take a closer look, how you should use this class.<br />
[source:php]<br />
<?php<br />
if(isset($_POST['submit']) &#038;& $_POST['submit'] == 'upload') {<br />
	include('upload.class.php');<br />
	$upload = new Upload;<br />
	if($upload->uploadPicture()) {<br />
		echo (isset($upload->success)) ? $upload->success : '';<br />
		echo '<br /><a href="upload.php">go back</a>';<br />
	} else {<br />
		echo (isset($upload->error)) ? $upload->error : '';<br />
		echo '<br /><a href="upload.php">go back</a>';<br />
	}<br />
} else {<br />
?></p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="picture" />
<input type="submit" name="submit" value="upload" />
</form>
<p><?php<br />
}<br />
?><br />
[/source]<br />
I think, this code doesn't need a detailed explanation. One hint only: don't forget to set the enctype of the form to multipart/form-data, because otherwise the $_FILES variable wont be initialized. If user clicks on upload button, we initialize our class and use the uploadPicture method to create our new picture. If this method returns true we display success message, if not the error message.</p>
<p>That's all. I hope it was clear enough for you, what I was trying to accomplish in this example. You can change it in probably hundreds different ways and adjust it to your own needs. Feel free to do that. </p>
<p>You can download all of the files <a href="http://www.designing4u.de/examples/image-upload-class/image-upload-class.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/class-for-picture-upload-with-fix-width-and-height/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Login and authentication class</title>
		<link>http://www.designing4u.de/2008/05/authentication-class/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=authentication-class</link>
		<comments>http://www.designing4u.de/2008/05/authentication-class/#comments</comments>
		<pubDate>Sat, 03 May 2008 09:50:01 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[Authentication Class]]></category>
		<category><![CDATA[object oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=5</guid>
		<description><![CDATA[I lately got maybe not the best but really helpful book about object oriented programming in php5 and I decided to write my own login and authentication class. In this post I will give you a short description, how I manage the authentication of users for restricted pages. This class will use information stored in [...]]]></description>
			<content:encoded><![CDATA[<p>I lately got maybe not the best but really helpful book about object oriented programming in php5 and I decided to write my own login and authentication class. In this post I will give you a short description, how I manage the authentication of users for restricted pages. This class will use information stored in MySQL table but I will comment it out for demo purposes. I assume that you know how to use MySQL database and you will be able to create the tables by yourself. If not please let me know and I will post also the table structure.<br />
<span id="more-5"></span></p>
<p><a title="demo" href="http://www.designing4u.de/examples/login-authentication-class/login.php" target="_blank">Here</a> you can find a working demo of the class I will introduce in this example</p>
<p>Let's start with defining the class and the variables we will use in the scope of this class. We will give it a Login name because it has to handle a user, who wants to login to view certain content, which is available only for registered users.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Login <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_pass</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_id</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$user_access</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$login_error</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$userpass</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_pass</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$userpass</span> <span style="color: #339933;">:</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userpass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We declared the properties of this class as private, because we will use them only in scope of it and we will access them only internally from this class. The only public property will be responsible for error handling. We use __construct function to assign values to the properties we just declared, when the class will be initialized. We assume that password stored in database was encrypted using md5() function. As the first step we will declare a method, which will handle user login.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loginUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location:index.php?page=overview&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error_login</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'wrong username or password'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We set the loginUser function to public so we can access it after we initialize our class. We check, if the user exists in our database. If the information in database matches the user input, we use setUser function to store the information about the user, which we can use later to authenticate or to get user content availably only for that certain user.</p>
<p>As second step we will declare a private method checkUser, which will check, if a the information provided by user exists in database. This will be a simple MySQL query and the method will return true in case user exists and false in case of any mismatch.</p>
<p>(In this place you might comment out MySQL check and just add hard coded check for certain string. I add this part as a commentary.)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*
    return ($this-&gt;user_name == 'demo' &amp;&amp; $this-&gt;user_pass == 'demo') ? true : false;
    */</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(*) AS exist FROM usertable WHERE username='<span style="color: #009933; font-weight: bold;">%s</span>' AND pass='<span style="color: #009933; font-weight: bold;">%s</span>' AND confirmed='1';&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>user_name<span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>user_pass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'exist'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the next step we will define a method, which will set our user as a session array, where we will store all the important information about our user.</p>
<p>(Again, if you don't want to use database, you can define default user id.)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*
    $this-&gt;user_ip = 1;
    */</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM miss_user WHERE username='<span style="color: #009933; font-weight: bold;">%s</span>' AND pass='<span style="color: #009933; font-weight: bold;">%s</span>';&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_name</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_name'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_pass'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_pass</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_ip'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userip'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can notice we saved user id, user name, user password and user remote address in an array, which we will later use to authenticate user. We declared this method as private again, because we will access it only in the scope of our class. Additionally we save a cookie with the IP address of the user to perform a check against session stealing. Even if someone would try to steal the session by just randomly typing it into the URL address, the authentication will be failed because it wont match the IP in the cookie.</p>
<p>In this step we will actually perform the authentication of a user.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> authenticateUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userip'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_ip'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First we check, if the session array is an array at all and if it's not empty. After that we perform checkUser again to compare the session array with the database information. At the end we do the last check, if the IP address saved in the cookie matches the address saved in session array. In my case the method authenticateUser returns true on success and false in case of failure but you can do whatever you want after a user passes authentication. We save id of a user because it might be useful later in getting the information relevant only for this certain user or from two MySQL tables, which are related to each other by an user ip.</p>
<p>The only thing, which we are missing right now is the log out method.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> logOut<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'userip'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location:login.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think this is self explanatory. We unset the sessionn, destroy the cookie and redirect the user to the login page. That would be all. Now i will present a simple usage of this class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.login.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loginUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'logout'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We start a session and include our class before we send any headers. We use error_reporting only for develpment reasons and you can erase this line or decrees the level of error reporting if you want. We perform a check, if user submit the login form. If yes we initialize the Login class with the information provided by user. If the input validates we log in user and redirect him to welcome page (as stated in the class). If the user didn't submit the login information we perform another check, if the session array is set and if it's not empty we initialize our class with the information saved in our session array. If the user clicks on the log out button, we use logOut method to destroy all the information about the user. After that we can send some headers.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticateUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'logged'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;a href=&quot;login.php?page=logout&quot;&gt;Logout&lt;/a&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error_login</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; method=&quot;post&quot;&gt;
username:&lt;input name=&quot;username&quot; type=&quot;text&quot; /&gt;
password:&lt;input name=&quot;pass&quot; type=&quot;text&quot; /&gt; &lt;input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;login&quot; /&gt;
&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In this part we perform actual authentication of the user and display information for authenticated users and if there is any error we display the error message</p>
<p>Wow I did it. It's my first tutorial kind of example thing:) If you want to download the files with source code for this example you can do it <a href="http://www.designing4u.de/examples/login-authentication-class/login-authentication-class.zip">here</a>. I'm waiting for your feedback right now. If you went that far you can write at least couple words:) Going back to work now...</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/authentication-class/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

