We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Flashdata adding GET data to URL after redirect?

Development and Programming

Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

I’m writing a module for a (very custom) multi-step membership application. I’ve got template group “apply”, and then apply/step1, apply/step2, etc. In those templates are tags mapping directly to the core module’s functions. Well, I might as well post the code 😊

‘apply’ (the index template) looks like

{exp:apply:index}
    {if error}
        Sorry, you cannot access this application for the following reason:
        {error}
    {if:else}    
        You can continue the application.
    {/if}
{/exp:apply:index}

and the corresponding function in the core module:

function index() {
    $tagdata = $this->EE->TMPL->tagdata;
    $error = $this->EE->session->flashdata('error_message');
    
    $tags[] = array(
        'error'    => (isset($error) ? $error : '')
    );

    return $this->EE->TMPL->parse_variables($tagdata, $tags);
}

Then the template ‘apply/step1’ looks like

{exp:apply:step1}
    <!-- form stuff here -->
{/exp:apply:step1}

And in the core module:

function step1() 
{
    // eventually will do a lot more to make sure they can access step 1
    if ($this->EE->session->userdata['group_id'] != 5)  
    {
        $this->EE->session->set_flashdata('error_message', 'You must be logged in.');
        $this->EE->functions->redirect($this->EE->functions->create_url('apply'));
    }

    return '';
}

Basically, at each step I need to do a bunch of checks to verify that the user can access that step, and if not, throw them back to /apply with the error message. I thought flashdata would be the right tool for the job…

But whenever I do a redirect with flashdata, ‘&fld=y’ gets added to the URL, causing a 404. So instead of redirecting to /apply it goes to /apply&fld=y . Odd, since I thought flashdata was cookie-based?

EDIT: Build is 20091207

       
John Henry Donovan's avatar
John Henry Donovan
12,339 posts
16 years ago
John Henry Donovan's avatar John Henry Donovan

Brandon,

Is there a htaccess in play here at all? Are you removing the index.php?

       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

Hey John, no, this is a fresh, very stock install of 20091204 updated to 20091207. No .htaccess, no other modules installed. XAMPP 1.7.2a (PHP 5.3) on OSX 10.6. Thanks.

       
Adam Dorsey's avatar
Adam Dorsey
1,439 posts
16 years ago
Adam Dorsey's avatar Adam Dorsey

Hi Brandon- Can you try changing this line:

$this->EE->functions->redirect($this->EE->functions->create_url('apply'));

to:

$this->EE->functions->redirect($this->EE->functions->create_url('apply', true));

Notice the true boolean. This will set “trailing_slash” to true. This should produce a url like /apply/&fld=y which may solve the 404.

Here is the Docs page for the create_url() function.

Does this solve your issue?

       
Adam Dorsey's avatar
Adam Dorsey
1,439 posts
16 years ago
Adam Dorsey's avatar Adam Dorsey

Actually, it looks like trailing_slash is true by default. If “forcing” it doesn’t do the trick.

Maybe try, putting in the full template_group/template, like so:

$this->EE->functions->redirect($this->EE->functions->create_url('apply/index'));
       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

Adam, no go unfortunately. In my experiences with CodeIgniter, flash data didn’t affect the URI…

To simplify, I got rid of the conditional test for member group in step1(). So all step1() does is set flash data and then redirect. Here’s what I tried/found:

  1. Trailing slash on/off and with apply/index (no effect)
  2. Occurs whether or not user is logged in
  3. Setting the User Session Type to ‘Cookies and Session ID’ instead of just ‘Cookies’ (has no effect other then the session ID appearing in the URI along with the weird &fld=y
  4. Redirecting to something other than the template index, for example to apply/step2 (no change; still adds &fld=y)
       
Ingmar's avatar
Ingmar
29,245 posts
16 years ago
Ingmar's avatar Ingmar

Brandon, I am going to move this thread to the 2.0 Add-ons formum, where I think these discussions are more appropriate. Thanks.

       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

OK - is this forum still alive though? It’s not searchable via Advanced Search any longer. I thought this was for beta2, and I’m on the PB (non-commercial as this is for a 501c).

Something else I’ve just tried is using $this->EE->session->cache instead of flashdata. However the cache array doesn’t persist across page loads (whether it’s a redirect or not), which seems seriously wrong.

Is it possible that the cache and flashdata are only meant for use on the control panel side, and not the front-end? If so the manual is pretty misleading… If that is indeed the case, I’d like to know another way to pass data to a template following a redirect (other than using the DB, which seems a bit overkill).

Thanks Ingmar and everyone else for their assistance.

       
Ingmar's avatar
Ingmar
29,245 posts
16 years ago
Ingmar's avatar Ingmar
OK - is this forum still alive though? It’s not searchable via Advanced Search any longer.

My mistake, moved to regular “Modules”. Sorry for the confusion.

       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

Not to be a pain (too late, I know! :lol:) but the same exact thing happens using PHP in regular templates. Example:

template ‘apply/test1’:

<?php
$this->EE->session->set_flashdata('test_message', 'Flashdata test');
$this->EE->functions->redirect($this->EE->functions->create_url('apply/test2'));
?>

template ‘apply/test2’:

<?php
$test_message = $this->EE->session->flashdata('test_message');
echo $test_message;
?>

Viewing test1 redirects to:

http://test.local/index.php/apply/test2&fld=y

So this isn’t really module related… I guess I’d just like an ‘official’ answer on whether this is expected behavior in 2.0PB (1.x didn’t have flashdata since it wasn’t built on CI). Thanks!

       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

And here’s a similar test for the session cache:

template ‘test1’

<?php
$this->EE->session->cache['test']['message'] = 'hello';

// confirms values are set...
var_dump($this->EE->session->cache);
?>

template ‘test2’

<?php
// shows as empty!
var_dump($this->EE->session->cache);
?>

View test1 and then test2… the cache shows as empty. This should definitely work on the front end from what I can tell.

EDIT: Ah, re-re-reading the manual - session->cache is for “values that you would like to persist during a page load” (emphasis on during - won’t work between pages).

Then I fall back to the flashdata question, as to whether it is usable on the frontend? The user guide is ambiguous here…

       
Brandon Jones's avatar
Brandon Jones
5,500 posts
16 years ago
Brandon Jones's avatar Brandon Jones

Indeed, looks like this was in fact a bug.

😉

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.