2

I need to host multiple different services on different subdomains sharing the same top level domain and I want to centralize the login procedure.

The idea was to have:

site1.tld.com 
site2.tld.com 
site3.tld.com 
siteX.tld.com 
login.tld.com

And redirect the users to login.tld.com. Then, once logged, they can have access to the services.

What's the best way in PHP to have it implemented and secured?

Is it possible to not store local cookies and keep the session open?

adding more details

I need the user to also have the possibility to remember the connection after he closes the browser. I have full access to the server thus I can configure php.ini as I want

int 2Eh
  • 325
  • 1
  • 3
  • 12

1 Answers1

3

If you don't want to set any local cookie, you'll need to stick to sessions. Having a session across sub domains works a tiny bit different than just on one domain. So lets get started.

First we'll need to give our session cookie a new session_name(). We need to do this because a session name must be defined before setting cookie parameters. This will store the old session name under $old_name and update the name of your session to "some_name":

$old_name = session_name("some_name");

Next we'll have to set the session cookie parameters with session_set_cookie_params(). Here's where we tell our server where the session cookie will function:

session_set_cookie_params(0, '/', '.tld.com');

By preceding the 3rd paramenter (domain "tld.com") with a dot, we're making sure the session cookie will be visible on all subdomains. As an alternative, you could also use:

ini_set('session.cookie_domain', '.tld.com');

Lastly ofcourse we need to start or resume our session in our script with session_start():

session_start();

So to sum it up, you should have this on top of each script that'll use your sessions:

<?php

$old_name = session_name("some_name");
session_set_cookie_params(0, '/', '.tld.com');
//ini_set('session.cookie_domain', '.tld.com'); //Uncomment and comment above line if prefered.
session_start();

As a sidenote I should include that setting php.ini parameters isn't allowed on most shared hosting providers. This script assumes you've got full access to your server or at least a provider willing to let you change those settings.

icecub
  • 7,964
  • 5
  • 34
  • 63