I am new to Drupal and also have very limited PHP knowledge.

On my first Drupal site, (7.x) I have built a gated page that site visitors can get to only after filling out a form.

I'd like people to be able to revisit that gated page without having to fill out the form again, so I was thinking I could set a cookie the first time they fill out the form, then if they landed on the form page a subsequent time Drupal could check for a cookie and if it was there, redirect them straight to the gated page.

On a regular site, I would set a cookie then check for the cookie, but as far as I can tell in Drupal I need to use modules to do this rather than add code to the page template head, so I have set up a very simple one.

First, I'm using the webform module which sets a cookie on submission of the form, then sends the user to the gated page.

I just can't figure out how to get Drupal to check for the cookie and redirect (if the cookie is there) when users land on that form page a second, third, etc. time.

I copied and adjusted something similar I found in the Drupal forums but it's not working. It could be really simple error or maybe there is a way better way to do this? Thanks for any help. My code is below.

Caitlin

<?php
//$Id: check_cookie.module $
function check_cookie() {
  global $base_url;
  global $cookie_domain;
  $cookie_name = 'webform-3';        		 //name of cookie that gets set by form
  $destination = '?q=node/10';     			   // protected download page url, send user if they have cookie already.
  
  if (arg(1) == 3) {   					// the number of the node (nid) of the form page
  	if (isset($_COOKIE[$cookie_name])) {
	  drupal_goto($destination); 		// the path to be redirected to
	} 
  } 
}

Comments

colony’s picture

I need to do the same exact thing, I'm using webform and 'redirect' option, and only dealing with anonymous users. Perhaps if you have solution you'd be willing to share.... Thx C

przemek_kompix’s picture

Dose only solution to redirect if cookie is present is to programming hook drupal_redirect_form ?

Best Regards
Przemysław Staniszewski

przemek_kompix’s picture

Is better solution to redirect user from node to form when cookie is not present then using

hookurl_inbound_alter(&$path, $original_path, $path_language) and $_COOKIE table?

Best Regards
Przemysław Staniszewski

ancientDX’s picture

To check for a cookie and redirect users if the cookie is present in Drupal 7, you can use a custom module and implement hook_init() or hook_boot(). Here's an example of how you can achieve this:

  1. Create a custom module: Create a new module in your Drupal installation. You can name it something like "custom_cookie_redirect". You'll need to create a folder for your module under the "sites/all/modules" directory and include a ".info" file and a ".module" file.

  2. Implement hook_init() or hook_boot(): In your custom module's ".module" file, add the following code:

  3. /**
     * Implements hook_init().
     */
    function custom_cookie_redirect_init() {
      // Check if the cookie is set.
      if (isset($_COOKIE['your_cookie_name'])) {
        // Perform the redirect to the gated page.
        drupal_goto('your-gated-page-url');
      }
    }
     

  4. Replace 'your_cookie_name' with the name of the cookie you set in your webform module. Replace 'your-gated-page-url' with the URL of the gated page where you want to redirect users if the cookie is present.

  5. Enable the custom module: Go to the Extend page in your Drupal admin interface and enable your custom module.

    After implementing the above code, Drupal will check for the presence of the cookie on every page load. If the cookie is set, users will be automatically redirected to the gated page.

    Remember to clear Drupal's cache after enabling the module or making any changes to the code.

    Note: The code provided assumes you are using Drupal 7. If you are using a different version of Drupal, some modifications may be necessary.  here the website you can your entire url redirection path called redirect checker.

    If you encounter any issues or need further assistance, feel free to ask!