Simple PHP email form with basic spam protection CAPTCHA

There are two pieces of code – the one which will display your email form, and a block of code to add into your “mailer” page – a page dedicated to sending the messages and displaying confirmations and errors.

Feel free to edit or tweak the code, download the images to your own server and use however you wish.

The form

<!-- email box ~ -->
<div style="width:400px;text-align:right;padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
<form action="send.php" method="post" style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
<p style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
Name: <input type="text" name="email_name" size=20><br>
Email: <input type="text" name="email_email" size=20>
<?php
$one = 1;
$two = 0;
$three = 0;
$four = 0;
while ($one % 2) $one = rand (10000,99999);
while (!($two % 2)) $two = rand (10000,99999);
while (!($three % 2)) $three = rand (10000,99999);
while (!($four % 2)) $four = rand (10000,99999);
?>
<br>Are you a human or internet virus?
<br>Choose the odd one out:
</p>
<div style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
<input type="radio" name="email_valid" value="<?php echo $four ?>" Checked=1> <img src="http://Pushka.co/i/sys/lion.png" alt="This is a public domain image">
<input type="radio" name="email_valid" value="<?php echo $two ?>"> <img src="http://Pushka.co/i/sys/elephant.png" alt="This is a public domain image">
<input type="radio" name="email_valid" value="<?php echo $one ?>"> <img src="http://Pushka.co/i/sys/lifter.png" alt="This is a public domain image">
<input type="radio" name="email_valid" value="<?php echo $three ?>"> <img src="http://Pushka.co/i/sys/penguin.png" alt="This is a public domain image">
</div>
<p style="width:200; text-align:left;padding-bottom:0px; margin:0px 0px 0px 0px;padding-top:0px; padding-left:150px;">Message:</p>
<textarea rows="7" cols="30" name="email_msg" style="padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;">
</textarea><br>
<input type="submit" value="Send">
</form>
</div>

The sender and confirmation message generator (should be in the main text region of your regular website theme)

