Jump to content

[solved] Force redirect to login page


rfasoli

Recommended Posts

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

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

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 by sridhar2do (see edit history)
  • Like 8
Link to comment
Share on other sites

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 by elpatron (see edit history)
Link to comment
Share on other sites

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 by elpatron (see edit history)
Link to comment
Share on other sites

  • 4 months later...
  • 2 weeks later...

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

  • 6 months later...

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 by Carlsen (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

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 by bmv (see edit history)
  • Like 4
Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...

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

  • 3 months later...

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

  • 2 weeks later...
  • 1 month later...
  • 3 weeks later...

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

  • 4 months later...

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

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

  • 1 month later...

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

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

  • 3 weeks later...
  • 3 months later...

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

  • 5 months later...
  • 2 months later...

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 by niltona (see edit history)
Link to comment
Share on other sites

  • 9 months later...

 

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

  • 2 years later...

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

  • 6 months later...
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 by shaktisingh (see edit history)
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...