Archive

Archive for the ‘PHP’ Category

Check a Date Exists Within a Given Period

July 11th, 2010 admin No comments

So I recently was asked to solve a problem for someone trying to write a script to check if a birthday (presented in DATETIME M d, Y form) was present within a given period of days from the current date. Naturally, I feel inclined to give some assistance because I love working on these things.

So, I am posting this because I found it interesting. I feel like this is a very useful code snippet for anyone looking how to do this. The code is fairly well-documented, so I am not going to go through and explain all the facets of the code. However, I will make special mention of the real challenge to this. You have to make sure to account for the coming year if the date is after December 21 in any given year. This is because the system won’t automatically rollover to the next year and as you approach January 1 of the coming year, no dates will match until January 1 is met. So you can take a look in the code to see how I personally accomplished this task although there are probably hundreds of other ways to tackle the issue.

<?php
/**
* Birthday Script created by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER, JR. ALL RIGHTS RESERVED.
*/
class Node
{
// For sake of keeping things similar to a usable environment
var $field_work_start = array();
}

/**
* checkBirthday function
*
* The function will check a birth date
* and see if it is in the range of specified
* days.
*
* @param $birthday
* Birth date parameter. Assumed to be in M d, Y form
* @param $days
* The range of days to check where the birthday is
* @return int
* 0 if the birthday is _NOT_ within the proper range
* 1 if the birthday _IS_ within the proper range
*/
function checkBirthday($birthday,$days)
{
// Parse out the year of the birthday
$pos = strpos($birthday,",")+2; // Find the comma which separates the year
$today = strtotime(date("M, d")); // Check if day is < Dec. 22. If not, we need to account for next year!

// Check if the birthday is within the range and has not passed!
if($today < strtotime("Dec 22")){
$birthday = substr_replace($birthday,date("Y"),$pos,4); // Replace the year with current year
if(strtotime($birthday) <= strtotime("+".$days." days") && strtotime($birthday) >= time()){
return 1;
}
} else { // Less than 10 days left in our year.. check for January birthdays!
if(!strstr($birthday,"December")){
$birthday = substr_replace($birthday,date("Y")+1,$pos,4); // Replace the year with next year
$year = date("Y")+1;
} else { // Still December?
$birthday = substr_replace($birthday,date("Y"),$pos,4); // Replace the year with current year
$year = date("Y");
}
$day = (date("d")+10)-31; // 10 days from now is...

if((strtotime($birthday) <= strtotime("January ".$day.", ".$year)) && strtotime($birthday) >= strtotime("December 25, 2010")){
return 1;
}
}
return 0;
}

$node[] = new Node;

$node[0]->field_work_start[0] = "January 1, 1970"; // First birthday is _NOT_ within range
$node[1]->field_work_start[0] = "July 20, 1970"; // Second birthday _IS_ within range

for($i=0;$i<count($node);$i++){
if(!checkBirthday($node[$i]->field_work_start[0],10)){
print $node[$i]->field_work_start[0]." is not within the 10 day range.<br /><br />";
} else {
print $node[$i]->field_work_start[0]." is within the 10 day range.<br /><br />";
}
}

unset($node);

?>

Enjoy!

Regards,
Dennis M.

Check_Date_Period.tar

Categories: PHP Tags:

Overloading Classes

May 29th, 2010 admin No comments

In OOP, it is a good practice to sometimes “overload” classes if you need to bring in information from the outside. For instance, if you want to include configuration variables in another class, you can “overload” that class with those variables. In this tutorial, I will explain how to do that in both PHP and C++.

Please note, before continuing, that I assume you are an intermediate to advanced C++/PHP programmer. Therefore, I will not go into great detail about how variables are set, etc. because you should already know how that works. We will jump right into the code now for an example as it’s usually the best way to explain these tasks.

PHP

<?php
/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/

// Create our simple class
class OverloadMe
{
/**
* We would like a global var for the whole class
*/
var $var1;

/**
* Constructor
*/
function __construct($name){
$this->var1 = $name;
}

/**
* Name function
*/
function name(){
return $this->var1;
}
}

// Define the name variable
$yourname = "Dennis";

// We overload the function when we initialize it
$overload = new OverloadMe($yourname);

// Now let's get the output
print "Your name is ".$overload->name();
?>

Now, the procedure is very similar in C++, just classes work differently as you already know. I’ll give the C++ code now and explain it all at the end!

C++
src/main.cpp

/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/

#include <string>
#include "overload.h"

int main(int argc,char* argv[])
{
std::string name = "Dennis";
OverloadMe overload(name.c_str());
overload.name();
return 0; // exit
}

