5

EDIT: I think I've worked something out. - This character - - seems to be breaking it. If I use a 'regular' apostrophe (') it appears to work fine, if I replace every snazzy apostrophe in it with a regular one, it submits just fine. So, next question: why does that fancy apostrophe break CodeIgniter or PHP? I will presume this is a CI bug, but please correct me if I'm wrong. Anyway, here's the question I originally posted, I would still appreciate some insight into why this is happening and if there's much I can do about it:


I'm tearing my hair out over this one. This is completely bizarre and I simply cannot work it out.

Firstly, I have a basic form:

<?php $attributes = array('class' => 'form1'); $selected="";
echo form_open('admin/edit_page/'.$page->row('id'), $attributes); ?>

    <div>               
        <?php echo form_error('title'); ?>
        <label>Page Title:</label><input type="text" maxlength="100" name="title" value="<?php echo $page->row('title');?>" />
    </div>

    <div class="textarea">
        <?php echo form_error('content'); ?>
        <label>Page Content:</label><textarea name="content"><?php echo $page->row('content');?></textarea>
    </div>

    <div class="no">
        <input type="submit" value="Submit"/>
    </div>

</form>

Next, I have the corresponding CI function:

function edit_page($id)
{
    $this->form_validation->set_rules('title', 'Title', 'trim|required|max_length[50]');
    $this->form_validation->set_rules('content', 'Content', 'trim|required');

    // If validation has failed...
    if ($this->form_validation->run() == FALSE)
    {
        $data['page'] = $this->gallery_model->get_page($id);

        $this->load->view('admin/header');
        $this->load->view('admin/editpage', $data);
        $this->load->view('admin/footer');
    }
    else    // Validation successful
    {
        $title = $this->input->post('title');
        $content = $this->input->post('content');
        $this->gallery_model->edit_page($id, $title, $content);
        redirect('admin');
    }

}

All OK so far, yes? Here's where the crazy problem begins.

  • I can 'manually' type in any data I like into the Content field. I click Submit, all works as expected - the database is updated with my new data, all is good in the world.
  • I can copy and paste 15 paragraphs of Lorem Ipsum or whatever into the field just fine. Hit submit, hooray.
  • However: - I am trying to copy-paste the content from the client's site, specifically this page: http://jeremywebbphotography.com/biog.php - I am updating his site since my web dev skills have improved much since then. Here's where things go weird - upon hitting save after copy-pasting that, the form reloads, I get the error "The Content field is required", and my copy-paste into the field disappears.
  • If I run WebKit Inspector on Network mode and capture the form submission request, I can clearly see my copy-pasted text in the $_POST array...
  • However, if I begin my edit_page function with die("Content is ".$_POST['content']); - IT HAS CLEARED ITSELF. The output literally says "Content is ". That's all.
  • The above works as expected with the title field - it only affects content.
  • I fathomed there might be some funny special characters or something causing this to happen in the copied contents (even though there isn't) - but if you copy-paste it, then delete everything up until the first word 'Biog', then submit (so you are literally submitting one line), the same thing still happens!.

WTF is going on?!

Things I have confirmed it is not:

  • My browser (latest Chrome Dev on Mac OS X Snow Leopard - tried in Safari and Firefox, same problem)
  • CI XSS Filtering or Security Sanitising - turned these all off, no luck
  • CI Form Validation. I've turned it off as well.
  • The data I'm submitting (kind of) - see final descriptive bullet point.
  • Also tried running htmlspecialchars and htmlentities via the Form Validation rules, no cigar.

Thanks for your help. To clarify, here's a screenshot of how I'm copy-pasting it:

How I'm copy-pasting

(Nothing complex there, right?) - and finally, a quick screenshot of the actual page output after submission (sorry about the CSS wonks, I'm working on it):

How it looks

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jack
  • 9,615
  • 18
  • 72
  • 112
  • What's the value of `post_max_size` and `suhosin.post.max_value_length` (if suhosin is installed)? – netcoder Jun 18 '11 at 14:16
  • Just updated my question as of about 10 seconds ago - I think I worked out what it is. Suhosin is not installed (this is just MAMP for now), don't think `post_max_size` is relevant though as I submitted much more data (15 paras) of Lipsum just fine. – Jack Jun 18 '11 at 14:18
  • What's the charset that your form page uses? – netcoder Jun 18 '11 at 14:20
  • CI outputs `
    ` - I'm not too sussed on character encoding. Does that look right?
    – Jack Jun 18 '11 at 14:39
  • Oh, and the file itself is UTF-8 too. – Jack Jun 18 '11 at 14:53
  • Looks OK. Is the page (i.e.: `` tag) in UTF-8 too? Definitely a charset issue in my opinion. The `’` character is a UTF-8 character, so something is definitely having difficulties with it. – netcoder Jun 18 '11 at 14:58
  • 1
    Found this: [$_POST empty on utf-8 characters](http://stackoverflow.com/questions/5784207/post-empty-on-utf-8-characters). – netcoder Jun 18 '11 at 15:00

2 Answers2

2

Not sure if this would help at all, but try using $this->input->post('postvar') instead of $_POST['postvar']. I believe CodeIgniter uses this function to properly sanitize and encode post data.

Jamie
  • 1,607
  • 3
  • 17
  • 25
1

I suggest you use the Text Helper library and use its ascii_to_entities function:

$this->load->helper('text');
$content = $this->input->post('content');
$content = ascii_to_entities($content);


ascii_to_entities():

Converts ASCII values to character entities, including high ASCII and MS Word characters that can cause problems when used in a web page, so that they can be shown consistently regardless of browser settings or stored reliably in a database. There is some dependence on your server's supported character sets, so it may not be 100% reliable in all cases, but for the most part it should correctly identify characters outside the normal range (like accented characters).

stealthyninja
  • 10,343
  • 11
  • 51
  • 59