Print Reseller Scheme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Font Switcher

Discussion in 'Website Design Forum:' started by iEthan, Oct 9, 2009.

  1. iEthan

    iEthan Member

    So, I'm trying to create a PHP script that does the following:

    There is a list of links, each representing a different font. When you click on one of them, it will change the font (I'm using the @font-face rules), then save it as a cookie for the next time they visit.

    I just can't figure out how to do it.
     
  2. Jazajay

    Jazajay Active Member

    Hay fella, try this:
    Have the link set to something like.
    <a href="samepage.php?font=arial" rel="nofollow">Arial</a>

    Then at the top of the page, you could set it via an include or add it in via a redirect page and just redirect it back depends on how the site is set up, but test for the get variable, if the get is present set a separate style sheet overriding the body font, add the rel noindex meta to stop search engines from indexing the same copy of the page under different urls if a competitor links to it and bobs your uncle job done.

    So ~
    XHTML:
    <link style="css/main.css"...
    <?php if(isset($_GET['font'])){?>
    <meta name="robots" content="noindex, follow" />
    <?php If($_GET['font']=="arial"){?>
    <link style="css/arial.css"...
    <?php }elseif($_GET['font']=="verdana"){?>
    <link style="css/verdana.css"...
    <?php }else(....?>
    <link style="css/...
    <?php }
    }?>

    Arial CSS:
    body{font-family: Arial}

    Verdana CSS:
    body{font-family: Verdana}

    O and set a cookie and test for it if you want as well, if you need help with that let me know. However again depending on how many pages this will be implemented on it may be slightly better to code the CSS on-page. If it is to be used on several pages then add it via a CSS sheet so the sheet can be cached and thus speed up page rendering.

    Jaz

    Key:
    Blue ~ XHTML
    Red ~ PHP
    Green ~ CSS
     
  3. iEthan

    iEthan Member

    Great! This helps so much! Thanks!
     
  4. Jazajay

    Jazajay Active Member

    No worries, glad to help. :)
     
  5. Harry

    Harry Senior Member

  6. Jazajay

    Jazajay Active Member

    Err...Isn't that more or less what I've just written but with about 4 times as much code? :p
     
  7. Harry

    Harry Senior Member

    You've not written the cookie have you?
     
  8. Jazajay

    Jazajay Active Member

    No but I said I would do it if he wanted. Even so what 1 line and an extra if statement, still a massive reduction in code dude.
     
  9. Harry

    Harry Senior Member

    Ah cool, well I'd love to take a look see if the cookie stuff gets written.
     
  10. Jazajay

    Jazajay Active Member

    :D Okay Harry I don't mind teaching you something new, :D
    Right take my above code, if I was going to do it TBH I would do it via a PHP session, that way it should work if cookies are disabled. But I could add a cookie version just for you Harry if you so wish, you just let me know fella if you want help with it. :)

    So at the top of the page above everything else lets start the session.

    So ~
    <?php session_start()?>

    Next we bring in the code I wrote with 2 added if's and a couple of extra lines of code.
    <link style="css/main.css"...
    <?php if(isset($_SESSION['style'])){
    if(
    $_SESSION['style']=="arial" || $_SESSION['style']=="verdana" || $_SESSION['style']==...){?>
    <link style="css/<?php echo $_SESSION['style']?>.css"...
    <?php }
    }else{

    if(isset($_GET['font'])){?>
    <meta name="robots" content="noindex, follow" />
    <?php If($_GET['font']=="arial"){
    $_SESSION['style']="arial"?>
    ?>
    <link style="css/arial.css"...
    <?php }elseif($_GET['font']=="verdana"){
    $_SESSION['style']="verdana"?>
    ?>
    <link style="css/verdana.css"...
    <?php }else(....)
    $_SESSION['style']="....?>
    <link style="css/...
    <?php }
    }
    }?>

    I don't think I have made a syntax error, but without testing I cant tell If I made a mistake changing the code for sure, if you get 1 let me know but you shouldn't it looks fine.

    Basically, this is for Harry's benefit, :D, lets explain the changes. lol.
    <?php session_start()?>

    This starts PHP session function, this needs to be at the top of all the pages using this.

    <?php if(isset($_SESSION['style'])){
    if(
    $_SESSION['style']==arial" || $_SESSION['style']=="verdana" || $_SESSION['style']==...){?>
    <link style="css/<?php echo $_SESSION['style']?>.css"...
    <?php }else{

    We then test to see if a session is set, if it is we add a security test to see if it is something we set it to. We then echo it out to the newly created CSS sheet. I don't think the meta robots should be needed as it would have been set on the page you set the session on and thus not needed on any secondary pages.

    If it's not set it's the first time it's being initiated so we add the first section of code in an else statement.

    Then once we have worked out which get variable it is we set the session to contain that font so we can then use it in the first if statement. Easy and still hugely reduced code Harry. :D

    Note: If you are going to have lots of fonts I would take out the security test via an if statement and instead add the security test via a PHP switch statement TBH, much more effective then, but that would depend on the amount of fonts you want to check.

    Is there anything else I can help you with today Harry or is that it for today? lol :D

    Jaz
    Key:
    Blue ~ XHTML
    Red ~ PHP

     
  11. Harry

    Harry Senior Member

    I know PHP and that, I'm just not fluent in it. That makes sense (without your patronising explanation :p ) to me mate, cheers.
     
  12. Jazajay

    Jazajay Active Member

    O sorry, lol, lol, thought you were taking the pis* TBH, to see if I could, lol. :D

    My appologese fella. :)
     
  13. Harry

    Harry Senior Member

    Nah I was genuinely inquisitive but I know what it all does.
     
  14. Jazajay

    Jazajay Active Member

    Aww good, good I personally would always use a session over a cookie, due to the fact that a session gets saved in a cookie anyway.

    Just remember to start then on every page you want to use them and they must come before the doctype to work properly.

    But yeah I knew you could read it hence why I thought you were taking the Mickey mouse. :)
     
  15. Kevin

    Kevin Senior Member

    Just had to do it... :lol:

    [​IMG]
     
  16. Jazajay

    Jazajay Active Member

    Onartis what can I say?
    I honestly mean that, truely I do. :D

    Dude you have way too much time on your hands but I do like the wizard's gaze. Maybe you should do him slightly higher than me, that way he's looking down on me with disgust. Just a thought fella. lol. :clap:
     

Share This Page