src/overload.h

/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/

#ifndef Overload_H
#define Overload_H

// Class definition

class OverloadMe
{
public:
// Constructor
OverloadMe(const char* namevar);

// Name function
void name();
private:
// A global var for the class
const char* var1;
};
#endif

src/overload.cpp

/**
* Class overloading tutorial
* by Dennis J. McWherter, Jr.
*
* (C) 2010 DENNIS J. MCWHERTER JR. All Rights Reserved.
*
*/

// Make the class work
#include <iostream>
#include <stdio.h>
#include "overload.h"

using namespace std;

OverloadMe::OverloadMe(const char* namevar)
: var1(namevar)
{
}

void OverloadMe::name()
{
cout<< "Your name is " << var1 << endl << endl << "Please hit enter to exit" << endl;
getchar();
return;
}

Now, for Windows users using MSVC++, this code will compile with a simple copy/paste. For *nix and BSD users, I’ve included a Makefile within the ZIP package for you to use when compiling. Basically, the compiler must compile each item as an object first, then it must compile the objects into the program file rather than compiling the .exe’s directly.

Well, I hope this is of some use to someone. The examples are fairly simple, but should explain the concept easily. As we can see in the C++ example, we initialize the class to a variable, and simply overload that variable at the time of initialization.

Regards,
Dennis M.

Overload_Tutorial

Categories: C/C++, PHP Tags: , , , , ,

Windows Programming: Creating/Using DLL’s

September 8th, 2009 admin 2 comments

In the world of programming, there are MANY different types. Unfortunately, programming for some systems is different than others, but it’s things like that which keep the world turning. Today, we will discuss what exactly is a Dynamic Link Library (DLL) and how to use them to keep our programming easier and more organized. I will also include an Italian translation of this tutorial in the download source below! (Un traduzione del questo tutorial è in il download link per richiesta)

We’ll begin with what a DLL is and what it’s purpose is. A DLL is a wonderful tool to keep your programs organized by creating a DLL per set of functions. Also, it helps developers keep their closed source functions closed source, but also allows (with proper documentation/instructions on doing so) other users to use their work. Below we will give a few simple examples on how it all works!

To create the DLL we will be using Microsoft’s Visual C++. This is a free IDE compiler. I use it for any sort of Windows programming that I may do and will include the solution file in the download. Of course, I prefer GNU’s GCC which is also free, but the Windows ports of the compiler are not as good as the standard linux version.

The first note that I want to make is that if you’re making this project on your own (and not using my solution file), be sure to go to Project->Your Project’s Properties->Configuration Properties->General->Configuration Type and change it to “Dynamic Library (.dll)” otherwise it will not compile correctly.

Now in this tutorial, I am assuming you already know how to write programs, so this first file is test_dll.cpp. It contains the actual definitions, etc. of our functions:

/**
* Test Dynamic Library File
*
* (C) 2009 Dennis J. McWherter, Jr. All Rights Reserved.
*
*/

#define WIN32_LEAN_AND_MEAN // No need for the extra stuff.
// Non includere i file cui non hanno un scopo
#include <windows.h>
#include <iostream>
#include "test_dll.h" // Our header file
// Il nostro header file

using namespace std;

