Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Posts
    3

    Exclamation Unanswered: Changing layouts w/ PHP and cookies?

    Hello, I was wondering if someone could tell me how to do this?

    Okay this is what i want to do. Im makeing a website and it has a couple layouts. I want the users to be able to change the layout of the site to the one they like best. And i need a cookie that will remember what layout they chose even after they restart.

    An example of what I am talking about is at www.dbgtn.com on the right side of the screen there is a drop down list that says choose layout. I want it to do exactly what that does. If someone could help me then i would greatly appreciate it!

  2. #2
    Join Date
    Aug 2004
    Posts
    3
    Okay someone gave me a tutorial on how to set this up. Im still a little confused on what code to put in what files and whereto put the files (ie what folder does what go in). Could someone please explain this to me?

    This is the tutorial that he gave me

    QUOTE:

    Introduction:
    In this tutorial I will show you how your site can have many layouts, all of which your users

    will be able to flip through to see!

    Step 1: Organizing the files:
    First of all, you need to have your header and footer files saved in your main directory using

    unique filenames for each layout. I.e:
    Layout 01: header01.php footer01.php
    Layout 02: header02.php footer02.php

    Step 2: The dynamic headers and footers
    Next you need to make a file called header.php with this code inside of it:
    1
    2 $default_layout = “01”;
    3 if (!$HTTP_COOKIE_VARS[‘layout’]) {
    4 $include_layout = $default_layout;
    5 } else {
    6 $include_layout = $HTTP_COOKIE_VARS[‘layout’];
    7 }
    8 if (!file_exists(“./header”.$include_layout.”.php”)) {
    9 echo “Could not include header”;
    10 } else {
    11 include_once (“./header”.$include_layout.”.php”);
    12 }
    13 ?>

    On line 2 of the above code, we define what we want our sites default layout to be if the user

    has not chosen a layout. In between the quotes you enter what is between ‘header’ and ‘.php’ in

    your default layout filename. I.e we have now decided that we want header02.php and footer02.php

    to be our default header and footer, so we have to change the $default_layout to “02”.
    On lines 3-7 is where the actual statement comes in to see if the user has choose a layout. The

    if statement reads like this:

    “If the user has not selected a layout, the layout to be included is the default layout,

    otherwise the layout to be included is the one they selected”

    Lines 8-12 then checks to see if the header file selected actually exists. Which would read like

    this:

    “If the header file does not exist, output an error message, otherwise display the contents of

    header($include_layout).php”


    That’s the header file done! We now need to create the footer file (footer.php) which should now

    be a bit easier seeing as we have already checked to see if they have selected a layout. The

    contents of footer.php will look like this:
    1
    2 if (!file_exists(“./footer”.$include_layout.”.php”)) {
    3 echo “Could not include footer”;
    4 } else {
    5 include_once (“./footer”.$include_layout.”.php”);
    6 }
    7 ?>
    This is basically lines 8-12 of the header.php, except with header being changed to footer in

    this file.

    Step 3: Basic content page
    The main part of the layout switching is done! To use this feature, you would just enter this

    into your page:
    1
    2 Your content will go here
    3

    Step 4: Creating the layout switcher engine
    We now need to create the engine for switching layouts.. here is where the ingenius part comes

    in.

    First you will need to create a file called layout_switch.php and inside this file you will need

    the following:
    1
    2 if (!$HTTP_SERVER_VARS[‘HTTP_REFERER’]) $HTTP_SERVER_VARS[‘HTTP_REFERER’] = “./index.php”;
    3 if (!$HTTP_GET_VARS[‘v’]) exit (“You have not selected a layout version!”);
    4 if (!file_exists(“./header”.$HTTP_GET_VARS[‘v’].”.php”)) exit (“The header for that layout

    version does not exists, please contact the webmaster with the subject

    ‘header”.$HTTP_GET_VARS[‘v’].”.php not exist!’”);
    5 if (!file_exists(“./footer”.$HTTP_GET_VARS[‘v’].”.php”)) exit (“The footer for that layout

    version does not exists, please contact the webmaster with the subject

    ‘footer”.$HTTP_GET_VARS[‘v’].”.php not exist!’”);
    6 setCookie ("layout", $HTTP_GET_VARS['v'], time()+(365*86400));
    7 echo "";
    8 ?>

    Line 2 says: "If a referring url cannot be found, redirect back to index.php"
    Line 3 says: "If a layout has not been select via the url, return an error"
    Line 4 says: "If the header file for this layout does not exist, output an error"
    Line 5 says: "If the footer file for this layout does not exist, output an error"
    Line 6 sets a cookie under the name "layout" with the value of the layout which they selected.

    This cookie will last for 365 days.
    Line 7 is just outputting some javascript to the browser so it redirects back to the referer.

    :END QUOTE

  3. #3
    Join Date
    Feb 2004
    Posts
    533
    Im still a little confused on what code to put in what files and whereto put the files
    LSkakarotto This is an interesting problem or idea to solve. I've been giving it some thought. The example you have looks good but leaves some wholes as far as being a finished solution. I think to use this format you will want to have a main 'index.php' -entry point- that includes the header.php, footer.php

    If the user has a previously set cookie then you would show their selected layout and update the cookie expiration date. If not then show the default and include controls to select a layout or choose a layout preferences selection form on the default setting Layout page.

    Do you have a development environment setup on your PC with a local server and the PHP kernel? Something like this you will need to work out all the bugs. Another option besides changing the Header.php, layout.php, footer.php may be to use CSS, Cascading Style Sheet, then use the same basic idea from your example to change the the Defined CSS file based on the users cookie settings. With some looking around you may find some pre-configured examples that are more complete.

    something to note about cookies and PHP the 'SetCookies' function must be run in the Headers section of the HTLM output. Here's another tut I found on PHP cookies that may give some information you need.

    http://www.spoono.com/php/tutorials/tutorial.php?id=18
    ~

    Bill

  4. #4
    Join Date
    Aug 2004
    Posts
    3
    I dont have any development environment on my PC but I can upload to my server which has PHP and all that real easily.

    Unfortunatly I dont know anything about Headers and Footers. How do i set up the page to refer to them? how does it work? Im so confused!

  5. #5
    Join Date
    Feb 2004
    Posts
    533
    Quote Originally Posted by LSkakarotto
    I dont have any development environment on my PC but I can upload to my server which has PHP and all that real easily.

    Unfortunatly I dont know anything about Headers and Footers. How do i set up the page to refer to them? how does it work?
    I like to using an IDE (integrated development environment), and testing on a local server. Though I upgraded my PC awhile back and still have not re-setup PHP and everything on the new PC. I was using Badblue for the local server and tried a couple different IDEs but havn't found one I really like yet. More often I just use UltraEdit for coding. I'm still very much a beginner with PHP, read through 'Teach Yourself PHP in 24 hours' in just 30 days wanting to learn more.

    With PHP you can Include a file to run additional code or printout text HTML or Other using the Require Statement or Require_Once (nameoffile)

    I found this artical for creating a Content Management Page with custom layouts using PHP and CSS. I think this will help to understand the process. See this link: http://www.alistapart.com/articles/phpcms/
    ~

    Bill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •