Title: Basic Tell a Friend Script
Notes: This was a real quick one, but I wanted to see what you guys want to say about it, especially since it was my first script when I made it. It's semi-organized code...
Code:
[INDENT]file.html[/INDENT]

<html>
<head>
<title>Tell a Friend Script</title>
</head>
<body>
<!-- begin Tell a Friend form -->
<?
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
?>
<p><font face="Verdana" size="2" color="red"><b><? echo $errorMessage; ?></b></font></p>
<form action="submit.php" name="email" target="_blank" method="post">
<input type="hidden" name="url" value="<? echo $url; ?>">
<table><tr>
<td><b><font face="Verdana" size="2">Your name:</font></b></td>
<td><input type="text" name="sender"></td></tr>
<tr><td><b><font face="Verdana" size="2">Your email:</font></b></td>
<td><input type="text" name="senderemail"></td></tr>
<tr><td><b><font face="Verdana" size="2">Friend's name:</font></b></td>
<td><input type="text" name="recipient"></td></tr>
<tr><td><b><font face="Verdana" size="2">Friend's email:</font></b></td>
<td><input type="text" name="recipientemail"></td></tr>
<tr><td><b><font face="Verdana" size="2">Verify Code: </font><font face="Verdana" size="1"><a href="#" onclick='alert("Type the text you see in the picture below in the box.\nThis form of verification ensures that you are not an automated computer. It protects this script from being used for spamming. \"Robots,\" programs that randomly sift through Web sites, are able to access these forms and can use them to spam, but since they cannot actually read the characters in the box because it is an image, they cannot submit the form.");'>?</a></font></b>
<br><img src="randomImage.php" border="0" name="imgverify"></td>
<td valign="top"><input type="text" name="verify"></td></tr>
<tr><td colspan="2"><b><font face="Verdana" size="2">Message: </font><font face="Verdana" size="1"><a href="#" onclick='alert("When customising your message, keep in mind that typing \"%url%\" will display the link to the web page you are submitting automatically when your friend receives the email.");'>?</a></font>
<br><textarea name="message" rows="5" cols="29">Hey!
I found this really cool site, you should check it out for yourself!
Go here: %url%</textarea></center></td></tr>
<tr><td colspan="2"><center><input type="submit" value="Send" name="send"></center></td>
</tr></table>
</form>
<!-- end Tell a Friend form code -->

[indent]submit.php[/indent]

<?php
session_start(); // Begin session

$errorMessage = '';
if (isset($_POST['sender']) && isset($_POST['senderemail']) && isset($_POST['recipient']) && isset($_POST['recipientemail']) && isset($_POST['verify']) && isset($_POST['message'])) {
	$number   = $_POST['verify'];

if (md5($number) == $_SESSION['image_random_value']) {		
$sender = $_POST['sender'];
$senderemail = $_POST['senderemail'];
$recipient = $_POST['recipient'];
$recipientemail = $_POST['recipientemail'];
$message = $_POST['message'];
$url = $_POST['url'];
$message = str_replace("%url%",$url,$message);


		// remove the random value from session			
		$_SESSION['image_random_value'] = '';

		mail("$recipient <$recipientemail>;","Check out this web page!",$message,"From: $sender <$senderemail>;");
		echo "<font face='verdana' size='6' color='blue'>Thank you!</font><br><font face='verdana' size='2'>The email was sent successfully from <b>$sender</b> to <b>$recipient</b>!</font>";
		echo "<hr><font face='verdana' size='2'><b>To:</b> $recipient &lt;$recipientemail&gt;<br><b>From:</b> $sender &lt;$senderemail&gt;<br><b>Message:</b> $message</font>";
		echo "<br><br><font face='verdana' size='1'><a href='javascript:self.close();'>[ close ]</a><hr>Tell a Friend script &copy;2006 <a href='http://www.thirdfromlast.co.nr/' target='_blank'>TFL</a>.</font>"; // Please do not remove copyright!
		exit;
	} else
	{
        echo "<font face='verdana' size='2' color='red'><b>Error: Incorrect image verification!</font></b>";
        echo "<br><br><font face='verdana' size='1'><a href='javascript:self.close();'>[ close ]</a><hr>Tell a Friend script &copy;2006 <a href='http://www.thirdfromlast.co.nr/' target='_blank'>TFL</a>.</font>"; // Please do not remove copyright!

        exit;
	}		
}	
else { 
echo "<font face='verdana' size='2' color='red'><b>Error: All fields are required!</b></font>";
       echo "<br><br><font face='verdana' size='1'><a href='javascript:self.close();'>[ close ]</a><hr>Tell a Friend script &copy;2006 <a href='http://www.thirdfromlast.co.nr/' target='_blank'>TFL</a>.</font>"; // Please do not remove copyright!
    exit; }
?>

[indent]randomImage.php[/indent]

<?php
session_start();

$rand = rand(10000, 99999);

$_SESSION['image_random_value'] = md5($rand);

$image = imagecreate(60, 30);

$bgColor = imagecolorallocate ($image, 255, 255, 255); 

$textColor = imagecolorallocate ($image, 0, 0, 0); 

imagestring ($image, 5, 5, 8,  $rand, $textColor); 

// send several headers to make sure the image is not cached	
// taken directly from the PHP Manual

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 

header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 

header("Pragma: no-cache"); 	


header('Content-type: image/jpeg');

imagejpeg($image);

imagedestroy($image);
?>

Bibliography: Various Internet sources (unfortunately I can't remember most) off of Google--to find how to display referring URL, javascript:self.close();, etc.
http://www.php-mysql-tutorial.com/ for the random image generator.

Demonstration: [ link ]
NOTE: The above URL is a free web host. There is a spamming limit there of six emails sent per day by user (by web site hosted) and in the email you receive you get a stupid ad. I don't have a way to pay for hosting at the moment, so DEAL WITH IT or upload the script to your server to see it work.

    Two points right off the top of my head:
    1) EEEEewwwww! <font> tags!
    2) $rand = rand(10000, 99999); Any particular reason you're limiting $rand to only 90,000 distinct values (less in the case of some operating systems)?

      Just because I only wanted the image to be five-digit numbers. Like I said, that's not my code, it's from a tutorial and I used it...

      And <font> tags are there because I didn't feel like using CSS.
      And I admit! I cheated: I used MS FrontPage to create the table because it's faster than hardcoding and it's only HTML. FrontPage doesn't automatically make CSS, you have to tell it to.

        Hi

        I think you may want to add trim, stripslashes and email verification to your code for security reasons.

          19 days later

          Maybe the first two, but I never liked email verification. It's just too complicated and I don't think they need to bother. It's more work for the user than needed. Besides, I'll prevent mass spamming with the image verification, usually...

            Write a Reply...