SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP control link target based on stylesheet in use?

    I have php controlling the user's ability to choose a CSS stylesheet. It uses a session cookie to store the user's preference.

    Above the <HEAD> section of the page I have the following php:

    Code:
    <?php
    if(isset($_GET['css'])){
    switch ($_GET['css']) {
    case 'css1':
    $stylesheet = '<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">';
    $_SESSION['csschanger']=$stylesheet;
    break;
    case 'css2':
    $stylesheet = '<link href="http://www.domain.com/styles/style2.css" type="text/css" rel="stylesheet">';
    $_SESSION['csschanger']=$stylesheet;
    break;
    default:
    $stylesheet = '<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">';
    $_SESSION['csschanger']=$stylesheet;
    }
    }
    ?>
    Before the end of the <HEAD< section, I have the line:

    Code:
    <?php echo ($_SESSION['csschanger'])? $_SESSION['csschanger']: '<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">' ;?>
    The question I am trying to resolve is this:

    Almost the entire site is controlled with this CSS stylesheet chooser, however, I have one page where I was unable to use this. This page can only be an HTML (not php) document.

    Is there a way to use php to "read" the session cookie and make the link point to a different page.

    In other words, if the visitor has chosen the default stylesheet "style1.css", then the HTML only page is fine, however, if the visitor switches to the alternative stylesheet (style2.css), can the links pointing to the HTML only page be controlled to point to an "alternative" HTML page where the stylesheet "style2.css" is used?

    Thanks in advance...

  2. #2
    SitePoint Zealot rain5017's Avatar
    Join Date
    Sep 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    session_start();
    $stylesheet=array(
    0=>'<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">',
    1=>'<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">',
    2=>'<link href="http://www.domain.com/styles/style2.css" type="text/css" rel="stylesheet">'
    );


    $csschanger=$_GET['css']?$_GET['css']:$_SESSION['csschanger'];
    if(!in_array($csschanger,array('0','1','2'))){
    $csschanger=0;
    }
    $_SESSION['csschanger']=$csschanger;

    echo $stylesheet[$csschanger];
    ?>
    Last edited by rain5017; Sep 27, 2005 at 02:38.

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi rain5017 and thanks for the reply.

    My php skills are limited and unfortunately, I do not understand completely. Where does the code point to the alternative page?

  4. #4
    SitePoint Addict ruchit's Avatar
    Join Date
    Sep 2005
    Location
    miDDle oF NoWHere
    Posts
    222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can probably take a look at reading cookies through javascript... and in the header of the html page you can use javascript to read the value of the cookie and display the associated stylesheet.
    (however, I have one doubt that javascript would be able to access php's session cookie)

    If that's the case, you can change calls to this html page so that it is called with stylesheet name as querystring and use this querystring through javascript and load the associated stylesheet.
    work together - make world a better place
    Outsource to India
    Web Design India
    Affordable Webhosting

  5. #5
    SitePoint Zealot rain5017's Avatar
    Join Date
    Sep 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    top

    eg:
    1.php
    PHP Code:
    <?
    session_start
    ();
    $stylesheet=array(
    0=>'<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">',
    1=>'<link href="http://www.domain.com/styles/style1.css" type="text/css" rel="stylesheet">',
    2=>'<link href="http://www.domain.com/styles/style2.css" type="text/css" rel="stylesheet">'
    ); 


    $csschanger=$_GET['css']?$_GET['css']:$_SESSION['csschanger'];
    if(!
    in_array($csschanger,array('0','1','2'))){
    $csschanger=0;
    }
    $_SESSION['csschanger']=$csschanger;

    ?>

    <html>
    <head>
    <title>Site title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta name="keywords" content="keywords" />
    <meta name="description" content="description" />
    <?=$stylesheet[$csschanger]?>
    </head>
    <body>

    bode
    </body>
    </html>


Bookmarks

Posting Permissions

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