ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
example_form.php
Go to the documentation of this file.
1<?php
2session_start(); // this MUST be called prior to any output including whitespaces and line breaks!
3
4$GLOBALS['DEBUG_MODE'] = 1;
5// CHANGE TO 0 TO TURN OFF DEBUG MODE
6// IN DEBUG MODE, ONLY THE CAPTCHA CODE IS VALIDATED, AND NO EMAIL IS SENT
7
8$GLOBALS['ct_recipient'] = 'YOU@EXAMPLE.COM'; // Change to your email address!
9$GLOBALS['ct_msg_subject'] = 'Securimage Test Contact Form';
10
11?>
12<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
13<html xmlns="http://www.w3.org/1999/xhtml">
14<head>
15 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
16 <title>Securimage Example Form</title>
17 <style type="text/css">
18 <!--
19 .error { color: #f00; font-weight: bold; font-size: 1.2em; }
20 .success { color: #00f; font-weight: bold; font-size: 1.2em; }
21 fieldset { width: 90%; }
22 legend { font-size: 24px; }
23 .note { font-size: 18px;
24 -->
25 </style>
26</head>
27<body>
28
29<fieldset>
30<legend>Example Form</legend>
31
32<p class="note">
33 This is an example PHP form that processes user information, checks for errors, and validates the captcha code.<br />
34 This example form also demonstrates how to submit a form to itself to display error messages.
35</p>
36
37<?php
38
39process_si_contact_form(); // Process the form, if it was submitted
40
41if (isset($_SESSION['ctform']['error']) && $_SESSION['ctform']['error'] == true): /* The last form submission had 1 or more errors */ ?>
42<span class="error">There was a problem with your submission. Errors are displayed below in red.</span><br /><br />
43<?php elseif (isset($_SESSION['ctform']['success']) && $_SESSION['ctform']['success'] == true): /* form was processed successfully */ ?>
44<span class="success">The captcha was correct and the message has been sent!</span><br /><br />
45<?php endif; ?>
46
47<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']) ?>" id="contact_form">
48 <input type="hidden" name="do" value="contact" />
49
50 <p>
51 <strong>Name*:</strong>&nbsp; &nbsp;<?php echo @$_SESSION['ctform']['name_error'] ?><br />
52 <input type="text" name="ct_name" size="35" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_name']) ?>" />
53 </p>
54
55 <p>
56 <strong>Email*:</strong>&nbsp; &nbsp;<?php echo @$_SESSION['ctform']['email_error'] ?><br />
57 <input type="text" name="ct_email" size="35" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_email']) ?>" />
58 </p>
59
60 <p>
61 <strong>URL:</strong>&nbsp; &nbsp;<?php echo @$_SESSION['ctform']['URL_error'] ?><br />
62 <input type="text" name="ct_URL" size="35" value="<?php echo htmlspecialchars(@$_SESSION['ctform']['ct_URL']) ?>" />
63 </p>
64
65 <p>
66 <strong>Message*:</strong>&nbsp; &nbsp;<?php echo @$_SESSION['ctform']['message_error'] ?><br />
67 <textarea name="ct_message" rows="12" cols="60"><?php echo htmlspecialchars(@$_SESSION['ctform']['ct_message']) ?></textarea>
68 </p>
69
70 <p>
71 <img id="siimage" style="border: 1px solid #000; margin-right: 15px" src="./securimage_show.php?sid=<?php echo md5(uniqid()) ?>" alt="CAPTCHA Image" align="left" />
72 <object type="application/x-shockwave-flash" data="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" height="32" width="32">
73 <param name="movie" value="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" />
74 </object>
75 &nbsp;
76 <a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false"><img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0" /></a><br />
77 <strong>Enter Code*:</strong><br />
78 <?php echo @$_SESSION['ctform']['captcha_error'] ?>
79 <input type="text" name="ct_captcha" size="12" maxlength="16" />
80 </p>
81
82 <p>
83 <br />
84 <input type="submit" value="Submit Message" />
85 </p>
86
87</form>
88</fieldset>
89
90</body>
91</html>
92
93<?php
94
95// The form processor PHP code
97{
98 $_SESSION['ctform'] = array(); // re-initialize the form session data
99
100 if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
101 // if the form has been submitted
102
103 foreach($_POST as $key => $value) {
104 if (!is_array($key)) {
105 // sanitize the input data
106 if ($key != 'ct_message') $value = strip_tags($value);
107 $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
108 }
109 }
110
111 $name = @$_POST['ct_name']; // name from the form
112 $email = @$_POST['ct_email']; // email from the form
113 $URL = @$_POST['ct_URL']; // url from the form
114 $message = @$_POST['ct_message']; // the message from the form
115 $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
116 $name = substr($name, 0, 64); // limit name to 64 characters
117
118 $errors = array(); // initialize empty error array
119
120 if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
121 // only check for errors if the form is not in debug mode
122
123 if (strlen($name) < 3) {
124 // name too short, add error
125 $errors['name_error'] = 'Your name is required';
126 }
127
128 if (strlen($email) == 0) {
129 // no email address given
130 $errors['email_error'] = 'Email address is required';
131 } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
132 // invalid email format
133 $errors['email_error'] = 'Email address entered is invalid';
134 }
135
136 if (strlen($message) < 20) {
137 // message length too short
138 $errors['message_error'] = 'Please enter a message';
139 }
140 }
141
142 // Only try to validate the captcha if the form has no errors
143 // This is especially important for ajax calls
144 if (sizeof($errors) == 0) {
145 require_once dirname(__FILE__) . '/securimage.php';
146 $securimage = new Securimage();
147
148 if ($securimage->check($captcha) == false) {
149 $errors['captcha_error'] = 'Incorrect security code entered<br />';
150 }
151 }
152
153 if (sizeof($errors) == 0) {
154 // no errors, send the form
155 $time = date('r');
156 $message = "A message was submitted from the contact form. The following information was provided.<br /><br />"
157 . "Name: $name<br />"
158 . "Email: $email<br />"
159 . "URL: $URL<br />"
160 . "Message:<br />"
161 . "<pre>$message</pre>"
162 . "<br /><br />IP Address: {$_SERVER['REMOTE_ADDR']}<br />"
163 . "Time: $time<br />"
164 . "Browser: {$_SERVER['HTTP_USER_AGENT']}<br />";
165
166 $message = wordwrap($message, 70);
167
168 if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
169 // send the message with mail()
170 mail($GLOBALS['ct_recipient'], $GLOBALS['ct_msg_subject'], $message, "From: {$GLOBALS['ct_recipient']}\r\nReply-To: {$email}\r\nContent-type: text/html; charset=ISO-8859-1\r\nMIME-Version: 1.0");
171 }
172
173 $_SESSION['ctform']['error'] = false; // no error with form
174 $_SESSION['ctform']['success'] = true; // message sent
175 } else {
176 // save the entries, this is to re-populate the form
177 $_SESSION['ctform']['ct_name'] = $name; // save name from the form submission
178 $_SESSION['ctform']['ct_email'] = $email; // save email
179 $_SESSION['ctform']['ct_URL'] = $URL; // save URL
180 $_SESSION['ctform']['ct_message'] = $message; // save message
181
182 foreach($errors as $key => $error) {
183 // set up error messages to display with each field
184 $_SESSION['ctform'][$key] = "<span style=\"font-weight: bold; color: #f00\">$error</span>";
185 }
186
187 $_SESSION['ctform']['error'] = true; // set error floag
188 }
189 } // POST
190}
191
192$_SESSION['ctform']['success'] = false; // clear success value after running
SMTP MX.
error($a_errmsg)
set error message @access public
$_POST['username']
Definition: cron.php:12
process_si_contact_form()
$GLOBALS['DEBUG_MODE']
Definition: example_form.php:4
< a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false">< img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0"/></a >< br/>< strong > Enter Code *if($_SERVER['REQUEST_METHOD']=='POST' &&@ $_POST['do']=='contact') $_SESSION['ctform']['success']
Project: Securimage: A PHP class for creating and managing form CAPTCHA images File: securimage....
$errors