Posted on April 24, 2009. Filed under
Coding,
Concepts,
Websites.
No Comments »
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.
Posted on April 19, 2009. Filed under
Coding,
Concepts.
4 Comments »
In PHP, sessions are pretty easy to work with since it just involves doing a few lines of code; initialize the session, define a session variable, then calling it back when you need it. PHP developers use sessions for passing data from page to page but shouldn’t it be a little more accountable then just passing data? A few examples would be data tracking, origin of variable, and what browser the user is in (for display purposes). This is valuable data for both the php coder and the web designer
Page1.php
// enable
session_start();
// define session variable
$_SESSION['test'] = 'good';
Page2.php
// start session again
session_start();
// display
echo $_SESSION['test']; // displays the word: good
That’s a very primitive way using sessions especially since were in php 5 (soon moving to php 6) and we can utilize classes to improve our usage of the $_SESSION variable.
http://www.phpclasses.org/browse/package/3307.html is the source of a session management class and expands the capabilities of $_SESSION. When using this class, its no different from actually declaring a variable (like above) but more or so how sessions as a whole is used. Look for class.dbsession.php.
One of the most important features of this class is the fact it can track the user’s info while their session is active. This is key for manual logouts from a different user (admin), see who’s been logging on a person’s account and how long they were active, or simply show a who’s online function (display all rows). This is all stored in the database via MySQL. If a session is terminated the class has a garbage collector and deletes any records that have not been updated within a specified amount of time. When I use this class, I set $session->gc(60*15);, so all records that are inactive for 15 minutes are deleted.
Try out the class, it’ll add a lot of neat features to your site, admin and public side. The class has comments all over so its easy directions in how-to-use.
Page1.php
//session class and start sessions
require_once("classes/class.dbSession.php");
$session = new dbSession();
//run session garbage collector (15 minutes is how much it runs)
$session->gc(60*15);
// define session variable
$_SESSION['test'] = 'good';
Page2.php
require_once("classes/class.dbSession.php");
$session = new dbSession();
$session->gc(60*15);
// display
echo $_SESSION['test']; // displays the word: good
Posted on April 13, 2009. Filed under
Coding,
Concepts,
Websites.
No Comments »
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.
Posted on April 10, 2009. Filed under
Coding,
Scripts,
Websites.
No Comments »
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