DieYuppieScum Homepage | Link-O-Rama | DieYuppieScum Webmail! | Send Feedback |
| Home | Tools | Glossary | NP FAQ | About DieYuppieScum |  

From <a href=http://www

From phpdeveloper.org



E-Cards with PHP & MySQL

by baxxy

So you want to make e-cards for your site, eh? I recently did this, and it took me a while to get it all working right, so here's a quick tutorial so you won't have to suffer what I did. You may notice a lot of references to "floppy dogs" in this tutorial, but that's because it comes straight from my own code on floppydogs.com.

You'd think it would be a quick and easy sessions piece, right? This didn't work. I don't know exactly why, but even though I did session_destroy(), the session ID refused to go away. The problem this created was when a user sent one e-card and then tried to go back and create a new one. The new one wouldn't work because the error of a duplicate ID came up. What a bother.

First of all, I had to make the pictures. Obviously. Those are all displayed on one page, index.php, and here's their code:

<A HREF="makecard.php?image=imagename.jpg">

Naturally, each image has a different imagename. Now for makecard.php, where the user gets to put in his/her/its personal information. This is where I used sessions. In case you skipped that tutorial, here's the brief rundown:


Registering "image" means that the image the user chose will stick to that variable name until the session is destroyed. All the session information has to go before the HTML even starts. Now, the form:

<IMG SRC="<?echo $image;?>">
<form action="sendcard.php" method=GET>

Your Name:
<input type=text name=s_name size=25 maxlength=50>
Your Email Address:
<input type=text name=s_email size=25 maxlength=50><?escapeshellcmd($s_email);?>
Receiver's Name: <input type=text name=r_name size=25 maxlength=50>
Receiver's Email Address: <input type=text name=r_email size=25 maxlength=50>
Your Personal Message:<BR>
<TEXTAREA name=message cols=50 rows=5 wrap></TEXTAREA>

<input type=submit value="Send my card!">
<input type=reset value="Clear it">

If you know how to use forms, most of that should make sense immediately. escapeshellcmd() is a security feature so that malicious users can't put mySQL commands into the fields and get your information. If you want to know all the details, you can look that up in the manual.

One place I ran into a problem was if the user chose a picture, then changed his/her/its mind and went back. Because the image was a registered variable, the first image remained no matter what was chosen. This, fortunately, was an easy fix. At the top of the index.php page, I simply added:


Again, remember to put that before any of the HTML.

Now to send the card. The first step was to create a table inside mySQL. You can find great tutorials for that on devshed.
To give each card an individual ID, I used a function (which goes before the HTML, again), because as I mentioned, session_destroy() didn't appear to be destroying the ID itself, just the information within the session. If some of you have Core PHP Programming, you'll probably recognize this function... that book is my main resource.

function CreateID($length=16){
$Pool .= "1234567890floppydogs";
for($index = 0; $index < $length; $index++){
$sid .= substr($Pool,(rand()%(strlen($Pool))), 1);

Basically, that generates a random assortment, 16 chars long, made up of those chars I provided within $Pool.
Now to put everything into the mySQL table (and remember to keep your variables in order!). If there is already an entry with the same ID, mySQL will automatically return an error, in which case, the if(!result) will run, generating a new ID.

$result = mysql_query("INSERT INTO ecards VALUES('$sid', '$s_name', '$s_email','$r_name', '$r_email','$message', '$image')");
$result = mysql_query("INSERT INTO ecards VALUES('$sid', '$s_name', '$s_email','$r_name', '$r_email','$message', '$image')");

Now to mail the lucky recipient a message letting him/her/it know he/she/it has an e-card! And don't forge to destroy the session at the end so the user can go make another card if he/she/it wants to.

$mailTo = "$r_email";
$mailSubject = "You Have A FloppyDog!";
$mailHeader = "From: $s_name";
$message = "You lucky person, you! Someone has thought of you in a warm and fuzzy way and sent you a floppydog studios (TM) e-card! You can view this card at the following webpage: ";
$message .= "http://www.floppydogs.com/ecards/viewcard.php?ID=";
$message .= $sid;
mail($mailTo, $mailSubject, $message, $mailHeader);

That's it! Have fun e-carding, and if you want to see how it all works when put together, feel free to send some e-cards to your friends from floppydogs.com


OpenSolaris: Innovation Matters

[FSF Associate Member]

These guys help me get a few hits here and there; they deserve a look.They've got quite a 
list goingMore Anime / 
Manga stuff than the likes of you could ever handle as opposed to 
'Sluggy Contractor'
Total Number of Hits: 42134Zillion hits.
(As of moving to the new server)
I'm Watching You. With Pierre's Help, of course.

Here's Lord Nougat's Geek Code, as if anybody cares:

-----BEGIN GEEK CODE BLOCK----- Version: 3.1

GIT$ dx@ s+:- a C++++ UBLS*+ P+ L++ E- W++ N+ o? K- w O-- M->+ v- PS+++ PE Y++@ PGP+@ t+ 5- X+ R tv b++ DI++ D+ G+ e+ h* r+++ z+


"Their hatred for homework streams into space. It forms a monster there." -Some Guy in "Godzilla Vs. Gigan" (1972)

01:28:48 up 140 days, 9:22, 0 users, load average: 0.08, 0.04, 0.12

©1999,2000,2001,2002,2003, etc,etc,etc dieyuppiescum.org. registered trademarks™ mentioned in here are registered trademarks.
Any unauthorised reproduction will leave me with no discourse but to ruthlessly glare at, yell at, and be generally mean to cute fuzzy wilderness creatures.
The fine text: By reading, talking about, mentioning, viewing, tasting, smelling, fondling, or feeling my site, you consent to relenquish, upon request, your soul, money, car, house, catfood, left shoe, old 12" monochrome monitor, cheese grater, or life, to the almighty LORD NOUGAT, and as mentioned herein shall be henceforth et cetera and so forth pluribus ad nauseam bla bla bla, until a minimum of 54782 and no more than 6

This page was made entirely with things you couldn't possibly understand.