kreoton web development

PHP free eBooks

September 6th, 2007

I noticed that many people are looking for free PHP eBooks. So i decided to help them. No I will not write one but i will write where to find some good free PHP eBooks. Most common free PHP eBook is PHP manual. PHP manual you can download directly from PHP.net site in documentation section.

Another good PHP eBook is written by Poul Hudson you can read it online on his site http://www.hudzilla.org. I think this good is quite good for novices to PHP.

However you would never be a good PHP programmer after you reading a book. To became a real and good PHP programmer you must take a lot of practice. Any kind of PHP and MySQL tutorials is good way to learn.

Good luck learning PHP. If you have found a good free PHP eBook post a link in comments. Thanks.

Zend Framework tutorials

August 23rd, 2007

I started to use Zend Framework, but there is a problem: How start to code applications with Zend Framework? i searched internet for ZF tutotrials many of them are for old not for curent stable Zend Framework version (1.0.1 current). So i list here my search results:

Nice ZF application demo Feed reader coded by Andries Seutens. Thanks Andries for this great Zend Framework usage demo.

Also good ZF tutorial in PDF format you can download it from Akra’s DevNotes website. In this same website you can find also other tutorial about Zend_Auth classes.

If you found more Zend framework tutorials please leave a comment with link.

Zend framework

Tool for deleted domain hunters.

July 28th, 2007

Site DeletedHunt.com started with unique tool, that alows site visitors search for pending delete or deleted domains in real time. Search engine is powered by Ajax technologie, that alows to find fast domains. If someone whants to get better results may go to Advanced search and setup their parameters.

PHP form validation class

June 12th, 2007

Are you bored to write long script to validate you user input? If yes I coded a user input validation PHP class. It’s very flexible and easy to use. This class can validate user e-mail, homepage url, text and other input formats.

Class features:

  • validate and return entered one word (perfect for username and password validation, you can write custom regex for allowed chars, by default it allows only letters);
  • validate and return entered text (may return formated text, strips tags or changes special HTML characters like <, >, & and others to character codes);
  • validate and return integer number input;
  • validate and return float number input;
  • validate and return email input;
  • validate and return www address input;
  • validate and return password and password confirm match input;
  • every form field may have required option;
  • for every form field you may write custom error message;

Example of using validation class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
<?php
require('validate.class.php');
$VD = new validate();
 
if ($_POST['form_submit'])
{
 
	$VD->validate_data('username', $_POST['username'], array(
			'isOneWord'=>array(
				'min_chars'=>5,
				'max_chars'=>20,
				'regex'=>"/^[a-zA-Z0-9_\.]{1,}$/",
				'required'=>true)
				),
			'Username not valid'
		);
 
	$VD->validate_data('password', $_POST['password'], array(
			'isOneWord'=>array(
				'min_chars'=>6,
				'max_chars'=>15,
				'regex'=>"/^[a-zA-Z0-9_\.]{1,}$/",
				'required'=>true)
				),
			'Password not valid'
		);
	$VD->validate_data('email', $_POST['email'], array(
			'isEmail'=>array(
				'required'=>true)
				),
			'Email not valid'
		);
	$VD->validate_data('details', $_POST['details'], array(
			'isText'=>array(
				'strip'=>true,
				'required'=>true)
				),
			'Details not valid'
		);
	$VD->validate_data('age', $_POST['age'], array(
			'isInteger'=>array(
				'required'=>true
			)),
			'You must fill in your age');
 
	if (count($VD->errors) > 0)
	{
		echo 'Please fix following errors:';
		foreach ($VD->errors as $error)
		{
			echo $error.'<br />';
		}
	}
	else
	{
		$link = mysql_connect($host, $user, $pass);
		mysql_select_db($name, $link);
 
		mysql_query("INSERT INTO users VALUES (
			'".$VD->return['username']."', 
			'".md5($VD->return['password'])."', 
			'".$VD->return['email']."', 
			'".$VD->return['details']."', 
			".$VD->return['age']."
		)");
	}
}
 
?>

Download form validation class

More secure PHP image upload class tutorial

June 8th, 2007

In the internet are many php image upload tutorials, but most of them are not so secure. In this tutorial i try to teach you how to write not very complex but secure image upload class. First of all we should define class ant it variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
class image_upload
{
	var $tmp_image;
	var $max_file_size = 100000;
	var $max_width = 800;
	var $max_height = 600;
	var $allow_types = array(
'image/jpeg', 
'image/png', 
'image/gif'
);
var $errors;
}
?>

For variables $max_file_size, $max_width, $max_height and $allow_types I assigned default values. There are two more variables its $errors for error printing and $tmp_image for $_FILES[’xxxx’] global variable.

Now then we have defined class and variables we can make some standard functions for image upload class. First we need to know if given file is really image so we use function exif_imagetype() witch reads firs file bytes and returns true if file is image else it returns false. (Note: exif_imagetype() function will work only if module exif is installed in server.)

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
function is_image ()
{
	if (exif_imagetype($this->tmp_image['tmp_name']))
	{
		$this->errors[] = 'Uploaded file is not image';
		return true;
	}
	else
	{
		return false;
	}
}

To check uploaded image type (mime type) i use next function. It searches for uploaded image type in $allow_types types array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function is_type ()
{
	if (in_array($this->tmp_image['type'], 
$this->allow_types))
	{
		return true;
	}
	else
	{
		$this->errors[] = 'Image type is 
not acceptable';
		return false;
	}
}

Next I check if image is suitable size:

1
2
3
4
5
6
7
8
9
10
11
12
function is_size ()
{
	if ($this->tmp_image['type'] <= $this->max_file_size)
	{
		return true;
	}
	else
	{
		$this->errors[] = 'Image file size is too big';
		return false;
	}
}

To check image dimensions I call getimagesize() function it returns array with image parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function is_dimensions ()
{
	$size = getimagesize($this->tmp_image['tmp_name']);
	$width = $size[0];
	$height = $size[1];
	if ($width <= $this->max_width && 
$height <= $this->max_height)
	{
		return true;
	}
	else
	{
		$this->errors[] = 'Image is too height 
or too width';
		return false;
	}
}

Finally I wrote image upload function it has two parameters $dest – upload destination and $safe for safe image upload witch by default is false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function upload_image ($dest, $safe=false)
{
	if ($safe)
	{
		$status = true;
 
		$status = $this->is_image();
		$status = $this->is_type();
		$status = $this->is_size();
		$status = $this->is_dimensions();
 
		return ($status)?move_uploaded_file(
$this->tmp_image['tmp_name'], $dest)
:false;
	}
	else
	{
		return move_uploaded_file(
$this->tmp_image['tmp_name'], $dest);
	}
}

Now class is ready for use. For class test I wrote a simle script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
include('upload.class.php');
 
$IM = new image_upload();
 
if ($_FILES['photo'])
{
	$IM->tmp_image = $_FILES['photo'];
 
	if ($IM->upload_image($_SERVER['DOCUMENT_ROOT'].
'/tnimg/'.$IM->tmp_image['name'], true))
	{
		echo 'Image uploaded';
	}
	else
	{
		foreach ($IM->errors as $error)
		{
			echo $error.'<br />';
		}
	}
}
 
?>
<form method="post" enctype="multipart/form-data" action="">
	<label for="photo">Photo file</label>
	<input type="file" name="photo" />
	<input type="submit" name="submit" value="Upload" />
</form>