Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php captcha

Featured Replies

Morning all,

I dont do php... yet, so at the moment i just use and edit available scripts

installed on = www.jayharrisofficial.com/contact.php

 

but the captcha doesnt seem to match up.

 

here is the code. for captcha.php

<?php
session_start(); // start a session
$image = imagecreate(50, 20); //create blank image (width, height)
$bgcolor = imagecolorallocate($image, 0, 0, 0); //add background color with RGB.
$textcolor = imagecolorallocate($image, 255, 255, 255); //add text/code color with RGB.
$code = rand(1000, 9999); //create a random number between 1000 and 9999
$_SESSION['code'] = ($code); //add the random number to session 'code'
imagestring($image, 10, 8, 3, $code, $textcolor); //create image with all the settings above.
header ("Content-type: image/png"); // define image type
imagepng($image); //display image as PNG
?>

 

and for contact.php

<?php
if (isset($_POST['contact_submit'])) {
$error = "";
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$error .= "You didn't type in your name. <br />";
}
if (!empty($_POST['company'])) {
$name = $_POST['company'];
} else {
$error .= "You didn't type in your company name. <br />";
}
if (!empty($_POST['companytype'])) {
$name = $_POST['companytype'];
} else {
$error .= "You didn't type in your comapny type <br />";
}

if (!empty($_POST['telephone'])) {
$name = $_POST['telephone'];
} else {
$error .= "You didn't type in a contact telephone number <br />";
}

if (!empty($_POST['city'])) {
$name = $_POST['city'];
} else {
$error .= "You didn't type in your Location <br />";
}

if (!empty($_POST['email'])) {
$email = $_POST['email'];
 if (!preg_match("/^[_a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
 $error .= "The e-mail address you entered is not valid. <br/>";
 }
} else {
$error .= "You didn't type in an e-mail address. <br />";
}
if (!empty($_POST['message'])) {
$message = $_POST['message'];
} else {
$error .= "You didn't type in a message. <br />";
}
if(($_POST['code']) == $_SESSION['code']) {
$code = $_POST['code'];
} else {
$error .= "The captcha code you entered does not match. Please try again. <br />";
}
if (empty($error)) {
$from = 'From: ' . $name . ' <' . $email . '>';
$to = "info@jayharrisofficial.com";
$subject = "JayHarrisOfficial Website has sent you a message.";
$content = $name . " has sent you a message: \n" . $message;
$success = "<h3>Thank you! Your message has been sent!</h3>";
mail($to,$subject,$content,$from);
}
}
?>
<div id="contactForm">


<?php
 if (!empty($error)) {
 echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) returned:</strong><br/>' . $error . '</p>';
 } elseif (!empty($success)) {
 echo $success;
 }
?>
 <form action="contact.php" method="post">

<p class="form_p">Name: <input type="text" name="name" class="contact_input" value="<?php if ($_POST['name']) { echo $_POST['name']; } ?>" /></p>
<p class="form_p">Company: <input type="text" name="company" class="contact_input" value="<?php if ($_POST['company']) { echo $_POST['company']; } ?>" /></p>
<p class="form_p">Company Type: <input type="text" name="companytype" class="contact_input" value="<?php if ($_POST['companytype']) { echo $_POST['companytype']; } ?>" /></p>
<p class="form_p">Email: <input type="text" name="email" class="contact_input" value="<?php if ($_POST['email']) { echo $_POST['email']; } ?>" /></p>
<p class="form_p">Telephone: <input type="text" name="telephone" class="contact_input" value="<?php if ($_POST['telephone']) { echo $_POST['telephone']; } ?>" /></p>	
<p class="form_p">City: <input type="text" name="city" class="contact_input" value="<?php if ($_POST['city']) { echo $_POST['city']; } ?>" /></p>
<p class="form_p">Message: <textarea name="message" rows="10" cols="45"><?php if ($_POST['message']) { echo $_POST['message']; } ?></textarea></p>
<p class="form_p"><label><img src="includes/captcha.php"></label></p>
<p class="form_p"><input type="text" name="code"> <br /> </p>
<p class="form_p"><input type="submit" class="contact_submit" name="contact_submit" value="Send message" /></p>

</form>

</div>

 

any help would be great, like i said i havnt ventured into php yet.

 

Cheers

Edited by craigpettit
Code formatting, dude

  • Author

I can't be remotely arsed to fix it, but the forum already has syntax highlighting - just use the code button or BBtags.

 

Thanks...

that wasnt really helpful tbh

that wasnt really helpful tbh

 

Fine. The code button is the one on the editor that looks like this: <>

 

BBtags are a simplified form of forum markup (BBCode) that's opened and closed like HTML but uses square brackets. So you'd want <code> and </code>, replacing the < > with [ ]. Better?

You need to start a session in contact.php to access $_SESSION['code'] created in captcha.php. Also I would imagine your form is displaying warning messages in the fields when the form is first displayed. In every input field you need to check that the $_POST variable exists because first time round it won't. Change

value="<?php if ($_POST['email']) { echo $_POST['email']; } ?>"

to

value="<?php if (isset($_POST['email']) ) { echo $_POST['email']; } ?>"

You need to start a session in contact.php to access $_SESSION['code'] created in captcha.php. Also I would imagine your form is displaying warning messages in the fields when the form is first displayed. In every input field you need to check that the $_POST variable exists because first time round it won't. Change

value="<?php if ($_POST['email']) { echo $_POST['email']; } ?>"

to

value="<?php if (isset($_POST['email']) ) { echo $_POST['email']; } ?>"

to elberate on the above code id just create a simple function to handle post request/get request and so on like below which also checks if the $_POST superglobal is actually populated with content or not using isset

<?php
function post($post){
if(isset($_POST[$post])){
return $_POST[$post];
}
}

Not a bad mail script on the whole, you would not believe how many do not sanitise that email field to prevent injection attacks.

 

Tip#

The GD library can use custom fonts (.ttf) and unusual ones in particular can play havoc with true spam bots pattern recognition scripts, humans paid to solve captcha's just find them more annoying.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.