14 December 2009

Simple Text File Hit Counter

0 comments
A simple hit counter using a text file. The script records every page hit into the text file called log.txt. The total number of hits is then displayed when the counter script is called.

You must create a blank file called log.txt with an initial value of 0.

<?php

$fp = fopen("log.txt", "r");
$count = fread($fp, 1024);
fclose($fp);

$count = $count++;
echo "Total Hits: $count";

$fp = fopen("log.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>


Assuming you call the script counter.php, then you can call the script by using:
<?php include_once('counter.php'); ?>

PHP Lottery Number Generator

0 comments
Here we have a simply PHP Lottery Number Generator. The script will generate 6 unique numbers from 1 to 49. This was designed for the UK National Lottery but only takes a few seconds to edit for other lotteries.

To change the quantity of numbers you would like to generate, change the 6 on this line to however many numbers you need:
while (count($numbers) < 6) {

To increase the maximum number, change the 49 on both of these lines:
$randomn = rand(1, 49);


<?php
$x = 0;
$numbers[0] = 0;
while (count($numbers) < 6) {
$randomn = rand(1, 49);
while(in_array($randomn, $numbers)) {
$randomn = rand(1, 49);
}
$numbers[$x] = $randomn;
$x++;
echo "Number $x is: $randomn.<br />";
}
?>


Tip: To edit this script for the EuroMillions, simply copy and paste the whole script twice. On the second paste just edit the while loop from 6 to 2, and the rand from 49 to 9.

12 December 2009

PHP Random Number Generator

0 comments
This is a very simple php random number generator script.

Creating a random number takes two commands; srand and rand. srand is a preparation command for rand. It helps rand create a truely random number, rand itself creates the random number.
 
The $from variable is the lowest number possible, and $to is the highest number possible. Simple edit the two variables to suit your requirements.

<?php
$from = 0;
$to = 100;
srand ((double) microtime( )*1000000);
$random_number = rand($from, $to);
echo "$random_number";
?>

8 December 2009

Secure PHP Form With Captcha

0 comments
Here we have a Secure PHP Form with captcha powered by reCAPTCHA.

I wrote this script based on three inputs (Name, Email, and Message) but you can edit it you fit your requirements. All three variables in this script go through a FILTER_SANITIZE_STRING which is used to filter out bad characters from the input. It also uses FILTER_SANITIZE_EMAIL to validate the email address.

You will need to edit line 58 and change the $to variable to your own email address.
 
For reCAPTCHA to work you will need to upload the recaptchalib.php file to the same directory as this script. You can download recapctahlib.php from http://recaptcha.googlecode.com/files/recaptcha-php-1.10.zip.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Secure PHP Form</title></head>

<h1>Secure PHP Form</h1>

<?php
// Check if form has been submitted
if (isset($_POST['submit'])) {
// Form has been submitted

// ReCaptcha Image And Settings
require_once('recaptchalib.php');
$privatekey = "6LcgKgkAAAAAAA0i5ZtgaAyOVkwQn9FsX8qOaSef";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
// Captcha was entered incorrectly
die ("Opps, the captcha image was incorrect. Go back and try again.");
} else {

// Captcha was entered correctly
// Check and validate name variable
if ($_POST['name'] != "") {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($name == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}

// Check and validate email variable
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}

// Check and validate message variable
if ($_POST['message'] != "") {
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($message == "") {
$errors .= 'Please enter a valid message.<br/><br/>';
}
} else {
$errors .= 'Please enter your message.<br/>';
}

// Check for error in the form
if (!$errors) {
$to = "someone@example.com";
$subject = "Secure PHP Form";
$headers = "From: $email";
mail($to,$subject,$message,$headers);
echo '<p>Form Submitted</p>';
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
}
} else {
// Form has now been submitted

?>

<form name="secureform" action="secureform.php" method="post">
Name: *<br /><input type="text" name="name" size="35" /><br />
Email Address: *<br /><input type="text" name="email" size="35" /><br />
Message: *<br /><input type="textarea" name="message" /><br />

<?php
require_once('recaptchalib.php');
$publickey = "6LcgKgkAAAAAAEtp0C1bbBWYRNZvZgoMEXRz_eyO"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>

<input type="submit" name="submit" value="Submit Form" />
</form>

<?php
}
?>

</body>
</html>

Secure PHP Form Without Captacha

0 comments
Update, also see: Secure PHP Form With Captcha.

Here we have a Secure PHP Form without captcha. This script does not use any captcha protection but I will be adding another version very shortly which does use reCaptcha.

I wrote this script based on three inputs (Name, Email, and Message) but you can edit it you fit your requirements. All three variables in this script go through a FILTER_SANITIZE_STRING which is used to filter out bad characters from the input. It also uses FILTER_SANITIZE_EMAIL to validate the email address.

You will need to edit line 43 and change the $to variable to your own email address.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Secure PHP Form</title></head>

<h1>Secure PHP Form</h1>

<?php
// Check if form has been submitted
if (isset($_POST['submit'])) {
// Form has been submitted
// Check and validate name variable
if ($_POST['name'] != "") {
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($name == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'Please enter your name.<br/>';
}

// Check and validate email variable
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}

// Check and validate message variable
if ($_POST['message'] != "") {
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if ($message == "") {
$errors .= 'Please enter a valid message.<br/><br/>';
}
} else {
$errors .= 'Please enter your message.<br/>';
}

// Check for error in the form
if (!$errors) {
$to = "someone@example.com";
$subject = "Secure PHP Form";
$headers = "From: $email";
mail($to,$subject,$message,$headers);
echo '<p>Form Submitted</p>';
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
} else {
// Form has now been submitted

?>

<form name="secureform" action="secureform.php" method="post">
Name: *<br /><input type="text" name="name" size="35" /><br />
Email Address: *<br /><input type="text" name="email" size="35" /><br />
Message: *<br /><input type="textarea" name="message" /><br />
<input type="submit" name="submit" value="Submit Form" />
</form>

<?php
}
?>

</body>
</html>