<div title="main"><h1>Send</h1>
<div style="width:400px;"><br /><br />
<?php
$name = $_POST["email_name"];
$email = $_POST["email_email"];
$valid = $_POST["email_valid"];
$message = $_POST["email_msg"];
$error = 0;
if (strlen($email) == 0) {
echo "<img src='http://Pushka.co/i/sys/cross.png'> Please enter an email<br>";
$error =1;}
elseif (strpos($email,"@") === false | strlen($email) < 6){
echo "<img src='http://Pushka.co/i/sys/cross.png'> Please enter a valid email in the form X@X.XX<br>";
$error=1;}
if (strlen($message)==0){
echo "<img src='http://Pushka.co/i/sys/cross.png'> Please enter a message<br>";
$error=1;}
if ($valid%2){
echo "<img src='http://Pushka.co/i/sys/cross.png'> There was an error with the image validation, please select the odd one out.<br>";
$error=1;}
if (strlen($name) == 0) $name = $email;
$message = "This message was sent from Pushka.co - from " . $name . " \n ---------------------------- \n \n " . $message . " \n \n ---------------------------- \n \n \n";
if ($error==0){
echo "<img src='http://Pushka.co/i/sys/tick.png'> Email Sent<br><br><FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'> </FORM> <br>";
$to = 'you@mail.com';
$subject = 'Message from Pushka.co';
$headers = 'From: ' . $email . " \r\n" .
'Reply-To: ' . $email . " \r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
else{
echo "<br><FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'> </FORM> <br>";
}
?>
<br><br>
</div>
</div>

 Here is an example

The emails are sent to the Cosmic Owl, who may or may not ever respond to you

  • Name:
    Email:
    Are you a human or internet virus?
    Choose the odd one out:

    This is a public domain image This is a public domain image This is a public domain image This is a public domain image

    Message:


    Send



    Please enter an email
    Please enter a message




You can have both blocks of code on the same page if you have action=”"

double click animation, double right click GIF animation

I made these two animations, since I needed them for explaining that in embedded Google Maps, you can double left or right click to zoom in and out..

Double Click GIF Animation
left-click-double-gif-animationDouble Right Click GIF Animation
right-click-double-gif-animation

You can download the ZIP of both GIFs and the photoshop file, if you want to edit it – you can make single click too
The original image is GPL licence, as are my derivative edits.

Double click Animation – GIF, double right click ZIP file - Free for any use.

HTML Back button for website

If you would like to add a back button on your website, rather than asking people to click back (which some people might never do when you want them to..)

You can use this HTML Back Button:

<form><input type=”button” VALUE=”Back” onClick=”history.go(-1);return true;”></form>

Shows up like this:

 

 

Redirect Dynamic URL to Static Link and Redirect old to new no loop error

I spent such a huge amount of time trying to find the answer to this problem, and different servers might behave differently, I spent a long time trying so many different variations and combinations

In the end I couldn’t get it to work, and so I renamed the PHP file that was generating the pages:

EG:

  • I want
  • http://PushkaCom.com/product.php?id=6
  • to redirect to
  • http://PushkaCom.com/products/toasters/

rename product.php to p-product.php

RewriteCond %{QUERY_STRING} ^id=6$
RewriteRule ^product\.php(.*)$ http://PushkaCom.com/products/toasters/? [R=301,L]
RewriteRule ^products/toasters/$ p-product.php?id=6 [NC,L]

This was coming up as an infinite loop…

RewriteEngine On
[R=301,L]
RewriteCond %{QUERY_STRING} ^id=6$
RewriteRule ^product\.php$ http://PushkaCom.com/products/toasters/? [R=301,L]

RewriteEngine on
RewriteRule ^products/toasters/$ product.php?id=6 {{{maybe add  [R=301,L]  and switch the two?? }}}

Make a static website using templates

CMS like WordPress are great for easily uploading and editing content, using templates and databases, but what if you don’t have a database, only HTML files?

  • add .PHP to all files,
  • cut out the head and footer, adding them to external files so that only the body text is in your PHP files
  • add this for the head and foot of each page
    <?php include $_SERVER['DOCUMENT_ROOT'].”/head.html ?>
    <?php include $_SERVER['DOCUMENT_ROOT'].”/head.html ?>

And there you have it, template.

Done.

Why this is awesome:

  • If you need to edit something in the header or footer, you can edit it once, in these two files, rather than editing every page. This is similar to the logic behind external CSS
  • If you want to change the template of your website, you can just edit these two files
  • You don’t need to install a database driven program to run your template

JQuery Javascript content rotator layering text scrambled on page load

On some content rotators, while the page is loading and the javascript is rendering the rotator, all the elements of the roator are layered on top of each other, so the text is layered on top of each other and looks like a mess…

Javascript is executed line by line as the HTML document is read, so what you can do, is hide the text with javascript at the top of your HTML page, and then fade it in at the bottom of your page, meaning that when your page loads, the text won’t all be visible until everything is ready to be displayed:

Add to beginning of <head>

<script type="text/javascript">

document.write("<style type='text/css'>.headerText{display:none;}</style>");

</script>

And add this to the bottom of the footer, before </body> if possible:

<script type="text/javascript">//<!--

//$(window).bind("load", function() {

// you can uncomment these

//two things to load this script when the page finishes loading,

//but that could add to a delay in the text being shown...

$('.headerText').fadeIn('slow', function() {

// Animation complete

});

//});

//-->

</script>

 

If URL contains X, Then do this in PHP

You can customise the code of your website using this function in PHP:

<?php if (stripos($_SERVER['REQUEST_URI'],’/current-page/’) !== false) {echo ‘class=”selected”‘;} ?>

or to check if the URL = something:

$urllow = $_SERVER['SCRIPT_URI'];
$urllow = strtolower($urllow);
if ((strpos($urllow,"search-text"))){
do something...
}

EG: showing a different header on a page etc

GitHub Notes, Social Coding

I’m working on a website with one other developer who lives out of sydney, and we’re collaborating through Git, the system used for the creation of various pieces of software, web apps and other programs.

These three videos helped me a lot with the basics:

 



My quick reference notes:

  • in terminal, cd “c:/location”
  • git status (to see if there is anything to push to the server)
  • git commit -m “comment for the updates”
  • git push https://branch-url.git