Processing URLs - Page 13
September 24, 2001
It's fairly common to embed URLs in e-mails and dynamic HTML that
will direct users to a personalized site. To do this properly,
however, you usually append one or more parameters at the end of
the URL (after the question mark). For example, I might point my
subscribers to a new product offering via a personalized e-mail.
In my example, I need to pass the user's full name. This can be
problematic because, like HTML, there are certain reserved
characters that you can't put in a URL. Here's a grueling example
of a URL that I might want to build, but would make my browser
unhappy:
http://www.wdvl.com?userid=jonnyo"tagline=[Jonny O'Neil is a
real brawler]"text=Give me that stick you silly
goat"access=admin-0+user-3+guest-5
[The lines above are one line. They have been split for
formatting purposes.]
Let's pretend for just a minute that I'm going to send an e-mail
and I would like to pass each user's information as part of the
URL which is picked up by the script and used to customize a Web
page. We have the userid, a user
tagline, the text that the user has
typed, and the user's access levels.
The URI::Escape module automatically encodes URL
reserved characters making it much easier to build dynamic URLs
that contain characters like spaces, semi-colons, slashes,
question marks, plus and minus signs, dashes, and all of those
other little nasties that will make Web browsers and servers
unhappy. To encode the URL pass it to the uri_escape
function:
my $url =
uri_encode("http://www.wdvl.com?userid=jonnyo"tagline=[Jonny
O'Neil is a real brawler]"text=Give me that stick you silly
goat"access=admin-0+user-3+guest-5");
[The lines above are one line. They have been split for
formatting purposes.]
The value of $url would now equal:
http%3A%2F%2Fwww.wdvl.com%3Fuserid%3Djonnyo%26quot%3Btagline%3D
%5BJonny%20O'Neil%20is%20a%20real%20brawler%5D%26quot%3Btext
%3DGive%20me%20that%20stick%20you%20s
[The lines above are one line. They have been split for
formatting purposes.]
Well, now we have a problem. While the function did encode all of
those special characters, it ran over the maximum URL length and
had to append a few characters. This can be a problem, which is
why you shouldn't try to include too much information in a URL
(like I just did). But the practicality of this function remains
where you need to encode a URL, especially when the contents are
dynamic.
Checking Credit Card Numbers
Processing credit cards on the Web is common place these days.
But credit card processing happens in many other places as well.
Before sending credit card information to a credit card merchant,
it's smart to do a sanity check on the credit card numbers in the
cases where a user or salesperson accidentally or purposely
entered an invalid number. The Business::CreditCard
module is able to validate credit cards based on the number and
also tell what type of credit card it is. Note that it doesn't
actually contact a merchant to process the credit card, this is
just a sanity check. So if I had card number 1234 5678 9012
3456 and wanted to check to see if it was a valid number:
#!/usr/bin/perl -w
use Business::CreditCard;
my $ccnum = "5276 4400 6542 1319";
if (validate($ccnum)) {
print "Card $ccnum is valid\n";
} else {
print "Card $ccnum is invalid\n";
}
print "cardtype is ". cardtype($ccnum) . "\n";
The code above checks to see if the card number is valid with the
validate() function. I've also included the
cardtype() function which tells us what kind of
credit card (Visa, Mastercard, etc.) it is. By the way, that's
not a real credit card number so don't try using it silly.
Conclusion
So we've had a short tour of a few perl modules that make
processing text easier for us. I hope you've learned alot from
this series. If you have any questions or problems, feel free to
send an e-mail to
eisen@pobox.com.
Additional Resources
Encrypting Text with RC4 - Page 12
Weaving Magic With Regular Expressions
Automating Image Manipulation with GD - Page 14
|