Change CSS when url contains a parameter

Discussion in 'PHP' started by dondor, May 25, 2010.

  1. #1
    Hi all,

    I'd appreciate your help if anyone can tell me how can I change the css file I want to call in when there's a certain parameter in the url.

    for example:

    My site (www.mysite.com) is using style.css
    I want when someone gets to my site with a parameter in the url (www.mysite.com?id=5) to use style2.css

    Thanks
     
    dondor, May 25, 2010 IP
  2. Travis

    Travis Peon

    Messages:
    539
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like this should get the job done, if you place it in the <head></head> tags:


    (Haven't tested it as I'm not at home).

    
    
    <?php
    $css_file = htmlspecialchars($_GET['id']);
    
    if (($css_file != '') && (is_numeric($css_file))) {
    	switch ($css_file) {
        case 0:
            echo "<style>";
            break;
        case 1:
            echo "<style>";
            break;
        case 2:
            echo "<style>";
            break;
    	}
    } else {
    	//return default CSS
    	echo "<style>";
    }
    	
    ?>
    
    
    PHP:
     
    Travis, May 25, 2010 IP
    dondor likes this.
  3. dondor

    dondor Well-Known Member

    Messages:
    844
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #3
    works great, thanks Travis !!
     
    dondor, May 26, 2010 IP
  4. cupi

    cupi Greenhorn

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    You can also simply retrieve the value from the URL and then embed it in the file path, like this:

    <?php
    $id = $_GET['id'];
    ?>
    
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="test<?php echo $id ?>.css" />
    </head>
    </html>
    Code (markup):
     
    cupi, May 27, 2010 IP
  5. Lord

    Lord Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    cupi that's not secure at all you don't want to use that method the $id must be checked to be a number like Travis did it
     
    Lord, May 27, 2010 IP