1

So on all pages of my site including error pages I want a cookie to be created on the clients browser to define if they are logged in or not.

Just need to know what Joomla core file or plugin etc I should go to (line number) and edit to achieve this.

Here is my example code :

Cookie name is "logged_in"

Value : "1" (registered user is logged in)

Value : "0" (guest user not logged in)

$app = JFactory::getApplication();
if(JFactory::getUser()->guest) { //User is guest
$app->input->cookie->set("logged_in", "0", time() - 1, $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection());
} else { //User has an account and is logged in
$app->input->cookie->set("logged_in", "1", 0, $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection());
}

3 Answers 3

2

You are forcing a PHP login in the CMS. You must follow Joomla philosophy to implement a general change. For example, implementing a user or system plugin to define a cookie.

Additionally, Joomla does already have session cookies.

1
  • If you see the comment i left on the @Nagarjun answer you will see what i am going to achieve.
    – C0nw0nk
    Aug 24, 2016 at 12:18
1

You don't need to create any cookie, Joomla already creates one when the user logged in. You just need to use Joomla API to know whether user is logged in or not. Here is an example:

$user = JFactory::getUser();
$loggedIn = $user->guest ? false :  true;
2
  • Yes but both guests and logged in users get the same MD5 cookie format so with my method i can set a cookie like this and the web server now knows what users to cache for and to send guest users conservative pages and registered logged in users progressive cache. proxy_cache and fastcgi_cache from Nginx. It allows me to reduce impact on mysql and PHP if i do it like this. My Nginx server can cache their activity.
    – C0nw0nk
    Aug 24, 2016 at 12:17
  • While PHP does cache, php resources impact and the MySQL query made to serve the page is still using un-needed resources Nginx can really save the day. I have already setup progressive caching Nginx using fastcgi_cache as can be seen here networkflare.com but with this cookie method i can make it so guest users all get the same cached pages (conservative) and registered logged in users all get their own unique cached pages (progressive).
    – C0nw0nk
    Aug 24, 2016 at 12:24
1

You can use the index.php file in joomla root folder, it should be called on every page, but I don't know if it could be overwritten in future joomla updates.

Another way could be including your code in your template (index.php, component.php and error.php) to reach every page in the front-end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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