Wednesday, October 6, 2010

PrestaShop Tips - How to allow mutiple access at maintenance mode without IP address

PrestaShop has a maintenance mode which only allow access from specified IP addresses. This function is very good and can satisfy most store owner's needs.

But for some reason (e.g. testing),if you want allow multiple users to access, you will have to collect all user's IP addresses. Another problem is that most store owners are using dynamic IP address. It is troublesome to look up his/her public IP and change the setting.

Here is I introduce a solution that can solve above problem. It only requires a few lines of code.

The solution is to use the same maintenance mode setting, but I will use the IP address field as Maintenance Access Token. Then at maintenance mode, you access your front store with the Maintenance Access Token as URL parameter.

1. Change ./init.php to make this happen
Replace line 204 with following lines.(line # may differ, see screen shot for detail)

if(isset($_GET['MAT']))$_SESSION['MAT'] = $_GET['MAT'];
if (isset($maintenance) AND (!isset($_SESSION['MAT']) OR $_SESSION['MAT'] !=Configuration::get('PS_MAINTENANCE_IP')))

For 1.4x, you need change as following (thanks Debbie for the contribution)

Find this line in file /classes/FrontClass.php,

if (!in_array(Tools::getRemoteAddr(), explode(',', Configuration::get('PS_MAINTENANCE_IP'))))

replace it with this:

if(isset($_GET['MAT']))$_SESSION['MAT'] = $_GET['MAT'];
if (!isset($_SESSION['MAT']) OR $_SESSION['MAT'] !=Configuration::get('PS_MAINTENANCE_IP'))

2. Set you site to maintenance and set Maintenance Access Toeken

3. Access your site with Maintenance Access Token

Now you access at maintenance mode without look up your IP address any more.


Mike said...

Thanx!! That really helps. Thanx for the effort to post this and what you did!!


Debbie said...

I've been using this for a while now, and love it! Its so much easier than having to update the IP every time it changes (I have a dynamic IP address).

However, it doesn't seem to work on the latest version of PS. Is there any chance of getting an update to the code to work with PS 1.4.3, pretty please? :)

Alvin said...

thanks for feedback.
I will take a look when I got a chance.

Alvin said...

In PrestaShop 1.4x, you are able use multiple IP address, separated by comma, so it is not necessary to do something special to allow multiple access.

Debbie said...

Hi Alvin - the problem for me isn't allowing multiple IP addresses, its having to continually update the IP address.

I don't know where you are in the world, but in Australia dynamic IPs are the norm. So when I'm working on a site for a client, not only do I have to keep updating the IP address so I can access the site, but so my client can access it too. It becomes extremely annoying.

Your code allowed me to put a site into maintenance mode without the hassle of having to update the IP addresses everyday!

I'm sure I'm not the only one who used it for this reason :)

Alvin said...

Hi Debbie
Thanks for your response.
yes, you are right, dynamic IP is annoying. that's why I wrote this blog.

"Your code allowed me to put a site into maintenance mode without the hassle of having to update the IP addresses everyday!"

Your words inspired me to try to get the code for PrestaShop 1.4x.
But I am really busy at this moment, I will try get it dine during coming weekend.

In case I forget, please remind me.

Debbie said...

I had a bit of time today, so I managed to figure this out myself. It actually only required a minor change to the code above.

In PS 1.4.3, replace the following line in classes/FrontController.php:

if (!in_array(Tools::getRemoteAddr(), explode(',', Configuration::get('PS_MAINTENANCE_IP'))))

with this:

if(isset($_GET['MAT']))$_SESSION['MAT'] = $_GET['MAT'];
if (!isset($_SESSION['MAT']) OR $_SESSION['MAT'] !=Configuration::get('PS_MAINTENANCE_IP'))

Thanks again Alvin for your wonderful code - its so handy! :)

Alvin said...

Hi Debbie,
Thank your for the contribution.
I was interrupted by something else while I was trying to do this. Sorry for not able to do that.

I have updated my post so that it will benefit someone else.

And thanks again for your contribution.

Anonymous said...

Perfect, thank you so much for posting that for PS 1.4.