// Initiate the DLL (For programs)
// Iniziare il DLL (per i programmi)
BOOL APIENTRY DllMain(HINSTANCE dllHinst, DWORD reason, LPVOID lpvReserved){
// Use a switch to tell the program how to cycle the processes
// Usare un switch per fare il programma funziona
switch(reason){
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return true;
}

// The functions of our class. These are defined in "test_dll.h"
// Le funzioni del nostro class. Gli questi sono definire in "test_dll.h"

// Constructor
test_dll::test_dll(){}

// Basically a pointless function just to see how we can make them work together XD
// Una funziona senza la usa. è più di un demonstrazione.
char test_dll::func1(char lang){
return lang;
}

int test_dll::func2(float x,float y,char op){
// Simple math function!
// Semplice funziona della matematica
switch(op){
case 'a':
cout<< x << "+" << y << "=" << x+y;
return 0;
break;
case 's':
cout<< x << "-" << y << "=" << x-y;
return 0;
break;
case 'd':
cout<< x << "/" << y << "=" << x/y;
return 0;
break;
default:
cout<< x << "*" << y << "=" << x*y;
return 0;
break;
}
return 0;
}

Again, a fairly straightforward example. What you do have to notice in this that is different from most programs is that fact that we’re not really calling a “Main” function to be run. We call BOOL WINAPI DllMain instead. This next file is the header file. A rather important file overall as it let’s us know what to export.


/**
* Test Dynamic Library File
*
* (C) 2009 Dennis J. McWherter, Jr. All Rights Reserved.
*
*/

#ifndef HEADER
#define HEADER
// To keep the code clean we'll define this with a macro but this tells the processor
// to export whichever function/class it preceeds.
// Ci definiamo il questo con un macro ma ci vedremmo come lo funziona in più ritardo
#define EXPORT_DLL __declspec(dllexport)

// Now our class - If you export a class, all its functions come with! :) else do each function
// Adesso il nostro class! Se exporti un class poi i tutti funzione di lo sono exportare.
// se individuale ti exporti poi si deve __declspec(dllexport) per ogni
class EXPORT_DLL test_dll{ // = class __declspec(dllexport) test_dll{
public:
test_dll(); // Constructor/Costruttore
char func1(char lang); // First function definition/La definizione della prima funziona
int func2(float x,float y,char op);
};

#endif

The comments explain it, but it’s really worth going over again. EXPORT_DLL is defined just for the purpose that we could potentially have more than a single class to export. This goes both ways for the dllexport and dllimport options, so really, EXPORT_DLL just translates to __declspec(dllexport) which would also work if you replaced EXPORT_DLL. Now, compile that code and VC++ should provide you with a .dll file and a .lib file with whatever name you gave the project (in our case: “DLL_Tutorial.lib/.dll”).

Now, you cannot run DLL files by themselves; you need a client program (.exe) which is what we’re getting to now. For the sake of brevity, we will just make this a simple console program which runs the proper functions. Now, create a new project and make sure you copy the “DLL_Tutorial.lib” file into the source directory. You need the .lib file to compile the client program, but you only need the .dll to run the program.

So here is the client main file. A console program of course to keep things simple :) We’ll call this test_client.cpp

/**
* Test Dynamic Library File
*
* (C) 2009 Dennis J. McWherter, Jr. All Rights Reserved.
*
*/

#include <iostream>
#include <string>
#include "test_client.h"

using namespace std;

int main(){
// Create object to class
// Creare l'oggetto a class
test_dll test;

// Init var
string num1,num2,addition,subtraction,division,multiplication,ans,operation;
float x,y;
char choice[5], op[5]; // Allow more chars just in case - otherwise we'll break the script :(
// Permettere dei più caratteri così non ci romperiamo il script! :(
cout<< "Please select a language\r\n\r\nEnglish - en\r\nItaliano - it\r\n \r\nSelect: ";
cin>>choice;
switch(test.func1(choice[0])){
case 'i':
num1 = "Numero 1: ";
num2 = "Numero 2: ";
addition = "Per piacere selezi i tuoi numeri per aggiungiere";
subtraction = "Per piacere selezi i tuoi numeri per sottrarre";
division = "Per piacere selezi i tuoi numeri per dividere";
multiplication = "Per piacere selezi i tuoi numeri per moltiplicare";
ans = "La tua risposta: ";
operation = "\r\nPer piacere selezi il tuo operazione:\r\na - Aggiungiere\r\ns - Sottrarre\r\nd - Divisione\r\nm - Moltiplicazione\r\n\r\nSelection: ";
break;
default:
num1 = "Number 1: ";
num2 = "Number 2: ";
addition = "Please select your numbers to add";
subtraction = "Please select your numbers to subtract";
division = "Please select your numbers to divide";
multiplication = "Please select your numbers to multiply";
ans = "Your answer: ";
operation = "\r\nPlease select your option:\r\na - Addition\r\ns - subtraction\r\nd - Division\r\nm - Multiplication\r\n\r\nSelection: ";
break;
}

cout<< operation;
cin>>op;
cout<< endl << endl << num1;
cin>>x;
cout<< endl << num2;
cin>>y;

test.func2(x,y,op[0]);

cout<< endl << endl << "Type in anything to exit...";
cin>>op;

return 0;
}

Naturally, the header file that follows is test_client.h

/**
* Test Dynamic Library File
*
* (C) 2009 Dennis J. McWherter, Jr. All Rights Reserved.
*
*/

#ifndef TEST_HEADER
#define TEST_HEADER
#define DLL_IMPORT __declspec(dllimport) // Same concept as export
// Il stesso concepto come export

// Define the class

// What is the purpose of redefining? That's all you need to do is redefine structure ;)
// and not function :)
// Perchè ti deve ridefinire la class? Perché solo lo struttura si deve essere definire!
// e no funziona :)
class DLL_IMPORT test_dll{
public:
test_dll(); // Constructor/Costruttore
char func1(char lang); // First function definition/La definizione della prima funziona
int func2(float x,float y,char op);
};

