Download links, without the source file

Now this “hiding the source file” may seem irreverent but it can definitely help you out when it comes to tracking your downloads, seeing who’s downloading them, and file management. These are essential for any web developer because you want all the content on your site to be tracked and analyzed (not just the site itself). SEO professionals can provide their clients ways to track their site efficiently but can they track the downloads too? CMS systems provide ways to do that but what about custom made sites or blogs (like wordpress)? How can site owners know if the downloads they post on their site are even an importance to the audience they try to attract?

The first goal is to start making the downloads.php file. Its pretty simple.
This is directly from http://us3.php.net/header

1
2
3
4
5
6
7
8
9
10
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
 
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
 
// The PDF source is in original.pdf
readfile('original.pdf');
?>

Pretty easy right? Its setting the page type to be a direct download to “downloaded.pdf” and readfile(); is showing the original path to the file. Already the source of your file is hidden. I prefer the sources to be hidden just for peace of mind. Lets see how we can track it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
// made up class
include("class.trackdownloads.php");
 
$download = new trackdownloads();
// some features you can add to your site
$download->get_host();
$download->add_one_to_number_of_downloads();
$download->check_for_refer_link();
$download->check_if_user_downloaded_file_more_than_once();
 
// We'll be outputting a PDF
header('Content-type: application/pdf');
 
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
 
// The PDF source is in original.pdf
readfile('original.pdf');
?>

As you see, there are numerous possibilities that you can do when you manually code your downloads rather than just uploading them to the FTP and giving out the direct link to whomever you need to give it out to. Anyone who wants me to create a working class please comment back with ideas in what the program should do.

PHP: “Friendly” Procedural Checking in OOP

Whats the use of procedural checking when there is OOP? Well lets say I wanted to check if a car’s door is closed and the light in the car only goes off when the door closes. In addition to that, if the light is off, the headlights also switch off. How can I have such procedures in OOP without returning critical errors? The answer is procedural checking!

Now after you read that first paragraph you probably realized you could do a series of if() {...}else{...} statements but if you like to keep things organized, the tabs will extend out too far making it annoying to check/improve coding. In addition to that it just seems primitive to use such a style (that’s if there’s not a method better than this one).

if() {...}else{...} Example

1
2
3
4
5
6
7
8
9
<?php
if($a == $b) {
	if($k == $c) {
		if($t == $x) {
			return 'Login Successful';
		}
	}
}
?>

The procedural checking method is only useful when your checking over 2 or 3 functions or inputs. Anything less than that amount and this style of coding might be a waste of time since there isn’t that much checking. This style is only good for large amounts of checks. Note: Between if statements and this, results are the same. Just different styles (though procedural method might be more flexible).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
class login {
	// last checking number
	protected $_lastint = 0;
	// username obj
	protected $_user = NULL;
	// password obj
	protected $_pass = NULL;
	// error variable
	protected $_error = NULL;
 
	public function __construct($user='oh', $pass=md5('k')) {
		$this->_user = $user;
		$this->_pass = $pass;
 
		// run linear function
		// in this case there are 3 checks
		for($i=0;++$i;$i<=3) {
			// stops further processing
			if($this->checklist($i) === false) {
				break;
			}
		}
	}

Everything is pretty ordinary in a normal class; we have initialized variables and a constructor. But the constructor also has a for loop that continuously calls the same function with a int parameter. That function returns true or false (Boolean).

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	// begin Procedural Checking
	public function checklist() {
		// its a critical error if the order is not in line
		if($int != ($this->_lastint+1)) {
			throw new Exception("Sequence is out of order");
		} else {
			$this->_lastint = $int;
		}
		// count case number
		$i = 0;
		switch($int) {
			// some functions, all of them are booleans
			case ++$i: return $this->check_empty(); break;
			case ++$i: return $this->userpass_mysql(); break;
			case ++$i: return $this->check_captcha(); break;
			case ++$i: return $this->no_multiple_logins(); break;
			// its int doesnt exist, critical error out
			default:
			   throw new Exception("Checklist default error");
			break;
		}
	}

If you implied that all the functions in checklist() are Boolean, you are correct. Each function is basically a single if() statement with the access of doing much more that what goes outside an if statement, without creating additional functions (unless needed for a complex work of art). But there is still a few things missing. The main question now should be: “How do I know which function stopped the loop?”

45
46
47
48
49
50
51
52
53
	protected function check_empty() {
		if($this->_user == '' || $this->_pass == '') {
			// outputs string
			$this->_error = 'error message goes here';
			return false;
		} else {
			return true;
		}
	}

We also need an error function so it can be checked if there are is an error; if there is return it.

54
55
56
57
58
59
60
61
62
63
	// if errors
	public function error_out() {
		if($this->_error != '') {
			// outputs string
			return $this->_error;
		} else {
			return false;
		}
	}
}

That’s about it. In conclusion, there’s an area where checklist() is called multiple times (linear format) which can stop if returned false, functions within checklist() which also returns true or false (using switch() statement), independent functions return true/false with the option to do individual tasks, and an error console. How to use that error console…

1
2
3
4
5
6
// ....some coding here
if($login->error_out() !== false) {
	// redirect or do whatever
} else {
	echo $login->error_out()
}

Personally, I think this would be perfect for long forms like a registration area for a site.
Hope you guys like my concept, please comment back on questions or concerns you have.

Random Images Script

This script will randomize images. Useful for ads or simply a change of scenery. Link capabilities are in this script. Please comment back, I would like to hear everyone’s response.

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
<?php
// Created by Matt Lo, very simple :)
class random_images {
	// store images
	protected $_images = array();
	// no constructor needed
 
	// properties per image, protected, all parameters are strings
	// $location = path/to/image.jpg, REQUIRED
	public function define_image($location, $title=NULL, $link=NULL, $target='_blank') {
		// html code for image with variables
		// removing border, you can edit it out or leave it in
		$image = '<img src="'.$location.'" alt="'.$title.'" style="border: none;" />';
 
		// if link param has value to it...
		if($link != '') {
			$image = '<a href="'.$link.'" target="'.$target.'">'.$image.'</a>';
		}
 
		// array, key index starts at 0
		$this->_images[] = $image;
	}
 
	public function display() {
		// randomize the array by finding max
		$rand_int = mt_rand(0, count($this->_images)-1);
		return $this->_images[$rand_int];
	}
}
 
// Usage
// NOTE: You can put the class into a separate page, then include it
// start
$image = new random_images();
// example 1
$image->define_image("http://cdn.joomla.org/images/logo.png", 'testing j');
// lets add a mix of linked images
$image->define_image("http://drupal.org/sites/all/themes/bluebeach/logos/drupal.org.png", 'drupal', 'http://drupal.org');
// output
echo $image->display();
?>

Example: http://mattloinfo.com/scripts/random_image.php

class.users.php

In my opinion, this will have to be the best class yet I’ve made. I’m not sure if its the best out there or even “good” to be out in the world wide web but whatever, its my website.

http://mattloinfo.com/pub-code.php/class.users.php

The class works wonderfully for my programs, please send comments to review!