Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:55:01+00:00 2026-05-14T02:55:01+00:00

I need to run a PHP code snippet in a Drupal template and prevent

  • 0

I need to run a PHP code snippet in a Drupal template and prevent it from being cached.
The PHP snippet sniffs for a cookie and if found, returns a message based on the cookie value.

ie:

if(isset($_GET['key'])) { 
    $cookievalue = $_GET['key']; 
}

if(isset($_COOKIE['cookname'])) { 
    $cookievalue = $_COOKIE['cookname']; 
}

switch ($cookievalue) { 
    case hmm01: echo "abc"; 
    break; 
    case hmm02: echo "def";
    break; 
    case hmm03: echo "ghi"; 
    break; 
    default: echo "hello"; 
}

Right now, Drupal renders the messages randomly according to when the page was first cached and based on the cookie used at the time.

I can’t see a great deal of info on how I might go about this – it seems that you have to turn the cache off for the page rather than run php code uncached.

Can the community guide me on how to use the correct technique with regards to Drupal templating and PHP based on the information I posted or direct me to a guide or tutorial that may be useful for my situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:55:02+00:00Added an answer on May 14, 2026 at 2:55 am

    Your problem is that Drupal caches pages which is a good thing since you would probably get a huge performance hit otherwise.

    A solution, though not so pretty is to let a js post the data with Ajax. Since Drupal don’t cache post requests this could be a solution. It is however js dependant and makes an extra request which requires extra system resources. But atleast it is better than disBling the page cache.

    Edit:

    After giving it some thought, I think a better solution would be to create a custom block. Since Drupal blocks has their own caching system you should be able to get the functionality you want. Custom blocks is created with the use of hook_block

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to run some PHP code from a file when I click a
I want to run php or python scripts from eclipse that will create folders
What I need to have happen: PHP needs to launch an server app which
I've got an Abstract PHP superclass, which contains code that needs to know which
A site I developed has a new requirement to get weather data from the
I've installed WordPress 3.0.2 on my local box using MS Webmatrix (beta 3). Webmatrix
I am in the following situation: I want to determine the sys- and usertime
If I download a hacked website on my laptop to fix it and I
I want to retrieve the user's location and then show a link that points
I have a MySQL table that contains many rows. The table structure is as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.