#endif

Within these two files, they are essentially creating a calculator. Since I wrote this program in dual languages, I added SIMPLE (meaning hard-coded) dual language support into the program. Also, this gives a reason for the first function, just to see how one can use multiple functions from a DLL just as if they were in the source.

If anyone has further questions or is receiving errors, feel free to let me know and I will be glad to help! Now make sure, however, that everything is set right as well. I ran into an error compiling the client the first time because my project subsystem (found in project properties->linker->System) was set to WINDOWS rather than CONSOLE so it was looking for the WINMAIN where there was none. So just a word of caution to anyone trying this without my source and you are receiving a WINMAIN compilation error.

So the land of DLL’s really aren’t as complex as it seems. It’s simply a closed source utility which helps other developers in WINDOWS programs.

Regards,
Dennis M.

DLL Tutorial Source Code and Binaries

Importance of Structure and Coding Etiquette

August 7th, 2009 admin No comments

Well, it’s been a very long time since I last updated and I’d like to apologize to all my subscribers for that. I’ve been very busy, but it seems the work load is going down and I’ll have more time to continue writing! Now, on with the article.

So recently, I have just finished a project where one developer had started and then decided he could not finish the work, so I was hired to finish it. The natural thought to one who is inexperienced is, “This will be a cakewalk. Most of the programming is already done!” – wrong. The first thing that went through my mind was, “I wonder how bad this really is.” So, I accept the project (as I had only a few projects at the time) and take a look.

The code was atrocious to say the least. I felt as if this other developer had never learned how to use comments or his tab key/space bar to format. Most of the time on the project was bent around figuring out what the original developer had tried to do. It was a nightmare.

As I started digging through files and files of unnecessary sloppy code, I thought to myself, “I need to write something about this. This kind of work needs to stop.” I was not upset because of the amateur programming, nor the fact that it was undocumented and poorly written. What bugs me is the fact that someone paid for that kind of work. It looked like the developer copy/pasted everything from snippets he or she found online. That being said, one must learn the importance of structure and coding etiquette.

Structure is important for general organization. It keeps code neat and clean looking and much easier for anyone, to include yourself, to go back and fix errors/security holes. Most people see structural formatting as a simple aesthetic quality when in reality it is like formatting a letter. The structure keeps things organized and understandable on a more universal level.

Coding etiquette, on the other hand, is something learned over a long period of time. No new developer can simply logon and expect to program to the standards set right now, but at the same time should begin mimicking the styles of major developers. Examining the work of others is one of the best ways for any developer to learn, so studying (yes, just like in school) the work of past developers, and prominent works of today, one can easily understand how to program professionally. A simple example would be to write functions rather than hardcode functions multiple times. Or rather than using raw MySQL functions, create an SQL wrapper to execute the functions.

There are many resources on learning how to program professionally, and be neat, but it’s up to developers to use the tools. The vast majority of developers, I would say, hold to the standards. However, for those who do not, they are just ripping off their client in the long-run.

Regards,
Dennis M.

Using php mail(); and headers

July 16th, 2009 admin 1 comment

So today, I came across an interesting question today which never had come up before to me. The PHP mail(); function. It is such a seemingly simple function, but often misunderstood; largely because its format and raw mail syntax.

PHP mail(); does not offer a specific field for each and every possible mail option, but we’ll go into it later. We’ll go through the very basics, then start touching upon what I am alluding to (headers).

The basic syntax is: mail(TO,SUBJECT,MESSAGE,HEADERS).
I assume one can figure out what each of those are (provided that you speak English well).

Now, since the function itself is self-explanatory, we’re back to the reason for this article; headers. Headers are optional fields in a CLR format using \r\n line-breaks between each of the different types. A common header would be something like from. For clarity (and cleanliness), headers are usually stored in a variable, so let’s take a quick look at how one code make this work.

<?php
$headers = "From: your@email.com\r\n
CC: a.public@email.address.com\r\n
BCC: secret@user1.com,\r\nsecret@user2.com,\r\nand@so.on.com\r\n";

mail("someone@somewhere.com","Test e-mail!","Some message",$headers);
exit();
?>

The above code should thoroughly explain headers. There are many different fields one could put in there, but these are just a few. The above example includes a from address, a CC, and a BCC (with multiple addresses).

So overall, the php mail(); function is a simple, yet very useful tool for many users to use. A much better alternative to some forms, especially the last resort “mailto” link type.

Regards,
Dennis M.

Categories: PHP Tags: