Jump to content
rfasoli

[solved] Force redirect to login page

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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 ?)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

thanks for sharing the solution, i also marked this topic as solved

 

regards

  • Like 1
  • Haha 1

Share this post


Link to post
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)

Share this post


Link to post
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 :)

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

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 by Koekedoe (see edit history)

Share this post


Link to post
Share on other sites

use another value for tools::redirect, point it to your homepage

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Hi,

i have multistore.

what i need to change in this code so only users from one store must be login?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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');
			}
		}
	}
}

Share this post


Link to post
Share on other sites

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}

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

1) create override file
2) remove class_index.php from /cache/ directory
3) try again (refresh page)

  • Like 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

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 by Jarodx (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

code for solution that Krystian proposed:

if (!$this->context->customer->isLogged() && Tools::getValue('controller') != 'authentication') {
Tools::redirect('index.php?controller=authentication');
}

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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'); }

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More