• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to avoid $_POST resending data?

After being web programmer for sometime found some problem on inserting data to database especially using $_POST...if we accidentally pressing F5 data will be resend...

I found a tutorial here http://www.ajaxray.com/blog/2008/01/12/h...d-warning/

But I am not sure it is the only way by storing $_POST data into session...

Any other solutions?

In case you want to show the user some details he just submitted
in a form, using sessions (native or CI-session) would be nessesary
or recommended.

You could add the db insert-id info as an url segment and start a query at
the redirect-page but that would open your database as you could
just edit the url segment containing the id and get infos about all your
other db entries!

You also could build the redirect url depending on the submitted data
but that could become "fiddly" (don't know if you can use this
word - I just googled to find a similar world I would use in my
language). If it's just a small set of data it would be ok this way.

But I would prefer using sessions.

I have implement session already in the situation...

Here is my steps
1. The Post data I copied it into session
2. Displaying the session data into a redirected page
3. Insert session data into DB
4. Unset/Clear the session...

btw I google for fiddly -> requiring close attention to detail : fussy ; especially : requiring an annoying amount of close attention


[eluser]Michael Wales[/eluser]
Eliminating refresh reposting is very easy in CodeIgniter:

if ($this->validation->run()) {
  // Our code validatates
  // Add to database


Since you are redirecting after the submission of data, it's impossible for the refresh to resubmit.

Similiar to what i did...

but my case is for temporary data,,,
After the user confirm, the data will be sent to database,,,so I will have to use session...

I think redirecting is good especially for upload file case.

But I don't know how to write the upload data into session because i have multiple upload field

$this->upload->data() is multi dimension array...

Here is my code i wrote in the helper


function generate_code($length = 10){

    if ($length <= 0)
           return false;
    $code = "";
    $chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
    srand((double)microtime() * 1000000);
    for ($i = 0; $i < $length; $i++)
        $code = $code . substr($chars, rand() % strlen($chars), 1);
    return $code;


function generatePhotoThumbnail($source, $newimage)
    $CI = & get_instance();
            $config['image_library'] = 'GD2';
            $config['source_image'] = $source;
            $config['create_thumb'] = TRUE;
            $config['thumb_marker'] = '_thumb';
            $config['master_dim'] = 'width';
            $config['quality'] = 75;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 175;
            $config['height'] = 175;
            $config['new_image'] = './assets/public/placesPhotos/thumb/'.$newimage;

            //$this->load->library('image_lib', $config);

function processPhoto($uploaddata, $reviewID)
        $CI = & get_instance();
        $time = ($gmt)  ? gmstrftime("%Y-%m-%d %H:%M:%S", time()) : strftime("%Y-%m-%d %H:%M:%S", time());
        //Move Files To User Folder
        foreach($uploaddata as $key => $value)
            //Gen Random code for new file name
            $randomcode = generate_code(12);
            $newimagename = $randomcode.$value['file_ext'];
            //Make Some Variables for Database
            $imagename = $newimagename;
            $thumbnail = $randomcode.'_thumb'.$value['file_ext'];
            $photoInfo = array(
                'place_id' => $reviewID,
                'imagename' => $imagename,
                'thumbnail' => $thumbnail,
                'title' => $value['raw_name'],
                'width' => $value['image_width'],
                'height' => $value['image_height'],
                'filesize' => $value['file_size'],
                'description' => '',
                'timestamp' => $time,
                'up_status' => 1
      /*      //Add Pic Info To Database
            $this->db->set('tour_id', $picInfo['tour_id']);
            $this->db->set('imagename', $imagename);
            $this->db->set('thumbnail', $thumbnail);
            $this->db->set('filesize', $filesize);
            $this->db->set('width', $width);
            $this->db->set('height', $height);
            $this->db->set('up_status', '1');
            $this->db->set('timestamp', $picInfo['timestamp']);
            //Insert Info Into Database
            return $this->db->insert('pictures');


and this is my controller

    $config['upload_path'] = './img/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '2048'; //2 meg
    $config['max_width']  = '1024';
    $config['max_height']  = '1024';
        foreach($_FILES as $key => $value)
         if( ! empty($value['name']))
            $data['error_upload'][] = $this->upload->display_errors();
            $sessUploadError = $this->upload->display_errors();
            //$errors = TRUE;
            // Build a file array from all uploaded files
             $sessUploadData = $this->upload->data();                                  
             processPhoto(array($this->upload->data()), $placeID);

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.