4

I want to display App download message(an image)to users browsing my site from Android device only. So i have developed a plugin to achieve this . Now i want to display this message only first time so, i need to set cookie. Here I'm checking if cookie is set first then don't display message else display message but condition is not working it is showing the message everytime.

<?php

defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');

class plgsystemandroidmsg extends JPlugin {

    function onAfterRender() {
        $app = JFactory::getApplication();
        $htmlString = $app->getBody();
        if ($app->isAdmin()) {
            return;
        }
        $uri = JFactory::getURI();
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
            if (stripos($ua, 'android') !== false) {
                $cookieName = 'mymsgseen';
                $cookieValue = $app->input->cookie->get($cookieName);
                if ($cookieValue != null && $cookieValue == 1) {
                    $htmlString = 'cookie is set : ' . $cookieValue;
                    $app->setBody($htmlString);
                } else {
                    $cookieValue = 1;
                    $expire = time() + 60 * 60 * 24 * 30;
                    $app->input->cookie->set($cookieName, $cookieValue, $expire);
                    $htmlString = 'cookie is not set';
                    $app->setBody($htmlString);
                }
            }
        }
        return true;
    }

}

how can i achieve this? how to set and get cookies in joomla 3 ?

  • This source code seems Ok to me. – Anibal Sep 25 '14 at 11:46
  • @Anibal, Then, What should be the issue here ? I couldn't understand also why it isn't working? – Ram Guru99 Sep 25 '14 at 12:00
2

First of all you could first try to fit this code in your template. If may give you more flexibility there.

onAfterRender does not seem right for me to fiddle the content and to set cookies / read cookies.

Are you sure that your plugin is actually being called? Anyway, check your browser console to see the status.

  • I have tried this on my localhost with joomla default template, it is working fine, do you have any suggestions why it is not working over dedicated server? – Ram Guru99 Sep 30 '14 at 12:59
  • Is Joomla managing to send the session cookies? – Valentin Despa Sep 30 '14 at 13:11
  • Yes, It sets the session cookies. I verified it in firefox – Ram Guru99 Oct 8 '14 at 4:46
  • This is working fine with same installation on localhost but not over dedicated hosting server. Please Suggestions – Ram Guru99 Oct 9 '14 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.