├── .gitmodules ├── update-submodules.sh ├── schema.sql ├── views ├── regenerate.php └── accept.php ├── README.md ├── index.php ├── lib └── save-signature.php └── documentation.md /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "signature-pad"] 2 | path = signature-pad 3 | url = git@github.com:thomasjbradley/signature-pad.git 4 | -------------------------------------------------------------------------------- /update-submodules.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Updates all the submodules 4 | git submodule foreach git pull origin master 5 | -------------------------------------------------------------------------------- /schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `signatures` ( 2 | `id` int(11) NOT NULL AUTO_INCREMENT, 3 | `signator` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 4 | `signature` text COLLATE utf8_unicode_ci NOT NULL, 5 | `sig_hash` varchar(128) COLLATE utf8_unicode_ci NOT NULL, 6 | `ip` varchar(46) COLLATE utf8_unicode_ci NOT NULL, 7 | `created` int(11) NOT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1; 10 | -------------------------------------------------------------------------------- /views/regenerate.php: -------------------------------------------------------------------------------- 1 | 5 |
6 |
7 |
8 | 9 |
10 |


11 |
12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Saving Signatures Using PHP & MySQL 2 | 3 | A sample application demonstrating how to save the output of Signature Pad into a MySQL database using PHP. 4 | 5 | --- 6 | 7 | ## Sample App Docs 8 | 9 | #### [☛ Documentation](documentation.md) 10 | 11 | --- 12 | 13 | ## Signature Pad 14 | 15 | - [Documentation](https://github.com/thomasjbradley/signature-pad/blob/gh-pages/documentation.md) 16 | - [Source](https://github.com/thomasjbradley/signature-pad) 17 | 18 | --- 19 | 20 | ## License 21 | 22 | This sample app is licensed under The Unlicense (aka: public domain). 23 | 24 | All dependencies: Signature Pad, jQuery, json2.js, and FlashCanvas retain their own licenses. 25 | -------------------------------------------------------------------------------- /views/accept.php: -------------------------------------------------------------------------------- 1 | 5 |
6 | 7 |

Please sign the document

8 | 9 | 10 |

Please enter your name

11 | 12 | 13 | 14 |

Draw your signature

15 | 19 |
20 |
21 | 22 | 23 |
24 | 25 |
26 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | Saving a Signature · Signature Pad 11 | 12 | 13 | 14 | 15 | 16 | 24 | 25 | 31 | 36 | 37 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /lib/save-signature.php: -------------------------------------------------------------------------------- 1 | exec('SET NAMES utf8'); 47 | 48 | // Create some other pieces of information about the user 49 | // to confirm the legitimacy of their signature 50 | $sig_hash = sha1($output); 51 | $created = time(); 52 | $ip = $_SERVER['REMOTE_ADDR']; 53 | 54 | // 5. Use PDO prepare to insert all the information into the database 55 | $sql = $db->prepare(' 56 | INSERT INTO signatures (signator, signature, sig_hash, ip, created) 57 | VALUES (:signator, :signature, :sig_hash, :ip, :created) 58 | '); 59 | $sql->bindValue(':signator', $name, PDO::PARAM_STR); 60 | $sql->bindValue(':signature', $output, PDO::PARAM_STR); 61 | $sql->bindValue(':sig_hash', $sig_hash, PDO::PARAM_STR); 62 | $sql->bindValue(':ip', $ip, PDO::PARAM_STR); 63 | $sql->bindValue(':created', $created, PDO::PARAM_INT); 64 | $sql->execute(); 65 | 66 | // 6. Trigger the display of the signature regeneration 67 | $show_form = false; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /documentation.md: -------------------------------------------------------------------------------- 1 | # Saving Signatures with PHP & MySQL 2 | 3 | **A small PHP tutorial on how to capture the signature from Signature Pad and store it in a MySQL database, for later retrieval.** 4 | 5 | **This tutorial depends on Signature Pad. If you don’t know what that is go [check out Signature Pad, for capturing electronic signatures](https://github.com/thomasjbradley/signature-pad).** 6 | 7 | --- 8 | 9 | *This tutorial is just a small overview of the important pieces of information from the downloadable sample application. [Check out the sample app itself](https://github.com/thomasjbradley/saving-signatures-sample/), there are lots of comments in the code to help you out.* 10 | 11 | ## Sample Application Files 12 | 13 | ``` 14 | saving-signatures-sample/ 15 | |- index.php 16 | |- lib/ 17 | | |- save-signature.php 18 | |- views/ 19 | | |- accept.php 20 | | |- regenerate.php 21 | |- signature-pad/ 22 | | |- … 23 | ``` 24 | 25 | - `index.php` contains the basic HTML template and a few `if-statements` to hide/show different pieces of HTML and Javascript. 26 | - `lib/save-signature.php` is the meat and potatoes, the controller: it gets the content from the form, validates it, and saves it to the database. 27 | - `views/accept.php` is the basic Signature Pad HTML template for [accepting a signature](https://github.com/thomasjbradley/signature-pad/blob/gh-pages/documentation.md#accepting-signatures), with the addition of some serverside validation code. 28 | - `views/regenerate.php` is the basic Signature Pad HTML template for [regenerating a signature](https://github.com/thomasjbradley/signature-pad/blob/gh-pages/documentation.md#regenerating-signatures), with the addition of a little PHP to output the signator and date. 29 | - `signature-pad` is just a clone of the complete Signature Pad Git repository. The sample application only uses files from the `build` sub-folder. 30 | 31 | ## Getting the Signature 32 | 33 | Signature Pad submits the signature, along with the rest of the form submission, inside a hidden input field. 34 | 35 | *From `views/accept.php`:* 36 | 37 | ```html 38 |
39 | ⋮ 40 | 41 | ⋮ 42 |
43 | ``` 44 | 45 | From this hidden field we can capture the signature and store it in the database. 46 | 47 | The easiest way to get the signature using PHP is with the `$_POST` super global. 48 | 49 | ```php 50 | 89 | 90 | 91 | Name 92 | Data-Type 93 | Length 94 | What’s it For? 95 | 96 | 97 | 98 | 99 | id 100 | int 101 | 11 102 | Primary key, auto increment 103 | 104 | 105 | signator 106 | varchar 107 | 255 108 | Holds the user input for the `name` field 109 | 110 | 111 | signature 112 | text 113 | 114 | Holds the signature in its JSON form 115 | 116 | 117 | sig_hash 118 | varchar 119 | 128 120 | Holds a SHA1 hash of the JSON signature 121 | 122 | 123 | ip 124 | varchar 125 | 46 126 | Holds the signator’s IP address 127 | 128 | 129 | created 130 | int 131 | 11 132 | The UNIX timestamp of when the signature was saved 133 | 134 | 135 | 136 | 137 | ## Saving to the Database 138 | 139 | After everything is validated, we can save the signature to the database. It’s easiest to just store the JSON representation of the signature in the database. If you want to create a graphic file, [check out how to convert the signature to an image](https://github.com/thomasjbradley/signature-to-image). 140 | 141 | It’s best to use PHP’s [PDO](http://php.net/pdo) for connecting to databases. By using `PDO::prepare()`, we can help protect against SQL injection attacks. 142 | 143 | *From `lib/save-signature.php`:* 144 | 145 | ```php 146 | exec('SET NAMES utf8'); 151 | 152 | // Create some other pieces of information about the user 153 | // to confirm the legitimacy of their signature 154 | $sig_hash = sha1($output); 155 | $created = time(); 156 | $ip = $_SERVER['REMOTE_ADDR']; 157 | 158 | // Use PDO prepare to insert all the information into the database 159 | $sql = $db->prepare(' 160 | INSERT INTO signatures (signator, signature, sig_hash, ip, created) 161 | VALUES (:signator, :signature, :sig_hash, :ip, :created) 162 | '); 163 | $sql->bindValue(':signator', $name, PDO::PARAM_STR); 164 | $sql->bindValue(':signature', $output, PDO::PARAM_STR); 165 | $sql->bindValue(':sig_hash', $sig_hash, PDO::PARAM_STR); 166 | $sql->bindValue(':ip', $ip, PDO::PARAM_STR); 167 | $sql->bindValue(':created', $created, PDO::PARAM_INT); 168 | $sql->execute(); 169 | ``` 170 | 171 | ## Regenerating the Signature 172 | 173 | One of the easiest ways to regenerate the signature is to use PHP to write out some Javascript onto one of your pages. When the page loads it will have a native Javascript varible containing all the signature information and can use Signature Pad to regenerate the display. 174 | 175 | ```php 176 | 183 | ``` 184 | 185 | ## What Else Do We Need? 186 | 187 | Well, this application is far from complete—even though it’s fully functional. It’s a simple tutorial on how to capture the signature. Some of the missing things are: 188 | 189 | 1. User authentication, sign-in/sign-out, or unique passkeys. 190 | 2. HTTPS—critical for capturing signatures online. 191 | 3. It might be nice to encrypt the signatures in the database. 192 | 193 | And likely more. 194 | --------------------------------------------------------------------------------