ttskch/contact-form
用于实现通用联系表单的PHP实用工具类
2.0.1
2019-05-29 12:19 UTC
Requires
- php: >=5.4.0
- swiftmailer/swiftmailer: >=5.4.12
Requires (Dev)
- phpunit/phpunit: >=4.8,<7.0
README
PHP实用工具类,用于实现通用联系表单(也包含确认视图)。这对于在WordPress或纯PHP网站上构建联系表单可能很有用。
要求
- PHP 5.4+
- 在php.ini中配置
date.timezone
支持的功能
- 使用会话和隐藏输入标签进行Csrf保护
- 对提交的值进行服务器端验证
- 简写以打印提交的值本身、验证错误、"选中"选项、"勾选"选项
- 在同一会话中附加文件并轻松处理它们
- 轻松发送包含提交信息的电子邮件
安装
$ composer require ttskch/contact-form
或
$ git clone git@github.com:ttskch/contact-form.git $ cd contact-form $ composer install --no-dev # If your web site is not composer-friendly, upload whole "contact-form" directory by hand.
用法
<?php // index.php require_once '/path/to/contact-form/vendor/autoload.php'; $cf = new \Ttskch\ContactForm\ContactForm(); // validation targets ("required" and "email" are supported) $requiredKeys = ['Name', 'Email']; $emailKeys = ['Email']; // after posted, validate csrf and submissions and redirect to next page $cf->validateAndRedirectAfterSelfPosted('./confirm.php', $requiredKeys, $emailKeys); ?> <!-- post to self --> <form action="" method="post"> <!-- put hidden input tag for csrf token --> <?= $cf->csrfHiddenInput(); ?> <!-- text field with submitted value if exists --> <input type="text" name="Name" value="<?= $cf->present('Name'); ?>" required autofocus> <!-- show error if exists --> <p><?= $cf->presentError('Name'); ?></p> <!-- text field with submitted value if exists --> <input type="email" name="Email" value="<?= $cf->present('Email'); ?>" required> <!-- show error if exists --> <p><?= $cf->presentError('Email'); ?></p> <!-- selector field with selection submitted or default option --> <select class="form-control" name="Gender"> <option value="Male" <?= $cf->presentSelected('Gender', 'Male', $default = true); ?>>Male</option> <option value="Female" <?= $cf->presentSelected('Gender', 'Female'); ?>>Female</option> <option value="Other" <?= $cf->presentSelected('Gender', 'Other'); ?>>Other</option> </select> <button type="submit">Confirm</button> </form>
<?php // confirm.php require_once '/path/to/contact-form/vendor/autoload.php'; $cf = new \Ttskch\ContactForm\ContactForm(); // redirect to top page if requested without submissions $cf->rejectAccessWithoutSubmissions('./index.php'); // after posted, validate csrf and redirect to next page $cf->validateAndRedirectAfterSelfPosted('./thanks.php'); ?> <!-- post to self --> <form action="" method="post"> <!-- put hidden input tag for csrf token --> <?= $cf->csrfHiddenInput(); ?> <!-- show submitted values --> <p><?= $cf->present('Name'); ?></p> <p><?= $cf->present('Email'); ?></p> <p><?= $cf->present('Gender'); ?></p> <button type="submit">Send</button> <!-- can back to index.php and re-edit inputs --> <a href="javascript:history.back();">Back</a> </form>
<?php // thanks.php require_once '/path/to/contact-form/vendor/autoload.php'; $cf = new \Ttskch\ContactForm\ContactForm(); // redirect to top page if requested without submissions $cf->rejectAccessWithoutSubmissions('./index.php'); $template = <<<EOT ---------------------------------------------------------------------- Name: %s ---------------------------------------------------------------------- Email: %s ---------------------------------------------------------------------- Gender: %s ---------------------------------------------------------------------- EOT; $body = vsprintf($template, [ $cf->present('Name', false), $cf->present('Email', false), $cf->present('Gender', false), ]); $cf->sendEmail( 'you@email.com', // to 'from@email.com', // from 'Your Name', // from name 'Got inquiry', // subject $body // body ); // clear submissions after sending email // by this, if users reload thanks.php after sending email they will be redirected to index.php $cf->clearSubmissions(); ?> <p>Form is successfully submitted!</p>
查看演示代码或在其本地运行以了解更多信息:)
注意事项
由于此实用工具调用header()函数进行重定向,因此必须在发送任何实际输出之前执行实例化和一些方法。