Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To my understanding, you would like to store user submitted text in a database, and then later display it on a page -- kind of like a basic commenting system or something. You just don't want any naughty/incomplete HTML characters breaking your page when outputting it.</p> <p>Whenever you have user submitted data, you want to utilize the form_validation library to clean it up and sanitize it as much as possible as a good security measure. If it goes to your database, you should use Active Records or Query Binding to get additional security from Codeigniter, such as escaping the strings, etc.</p> <p>Let me show my solution on submitting and outputting user's input on a website. There are probably better ways to do this, but this will get the job done.</p> <pre><code>&lt;?php /*Controller **************************************************/ class Something extends CI_Controller { function comments_or_whatever() { //Required -&gt; trim value -&gt; max_length of 100 -&gt; strip HTML tags -&gt; remove additional HTML entities missed by strip tags $this-&gt;form_validation-&gt;set_rules('input_1', 'The First User Input', 'required|trim|max_length[100]|xss_clean|strip_tags|callback__remove_html_entities'); $this-&gt;form_validation-&gt;set_rules('input_2', 'The Second User Input', 'trim|exact_length[11]|xss_clean|strip_tags|callback__remove_html_entities'); if ($this-&gt;form_validation-&gt;run() == FALSE) { //form didn't validate.. try again display error messages $this-&gt;load-&gt;view('your_view'); } } else { $input_1 = $this-&gt;input-&gt;post('input_1'); $input_2 = $this-&gt;input-&gt;post('input_2'); $submission_array = array( 'db_field_1' =&gt; $input_1, 'db_field_2' =&gt; $input_2 ); $this-&gt;load-&gt;model('comments'); $result = $this-&gt;comments-&gt;submit_comments_or_whatever($submission_array); if ($result['is_true'] == TRUE) { //creates a temporary flash message and redirects to current page //if on a windows server use 'refresh' instead of 'location' $this-&gt;session-&gt;set_flashdata('message', '&lt;div class="message"&gt;'.$result['message'].'&lt;/div&gt;'); redirect('something', 'location'); } else { $data['message'] = $result['message']; $this-&gt;load-&gt;view('your_view', $data); } } } // Very important to get rid calling HTML Entities via HTML number codes such as &amp;#60 etc. Strip_tags does not do this. // This is privately called during validation from the callback__remove_html_entities custom callback function _remove_html_entities($submission) { $submission = preg_replace("/&amp;#?[a-z0-9]{2,8};/i","",$submission); return $submission; } } /* Model ****************************************/ class Comments extends CI_Model { function submit_comments_or_whatever($submission_array) { // Active record escapes string and does additional security $query = $this-&gt;db-&gt;insert('comments', $submission_array); if ($query == TRUE) { $data['is_true'] = TRUE; $data['message'] = 'Your message has been successfully shared!'; return $data; } else { $data['is_true'] = FALSE; $data['message'] = 'Sorry, but there was an error dude inserting your message into the database.'; return $data; } } } /* View -&gt; your_view.php ****************************************/ &lt;?php echo validation_errors('&lt;div class="message"&gt;', '&lt;/div&gt;'); ?&gt; &lt;?php echo $this-&gt;session-&gt;flashdata('message'); ?&gt; &lt;?php if (!empty($message)) echo '&lt;div class="message"&gt;'.$message.'&lt;/div&gt;'; ?&gt; &lt;?php echo form_open('something/comments_or_whatever'); ?&gt; &lt;?php echo form_label('The First User Input', 'input_1'); ?&gt;&lt;br&gt; &lt;?php $input_1_form = array('name' =&gt; 'input_1', 'id' =&gt; 'input_1', 'value' =&gt; set_value('input_1')); ?&gt; &lt;?php echo form_input($input_1_form); ?&gt;&lt;br&gt; &lt;?php echo form_label('The Second User Input', 'input_2'); ?&gt;&lt;br&gt; &lt;?php $input_2_form = array('name' =&gt; 'input_2', 'id' =&gt; 'input_2', 'value' =&gt; set_value('input_2')); ?&gt; &lt;?php echo form_input($input_2_form); ?&gt;&lt;br&gt; &lt;?php echo form_submit('submit', 'Dude, submit my user inputed text!'); ?&gt; &lt;?php echo form_close(); ?&gt; </code></pre> <p>This code assumes you autoload the Form Validation, Sessions, and Database Libraries and the Form Helper. Now, all your user inputed data is stripped to a bare minimum of plain text using a custom Regular Expression call back during form validation. All naughty HTML characters are gone/sanitized, completely. You can now be worry-free to output the submitted data anywhere you'd like on a webpage without it breaking or being a security concern.</p> <p>The problem with just doing HTMLSpecialChars() and html decode is it doesn't account for incomplete HTML tags. Hopefully this helps, best of luck dude, and as always, nothing is ever completely secure. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload