Welcome Guest, Not a member yet? Register   Sign In
How to prevent duplicate form submit with browser refresh
#11

[eluser]Thorpe Obazee[/eluser]
I was refering to the 'without the forwarding to another URL' option.
#12

[eluser]Jondolar[/eluser]
Then a cookie, session or db search would have to be used.
#13

[eluser]Zeeshan Rasool[/eluser]
It is a common problem which have to face by developers. Basically, the reason is that when our function performs any action then if it still using the executed function name in the url then presing F5 or refreshing the page will submit the page again and again so I think if you redirect the page instead of loading the page, then this can be resolved.

i-e

After inserting data Use..

redirect('picks', 'refresh');

instead of

$this->load->view('picks');
#14

[eluser]Tim Stackhouse[/eluser]
[quote author="zEsHaN" date="1239873149"]...

After inserting data Use..

redirect('picks', 'refresh');

instead of

$this->load->view('picks');[/quote]

That's what I'm doing, however I'm not doing a meta refresh, but using a header redirect. I've done this before with PHP:

Code:
function _create($data) {
        //user exists?
    if($user = $this->db->_one($data['login'], "login")) {
            //password OK?
            if($user['password'] === md5($data['password'])) {
                //set the session to the salted password hash.
                $_SESSION['logged_in'] = $user['login'];
                $_SESSION['name'] = $user['name'];
                $salted_string = $user['password'] . SESSION_SALT;
                $_SESSION['hash'] = md5($salted_string);
                set_flash("notice", "You have successfully logged in.");
                header("Location: /admin/");
            }
            else {
                set_flash("notice", "Your username or password is incorrect, please try again.");
                header("Location: /admin/login/");
            }
        }
        else {
            set_flash("notice", "Your username or password is incorrect, please try again");
            header("Location: /admin/login/");
        }
  }

This correctly accepts POST data, and performs a header redirect that results in a GET request to the redirected URL.

I'm trying to get CI to do the same thing with this code:

Code:
function create() {
      $email = $this->input->post('email');
      $password = $this->input->post('password');
      
      if ($session_data = $this->User->login($email, $password)) {
        //success
        $this->session->set_userdata('login_session',
                                     $session_data->session_id);
        redirect(site_url(''));
      }
      else {
        //failure
        $this->session->set_flashdata('message', 'Your login is invalid.  Please try again.');
      redirect(site_url("sessions/login"));
      }
    }

This results... In the correct behavior that I wanted to begin with... I just retested it and it wasn't doing this last night. At any rate, this may be something useful for people to reference.
#15

[eluser]Zeeshan Rasool[/eluser]
Code:
function create() {
      $email = $this->input->post('email');
      $password = $this->input->post('password');
      
      if ($session_data = $this->User->login($email, $password)) {
        //success
        $this->session->set_userdata('login_session',
                                     $session_data->session_id);
       // redirect(site_url('')); why you are using like this?
       redirect('picks/function name','refresh');  USE THIS
      }
      else {
        //failure
        $this->session->set_flashdata('message', 'Your login is invalid.  Please try again.');
     // redirect(site_url("sessions/login"));
      redirect('picks/sessions/login','refresh'); // USE THIS
      }
    }
#16

[eluser]Zeeshan Rasool[/eluser]
Code:
function create() {
      $email = $this->input->post('email');
      $password = $this->input->post('password');
      
      if ($session_data = $this->User->login($email, $password)) {
        //success
        $this->session->set_userdata('login_session',
                                     $session_data->session_id);
       // redirect(site_url('')); why you are using like this?
       redirect('picks/function name','refresh');  USE THIS
      }
      else {
        //failure
        $this->session->set_flashdata('message', 'Your login is invalid.  Please try again.');
      redirect(site_url("sessions/login"));
      redirect('picks/sessions/login','refresh'); // USE THIS
      }
    }
#17

[eluser]Tim Stackhouse[/eluser]
Doing it the way I'm doing it follows proper REST form, not to mention that refresh redirects outside of the windows environment as a last resort are horribly bad form, IMO. I've also made some tweaks as noted below, since redirect() automatically parses stuff out via site_url()

[quote author="zEsHaN" date="1240749588"]
Code:
function create() {
      $email = $this->input->post('email');
      $password = $this->input->post('password');
      
      if ($session_data = $this->User->login($email, $password)) {
        //success
        $this->session->set_userdata('login_session',
                                     $session_data->session_id);
       // redirect(site_url('')); why you are using like this?
       // redirect('picks/function name','refresh');  USE THIS
       redirect(''); //send the user to the default page
      }
      else {
        //failure
        $this->session->set_flashdata('message', 'Your login is invalid.  Please try again.');
      // redirect(site_url("sessions/login"));
      // redirect('picks/sessions/login','refresh'); // USE THIS
      redirect('login');  //back to the login page (redirected in routes.php to 'login_sessions/add')
      }
    }
[/quote]
#18

[eluser]Tim Stackhouse[/eluser]
Not to mention that header redirects do not break the functionality of the Back button like meta redirects do in most browsers.
#19

[eluser]Dinesh Viswanath[/eluser]
Earlier I have posted a thread for Redirection problem in the forum. They have guided me like this.
Instead of
Code:
self::index();
use following code to redirect.
Code:
redirect('/index.php/newscontroller/','refresh');
It worked for sometime without problem.now , it's showing the subsequent error message.
Code:
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at D:\www\system\application\controllers\newscontroller.php:1)
Filename: helpers/url_helper.php
Line Number: 541

Help me out.
Thanks in advance.
#20

[eluser]Thorpe Obazee[/eluser]
Your text editor could be putting in invisible characters in the document. I once had that issue before.




Theme © iAndrew 2016 - Forum software by © MyBB