tags:

views:

140

answers:

5

I have a cakephp application where there is a form that is visible to visitors who haven't even logged in. If they submit the form without logging in, they are redirected to a login page from where i want them to go back to the add controller with all the submitted form data. Is this possible? If yes, how?

+1  A: 

I think you will have to use the SESSION or do something like to this:

<input type="text" name="user" value="<?php echo $_POST['user'];?>">
<input type="password" name="password" value="<?php echo $_POST['password'];?>">

Note that above i have used the POST array but you can also use Cake's array for this if there is one.

Sarfraz
+1  A: 

Assign a session to the users before logging in, and store the data in the session. That session might have an attribute loggedIn which is default false.

Don't store the session data in a cookie though, keep it server side :)

extraneon
A: 

Put session_start(); on the top of the form, and login page. On the form's action page set session variables:

$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
etc...

On the form page, set values by saying the following:

<input type="text" name="name" id="name" value="<?php echo $_SESSION['name']; ?>" />
<input type="text" name="address" id="address" value="<?php echo $_SESSION['address']; ?>" />
etc...
Josh K
this is the a general PHP way of handling sessions, is there a way to do it with cakePHP. It has a lot of helpers but I don't know what to use and how.
Mayank
I'd probably start here. http://book.cakephp.org/view/322/The-Cake-Session-Component
Josh K
+3  A: 

Off the top of my head, something like this should work:

function beforeFilter() {
    // be sure to do this before any Auth or security checks
    if ($this->RequestHandler->isPost() && $this->data) {
        $this->Session->write('last_post_data', $this->data);
    }
}

function add() {
    if (!$this->data && $this->Session->check('last_post_data')) {
        $this->data = $this->Session->read('last_post_data');
    }
    $this->Session->delete('last_post_data');

    if ($this->data) {
        // save as usual
    }
}

Just make sure to properly dispose of the POST data saved in the Session, or it could wreck havoc later on. In fact, you should not only save the data in the Session, but also which action it was intended for ($this->action and $this->controller) and check for that before reusing the data in the action. Possibly also put a very tight timeout on the data.

deceze
This would be the same thing that I'd do.
jpdelatorre
A: 

If you only allow logged in users to access the form, you could also add a periodic AJAX request to the page with the form that keeps the session alive.

window.setInterval(function(){
    jQuery.get("/url/to_page_that_does_nothing");
}, 600000);

You could also set it up so that the timer is acticated only when the user starts filling out the form.

Disclaimers: 1) this uses jQuery 2) not as secure as logging in again, but faster/easier to implement.

Joel L