Search code examples
phphtmlcsssubdomainredirectwithcookies

Allow a user to store a subdomain as a preferred page


I have a website that I will call example.com. This website has several subdomains a.example.com, b.example.com, c.example.com and so on. I want to allow the visitors to the domain to choose a particular subdomain as the preferred page. From this point on when the user visits the page they will be automatically redirected to their preferred page.

I have no clue how to do this or even where to look.


Solution

  • First, are the users registered and logged in or are you identifying them by their IP Address?

    Registered and logged in: Log their user ID, email, or whatever you use as the primary key (hopefully ID) in the database in another table with their preferred subdomain when they select their preference.

    Not registered / logged in: Log their IP address and their subdomain choice in the table.

    Basic table structure could be

    id - ip_address(or user_id) - subdomain

    Then do an if statement checking the database for their IP or user ID.

    <?
        /* database checking code, etc, assuming we're returning $result from the query */
        $subdomain = @mysql_result($result,0,'subdomain');
        if($subdomain != '') header('Location: '.$subdomain.'.yourdomain.com');
    ?>
    

    That's only one strategy, and probably the most basic. Obviously you'll have to write the code (I'm assuming you know how to do this and want the strategy, otherwise please look at tutorials instead of asking here), and be sure that header is called before absolutely any output is generated (i.e. at the top of your file).

    If it fails to find a preference, they just stay on that page.

    Half asleep while writing this so I apologize if any information is incomplete or not understandable.