Results 1 to 7 of 7

Thread: is generating a REQUEST_TOKEN with code possible?

  1. #1
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default is generating a REQUEST_TOKEN with code possible?

    Is it possible currently (2.10.3) to generate a new REQUEST_TOKEN via php?

    I have a problem thats a bit hard to explain, but in essence I have an 'Export CSV' button which calls a function that constructs a csv file and sends it to screen for download. After the button is pressed the download works fine - but I get the dreaded Token Error :twisted: :evil: :twisted: if any other buttons, filters etc are used on the same screen.

    To add to the complication, the entire screen (buttons,filters,record listing) exists because it is generated on a key (its not a Contao Table with its own DCA. I have another (normal) screen with a button to create a CSV, and that one works fine.

    So I'm wondering if/how I might regenerate the token by code in the called function after the button press.

    I realise the token system will be changed in Contao 2.11, but I'd really like to solve this now if possible - and I don't know how the token system works at all.

    Cheers, Murray

  2. #2
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default Re: is generating a REQUEST_TOKEN with code possible?

    Disable the token system temporarily until you can update is also an option

    I'm not sure about Contao standard methods of retrieving new tokens, but if you use Andreas Ajax extension then any call will return the result and a new token.

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: is generating a REQUEST_TOKEN with code possible?

    Thanks Ruud, but I'd rather not disable it - and I've never used Ajax. I'm hoping its possible with php somehow, as javascript makes me want to eat my shorts!

  4. #4
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: is generating a REQUEST_TOKEN with code possible?

    I had a look at the Ajax extension, and spent half yesterday printing $_POST and $_SESSION to screen to try and figure how it all works .... and I see Contao 11 beta is out today! I wasn't expecting it for a few months.

    So thats good, i'll be able to see if the change to the request token system fixes my problem. (which I'm sure it will, as its no longer a token per request).

  5. #5
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default Re: is generating a REQUEST_TOKEN with code possible?

    When a token is inserted into a page, php cannot change that (serverside). Older pages (accessed through back button) come from cache, so no php.

    The only way is a client side script alike javascript. It actually isn't extremely hard to do; look inside isotope.js for example. There you see how to do a request and how to access any FE module. Them write a module and ajax does the rest...

  6. #6
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: is generating a REQUEST_TOKEN with code possible?

    I didn't think to say Ruud, this module is entirely backend.

    As I thought 2.11 beta solves it for me.
    But I have managed to solve it with php for 2.10 as well.

    I'll describe it, as it may help others, but then again it may just be specific to my modules setup.


    Specifically for me it only affected three function calls from three buttons to a class that generates a pdf, a csv and a text file depending on what is passed. So all three returns print to screen. Any of these buttons works once, but immediately after that any other form submit buttons,filters,search etc give the token error (on the Overview screen only - which is entirely code generated from a key, not Contao generated) .

    The solution for me was to add this code before each function return outputs the download dialog box to screen.
    All i'm really doing is adding my forms POSTED request token into the SESSION array, as it was not being added.

    Effectively the var REQUEST_TOKEN (which is added to the session) is out of sync with $_POST['REQUEST_TOKEN'] returned by my forms for some reason.
    (Yes.. they all have the hidden Input REQUEST_TOKEN needed for 2.10)



    Code:
    $count = count($_SESSION['REQUEST_TOKEN']['BE']);
    
    if($count > 24)
    {
        array_shift($_SESSION['REQUEST_TOKEN']['BE']);
        $_SESSION['REQUEST_TOKEN']['BE'][24] = $_POST['REQUEST_TOKEN'];
    
    }else
    {
        $_SESSION['REQUEST_TOKEN']['BE'][$count] = $_POST['REQUEST_TOKEN'];
    }
    I notice the $_SESSION['REQUEST_TOKEN']['BE'] array limits itself to 25 tokens, so the if() is just to shift the first one off before adding mine to the end if it has reached that many tokens, to keep it at 25.

    I need to wrap that in something to say "Only if Contao is 2.10 series", but dev.contao and contao.org haven't been reachable for 24 hours for some reason. Wonder if Leo knows. I'm not sure who to inform.

  7. #7
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: is generating a REQUEST_TOKEN with code possible?

    Code:
    if(VERSION == 2.10)
    {
           $count = count($_SESSION['REQUEST_TOKEN']['BE']);
    
          if($count > 24)
          {
               array_shift($_SESSION['REQUEST_TOKEN']['BE']);
               $_SESSION['REQUEST_TOKEN']['BE'][24] = $_POST['REQUEST_TOKEN'];
    
          }else
          {
                  $_SESSION['REQUEST_TOKEN']['BE'][$count] = $_POST['REQUEST_TOKEN'];
          }
    }
    covers just Contao 2.10 series

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •