rfasoli Posted January 29, 2013 Share Posted January 29, 2013 Hi, I'm new as PrestaShop developer. How can I create a module that redirects users to the login page if they are not logged in? I want to create a private shop. I know that already exists modules that does this but I want to create my one to learn better the prestashop architecture. Thanks Link to comment Share on other sites More sharing options...
vekia Posted January 29, 2013 Share Posted January 29, 2013 it's very simple and you don't have to use module in this case. But i must know something more about what you want. well, you want to redirect user to login page, even if the user are on product page, cms pages etc? or what? Link to comment Share on other sites More sharing options...
rfasoli Posted January 29, 2013 Author Share Posted January 29, 2013 Thank you very mutch for your quick answer :-) If the user try to load whatever page, if he has not already logged in, I want to redirect to the login page (I don't want that an anonymous user can view my catalog). I don't know if I have to use a hook or if I have to override some function in some controller (IndexController ?) Link to comment Share on other sites More sharing options...
sridhar2do Posted January 31, 2013 Share Posted January 31, 2013 (edited) Goto classes\controller\FrontController.php Find the function public function init() Past the below mentioned code after parent::init(); if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') Tools::redirect('index.php?controller=authentication?back=my-account'); Edited January 31, 2013 by sridhar2do (see edit history) 8 Link to comment Share on other sites More sharing options...
rfasoli Posted January 31, 2013 Author Share Posted January 31, 2013 Thank you very muth sridhar2do! It's works perfect! Link to comment Share on other sites More sharing options...
vekia Posted January 31, 2013 Share Posted January 31, 2013 thanks for sharing the solution, i also marked this topic as solved regards 1 1 Link to comment Share on other sites More sharing options...
El Patron Posted February 3, 2013 Share Posted February 3, 2013 (edited) Goto classes\controller\FrontController.php Find the function public function init() Past the below mentioned code after parent::init(); if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') Tools::redirect('index.php?controller=authentication?back=my-account'); the problem with this solution? 1. Very bad for SEO, now search engines can not index your site. 2. Any product links from other websites will not work, as they will be forced to log in and lose the original link. Edited February 3, 2013 by elpatron (see edit history) Link to comment Share on other sites More sharing options...
rfasoli Posted February 4, 2013 Author Share Posted February 4, 2013 Yes elpatron, your observations are right related with a public e-commerce but, with a private shop this is exactly what I want Link to comment Share on other sites More sharing options...
El Patron Posted February 4, 2013 Share Posted February 4, 2013 (edited) Yes elpatron, your observations are right related with a public e-commerce but, with a private shop this is exactly what I want Nothing but kind words for the solution posted, but typical private shops do not require this sort of solution. I posted just to make others that read this post aware of what happens. So if you do not care that search engines can not index your content, your wishes have come true. EDITED: I will test this, if one builds a sitemap.xml then will the site be indexed? I think it would, it would just be the bots that could not follow...will post results... Edited February 4, 2013 by elpatron (see edit history) Link to comment Share on other sites More sharing options...
Koekedoe Posted June 25, 2013 Share Posted June 25, 2013 (edited) Okay, i added this code FrontController.php and it works so far, but after the customer is logged in, it shows the my account page. But i like it to be the homepage. What do i have to adjust further? (i'm using 1.5.4) Edited June 25, 2013 by Koekedoe (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted June 25, 2013 Share Posted June 25, 2013 use another value for tools::redirect, point it to your homepage Link to comment Share on other sites More sharing options...
cammaillard Posted July 9, 2013 Share Posted July 9, 2013 That's great solution, exactly what I was looking for! Just one thing : do you have an idea on how to exclude "CMS pages" and "contact-us page" from this ? I would like that Google and guest can access to those pages. I've been trying many solution today and nothing is working ! Thanks a lot. Link to comment Share on other sites More sharing options...
Carlsen Posted February 8, 2014 Share Posted February 8, 2014 (edited) Just one thing : do you have an idea on how to exclude "CMS pages" and "contact-us page" from this ? In the code posted by sridhar2do, you add this after 'password': && $this->php_self != 'cms' && $this->php_self != 'contact' This will "unlock" the CMS pages and contact-form. Result: if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password' && $this->php_self != 'cms' && $this->php_self != 'contact') Tools::redirect('index.php?controller=authentication?back=my-account'); (Sorry for bumping an old thread) Edited February 8, 2014 by Carlsen (see edit history) 1 Link to comment Share on other sites More sharing options...
bmv Posted March 25, 2014 Share Posted March 25, 2014 (edited) Hi, The proposed solution works very nicely, but bear in mind this changes will be lost when you update prestashop to a newer version. It is a lot better idea to use the override function of prestashop 1.5 Simply upload the original file: classes\controller\FrontController.php into: override/classes/controller/FrontController.php Next, rename the class. Final code should look like this: class FrontController extends FrontControllerCore { public function init() { parent::init(); if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') { Tools::redirect('index.php?controller=authentication?back=my-account'); } } } The last step is to manually delete the following file so prestashop is aware of the overriden class ( It will be re-generated automatically.) cache/class_index.php And voilà, functionality achieved without overwriting core files. HTH Edited March 25, 2014 by bmv (see edit history) 4 Link to comment Share on other sites More sharing options...
Buaha Posted May 19, 2014 Share Posted May 19, 2014 Hi, i have multistore. what i need to change in this code so only users from one store must be login? Link to comment Share on other sites More sharing options...
Ash.Krish Posted October 15, 2014 Share Posted October 15, 2014 Okay, i added this code FrontController.php and it works so far, but after the customer is logged in, it shows the my account page. But i like it to be the homepage. What do i have to adjust further? (i'm using 1.5.4) use another value for tools::redirect, point it to your homepage Guys any more updates on this @vekia, can you be bit more details on adding another value i tried copying the tools:redirect in to next line but it ends with error.... please explain more that would be very useful... thanks Ash Link to comment Share on other sites More sharing options...
LuckyMe Posted January 27, 2015 Share Posted January 27, 2015 Hi, i have multistore. what i need to change in this code so only users from one store must be login? One quick solution for multi store override is to check the theme name: <?php class FrontController extends FrontControllerCore { public function init() { parent::init(); if (_THEME_NAME_=='shop_theme_name') { if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') { Tools::redirect('index.php?controller=authentication?back=my-account'); } } } } Link to comment Share on other sites More sharing options...
ArsalanAnsari Posted February 5, 2015 Share Posted February 5, 2015 how can i do this in 1.6.0.11? Need help Link to comment Share on other sites More sharing options...
studiotom Posted March 17, 2015 Share Posted March 17, 2015 hello , authentication.tpl login form how to not display it on : logowanie?multi-shipping BUT display it on : logowanie?back=my-account i've tried , doesnt work {if page_name == "logowanie?back=my-account"} formform {/if} Link to comment Share on other sites More sharing options...
< Spider > Posted April 4, 2015 Share Posted April 4, 2015 In the code posted by sridhar2do, you add this after 'password': && $this->php_self != 'cms' && $this->php_self != 'contact' This will "unlock" the CMS pages and contact-form. Result: if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password' && $this->php_self != 'cms' && $this->php_self != 'contact') Tools::redirect('index.php?controller=authentication?back=my-account'); (Sorry for bumping an old thread) nice Carlsen, works like a charm! What about excluding also the "blog module" too?. I want it to be visible to non-logged users too. I've tried to add $this->php_self != 'blog' or $this->php_self != 'st_blog' or $this->php_self != 'stblog' and none works. Any idea? Thanks for help! Link to comment Share on other sites More sharing options...
gamdev Posted September 2, 2015 Share Posted September 2, 2015 Hi, The proposed solution works very nicely, but bear in mind this changes will be lost when you update prestashop to a newer version. It is a lot better idea to use the override function of prestashop 1.5 Simply upload the original file: classes\controller\FrontController.php into: override/classes/controller/FrontController.php Next, rename the class. Final code should look like this: class FrontController extends FrontControllerCore { public function init() { parent::init(); if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') { Tools::redirect('index.php?controller=authentication?back=my-account'); } } } The last step is to manually delete the following file so prestashop is aware of the overriden class ( It will be re-generated automatically.) cache/class_index.php And voilà, functionality achieved without overwriting core files. HTH I'm trying to put in override, but does not work me. I work well if I add in "classes\controller\FrontController.php", but if I put in override "override/classes/controller/FrontController.php" not work can you help me :S Link to comment Share on other sites More sharing options...
vekia Posted September 3, 2015 Share Posted September 3, 2015 1) create override file2) remove class_index.php from /cache/ directory3) try again (refresh page) 1 Link to comment Share on other sites More sharing options...
gamdev Posted September 4, 2015 Share Posted September 4, 2015 thank you so much ^__^ Link to comment Share on other sites More sharing options...
mayman_design Posted September 4, 2015 Share Posted September 4, 2015 use another value for tools::redirect, point it to your homepage I've tried this and can redirect to other pages but struggling with the homepage do you have any further details? Thanks Link to comment Share on other sites More sharing options...
mayman_design Posted September 11, 2015 Share Posted September 11, 2015 Hello? Anyone can help with redirection to homepage after login? Link to comment Share on other sites More sharing options...
joaomag Posted September 14, 2015 Share Posted September 14, 2015 This solution is perfect when you have a private shop. What sshould be the solution if i want that the shop is normal, but if we try to add a product and isn't registred it would make himj login or register an account ? Can someone give me an idea how to do it. On my case the users will be very interested because the prices are the best, but i'm having a lot of abandoned carts and i need at leats to have an e-mail so i can contact them ... Thx Link to comment Share on other sites More sharing options...
thijsvk Posted November 4, 2015 Share Posted November 4, 2015 I'm trying to adapt this solution to my needs (allow access to several CMS pages to visitors, all others need login first), however, I don't even get that far. When I put in the override and remove class_index.php I get a 'Fatal error: Allowed memory size of "X" (tried to allocate "X" bytes) exhausted' on line XXX' Could this be because it is a local installation on XAMPP (PHP.ini has a memory limit of 128M) or could it have something to do with the actual override? Link to comment Share on other sites More sharing options...
mbastida Posted November 6, 2015 Share Posted November 6, 2015 Goto classes\controller\FrontController.php Find the function public function init() Past the below mentioned code after parent::init(); if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') Tools::redirect('index.php?controller=authentication?back=my-account'); Muchas gracias, ha sido muy útil. Thanks for help! very useful! Link to comment Share on other sites More sharing options...
Jarodx Posted November 25, 2015 Share Posted November 25, 2015 (edited) With this solution I have problems with payments. When I pay the notification of payment of POS is untreated. add && $this->php_self != 'order_confirmation' neither works. Any solution? thx Edited November 25, 2015 by Jarodx (see edit history) Link to comment Share on other sites More sharing options...
pedjadavidovic91 Posted March 17, 2016 Share Posted March 17, 2016 Can anyone tell me how to: Make blank page with login and registration form for users who are not loggedin.Something similar like this: if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password' ) Tools::redirect('index.php?controller=authentication?back=my-account'); But just with blank page and login form. Link to comment Share on other sites More sharing options...
kikiritikosul Posted September 3, 2016 Share Posted September 3, 2016 How to make blank page with login and registration form for users who are not loggedin? does anyone have any solution? Pls.... Link to comment Share on other sites More sharing options...
Krystian Podemski Posted September 5, 2016 Share Posted September 5, 2016 Override FrontController init, check if user is logged, if not and page is different than authentication redirect to index.php?controller=authentication Link to comment Share on other sites More sharing options...
vekia Posted September 5, 2016 Share Posted September 5, 2016 code for solution that Krystian proposed: if (!$this->context->customer->isLogged() && Tools::getValue('controller') != 'authentication') { Tools::redirect('index.php?controller=authentication'); } Link to comment Share on other sites More sharing options...
niltona Posted November 24, 2016 Share Posted November 24, 2016 (edited) use another value for tools::redirect, point it to your homepage Hello, I want to redirect to login page users don't logged but I want do show homepage, contact page and cms page... What's the code? SOLVED: if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password' && $this->php_self != 'cms' && $this->php_self != 'contact' && $this->php_self != 'index') Tools::redirect('index.php?controller=authentication?back=my-account'); Edited November 25, 2016 by niltona (see edit history) Link to comment Share on other sites More sharing options...
eldrico Posted September 21, 2017 Share Posted September 21, 2017 One quick solution for multi store override is to check the theme name: <?php class FrontController extends FrontControllerCore { public function init() { parent::init(); if (_THEME_NAME_=='shop_theme_name') { if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') { Tools::redirect('index.php?controller=authentication?back=my-account'); } } } } Hi Guys, I know the topic is a bit but does someone make it working with the multistore mode? I am currently using the same template for both shops in a multistore mode. 1shop for retail and i want 1 private shop for wholesale (only logged in wholesaler should see the shop). The solution works perfectlybut on both shops. I have 2 urls like : http://myshop.com and http://myshop.com/wholesale Thanks if someone can help. Link to comment Share on other sites More sharing options...
juancho300000 Posted October 1, 2019 Share Posted October 1, 2019 hi, sorry for the time, i search for 1.7.4 but i can´t redirect well, i have a override with frontcontroller, i try to use that but nothing, in the home it´s show ok, but when i try to create a new account, the last step(save) i get a error "SyntaxError: Unexpected end of JSON input", if i delete the overrride all is ok i use that if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password') { Tools::redirect('index.php?controller=authentication?back=my-account'); } Link to comment Share on other sites More sharing options...
juancho300000 Posted October 1, 2019 Share Posted October 1, 2019 i use 1.7.6, sorry Link to comment Share on other sites More sharing options...
shaktisingh Posted April 9, 2020 Share Posted April 9, 2020 (edited) On 1/29/2013 at 6:53 PM, vekia said: it's very simple and you don't have to use module in this case. But i must know something more about what you want. well, you want to redirect user to login page, even if the user are on product page, cms pages etc? or what? hello may you tell me where can i get frontend login controller in folder in prestashop 1.7.6.1 Edited April 9, 2020 by shaktisingh (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now