Module

Archived
Forum
(read-only)

Google Custom Search

ExpressionEngine 2, ExpressionEngine 3, ExpressionEngine 4

Back to this add-on's main page
View Other Add-ons From Derek Hogue

     

Error: This form has expired.  Please refresh and try again.

Support (Resolved)

vig1
vig1

Hi Derek,

We are occasionally getting this error as some uses submit a search:

The following errors were encountered
This form has expired. Please refresh and try again.
Return to Previous Page

We are currently using version 1.1.4 of Google Custom Search with version 2.8.0 of Expression Engine.

Our code to call upon Google Custom Search is as follows:

{exp:google_custom_search:form results_page="search/results" id="search-form" class="form-search" honeypot="y" snaptcha="n"}
            
<div class="input-append">
                <
input type="text" name="keywords" id="keywords" placeholder="Search" required class="search-query"><!-- Fix Gap
--><button type="submit" value="submit" class="btn">Search</button>
            </
div>
{/exp:google_custom_search:form} 

On a separate results template file we have:

{exp:google_custom_search:form results_page="search/results" id="search-form" class="form-search" honeypot="y"}
 
<div class="input-append">
  <
input type="text" name="keywords" id="keywords" placeholder="Search" value="" class="span6 search-query">
  <
button type="submit" value="submit" class="btn">Search</button>
 </
div>
{/exp:google_custom_search:form} 

And then the actual results code, located on the same page as above:

{exp:google_custom_search:results cse="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" keywords="{segment_3}"}
 {if no_search_results}
<p>Sorryyour search returned no results.</p>{/if}
 {if search_results}
  
<h2>{total_search_results} result{if total_search_results != 1}s{/if} for &ldquo;{keywords}&rdquo;</h2>
  <
hr class="dotted">
  <
ul class="unstyled">
   
{results}
   
<li class="search-result-item {format}">
    <
article>
     <
h3><a href="{url}">{title}</a></h3
     <
small>{url}</small>
     <
p>{excerpt}</p>
    </
article>
   </
li>
   
{/results}
  
</ul>
  
{if pagination}
  
<hr class="dotted">
  <
section class="pagination center">
   <
ul>
    
{if previous_page}
                                       
<li><a href="{previous_page}">Previous Page</a></li>
                                
{/if}
     
<li class="disabled"><a href="#">Page {current_page} of {total_pages}</a></li>
    
{if next_page}
                                        
<li><a href="{next_page}">Next Page</a></li>
                                
{/if}
   
</ul>
   <
p>Showing results{current_results_start} {current_results_end} of {total_search_results}</p>
  </
section>
  
{/if}
 {
/if}
{
/exp:google_custom_search:results} 

This error only shows up every once in a while, but it has happened enough that many users have reported it, and others have said that they just don’t use our search anymore and consider it broken.

I will continue investigating and report back with any updates here.  My first guess is that this has something to do with caching and the csrf_token being invalid.  Another possibility is the Stash plugin somehow, maybe, stashing the token and causing an error?  The problem with that theory is that this error isn’t consistent, so it cant be that simple.  I will also update to the latest version of 1.1.5 to see if that has any effect too.

Please let me know if you have any suggestions. 

Thank you!

Derek Hogue
# 1
Developer
Derek Hogue

Yup, this is definitely an issue with an expired or invalid csrf_token, which my add-on doesn’t control in any way (EE generates and validates it with the form creation and submission). EE 2.9 improved the handling of these, so that would be my first suggestion is to upgrade EE. And yes, also making sure that the form tag is created outside of any caching mechanism, as you don’t want that token cached ever.

(The caching issue does make sense for an intermittent problem, as many users could be the ones whose visit create the cache, so for them the form would still work.)

vig1
# 2
vig1

Thank you for the fast reply.  I will be trying various methods to fix out the cache before doing a whole EE system upgrade, but an EE upgrade will be an option by the end too (it will be for the best to upgrade, I know, it’s just convincing my co-workers heh).

Again, thank you for taking the time, and the sheer quickness of the helpful reply.  Great support!