Appendix C: Functions

Google
All the functions described in the text and in this Appendix are in a separate file named library.php. You may choose to simply include that file in scripts that can use any of the functions in the library. Or you may choose to copy and paste any function(s) into scripts you're writing or enhancing.

You can call as many of these functions as you need to validate the entries on a form. If you wanted a count of how many errors occurred, you could call them this way:

$result = 0;
$result += verify_fname($fname, $fail);
$result += verify_lname($lname, $fail);
etc....

The final value of $result will be the number of errors. Each function would add either 0 or 1 to the running total in $result. You might want to just check whether it's zero or not and use that result to determine whether to display any error text.


Functions for use with forms, etc.

  • verify_fname(name) - checks for empty or invalid first name; returns 1 on failure and adds text to $fail.

  • verify_lname(name) - checks for empty or invalid last name; returns 1 on failure and adds text to $fail.

  • verify_address(address) - checks for empty or invalid address; returns 1 on failure and adds text to $fail.

  • verify_city(city) - checks for empty or invalid city; returns 1 on failure and adds text to $fail.

  • verify_state(state) - checks for empty or invalid state; can check for province/county when state is not a US state or a Canadian province; returns 1 on failure and adds text to $fail.

  • verify_zip(zip) - checks for empty or invalid zip; returns 1 on failure and adds text to $fail.

  • verify_zip2(zip) - checks for empty or invalid zip; can use a patterns array to verify virtually any postal code; returns 1 on failure and adds text to $fail.

  • verify_country(country) - checks for empty or invalid country; returns 1 on failure and adds text to $fail.

  • verify_email(email_address) - checks for empty or invalid email address; can compare email address or domain to a list of unacceptable addresses and/or domains; returns 1 on failure and adds text to $fail.

  • verify_phone(phone) - checks for empty or invalid phone number; returns 1 on failure and adds text to $fail.

  • verify_cell(cell) - checks for empty or invalid cell phone number; returns 1 on failure and adds text to $fail.

  • verify_fax(fax) - checks for empty or invalid fax number; returns 1 on failure and adds text to $fail.

  • verify_company(company) - checks for empty or invalid company name; returns 1 on failure and adds text to $fail.

Miscellaneous functions

check_url() - checks to see if the request to use your script originated from a server you control. You may list as many servers in the array as you like. The script will exit instantly if the request is from an unlisted server. Note that it is possible for some people to fake the server name in the request. This is known as "IP spoofing." That means that this routine can't defend against all unauthorized use of your script. Still, it provides some security at little or no cost.

Call it like this:

if (check_url != 1} die();

cleanup(item) - takes a string of characters and removes semi-colons, PHP start and end markers, HTML tags (including JavaScript!) and more; sanitizes inputs for your protection.

Call it like this:

$item = cleanup($item);

dollar(real_number) - will force the value of $dollar_amount to have exactly two digits after the decimal point. It will also round up if there was originally a third digit after the decimal point in $price that is 5 or larger.

Call it like this:

$dollar_amount = dollar($price);

expires(days) - calculates an expiration date or any other future date and time.

Call it like this:

$exp = expires(2);


On Your Own

Whenever you find a really useful function, add it to the library.php file. You may find new functions in scripts that you download or that you write yourself. Sometimes you write a really clever chunk of code that you'd like to re-use. If the code isn't already in the form of a function, a few quick edits will fix that.

First, make a generic function body like this:

function func_name ()
{
the code goes here...
}

Next, replace "the code goes here..." with your actual PHP code.

Then replace "func_name" with the name you want your function to have.

Finally, put any arguments inside the parentheses after the function's name. Arguments are just variables being passed to the function. There are a couple of things to remember about the arguments.

Most of the time they are passed in as normal variables (such as $price). This means that a copy of the variable $price is created inside the function body. It can be modified by code inside the function and it won't affect the original variable. It will vanish when the function finishes.

But sometimes you pass a variable to a function and you want it to change - depending on what happens when the function is running. To do that just put an & in front of the variable's name in the function's argument list. Like this:

function MyFunc(&$myVar);

Now if $myVar gets changed inside MyFunc(), the orginal $myVar will be changed, too.

Maybe you're wondering how this is possible? OK, I'll try to explain it. You see, $myVar is a variable or symbolic name for an address in memory where something is stored. BUT - &$myVar is really the address itself. So &$myVar gives us access to the memory address where "something" is stored rather than the "something" itself.

When we assign some new value to $myVar in a function where it appeared (in the argument list) as &$myVar, we actually write a new "something" into that memory address. This changes the actual contents of $myVar. Programmers call this "passing by reference" as opposed to "passing by value."

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey