johnathanamber Posted January 8, 2010 Share Posted January 8, 2010 Hey everyone, I have a site that has multiple URLs and CSS files. Based on the URL that the user goes to I want that CSS file to load. This is what I have thus far in the <head> of my HTML/PHP file: index.php <!-- ====================================================== --> <!-- Which CSS template to use? It depends on the site URL. --> <!-- ====================================================== --> <?php $site = $_SERVER['SERVER_NAME']; ?> <!-- ====================================================== --> <!-- ============== Harvest Auction Domains =============== --> <!-- ====================================================== --> <?php if ($site = "www.agcooponline.com") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.agliberty.com") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.harvestauction.com") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.harvestauction.net") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.harvestauction.org") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.harvestauction.biz") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.harvest2market.com") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <!-- ====================================================== --> <!-- ============== Hobbies Auction Domains =============== --> <!-- ====================================================== --> <?php if ($site = "www.hobbiesauction.com") { ?> <link href="../templates/tsgcalpha/css/hobbiesauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <!-- ====================================================== --> <!-- ============== Tools Auction Domains ================= --> <!-- ====================================================== --> <?php if ($site = "www.toolsforauction.com") { ?> <link href="../templates/tsgcalpha/css/toolsforauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.tooleagle.com") { ?> <link href="../templates/tsgcalpha/css/toolsforauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <?php if ($site = "www.tooltwister.com") { ?> <link href="../templates/tsgcalpha/css/toolsforauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> <!-- ====================================================== --> <!-- ============== Aviation Auction Domains ============== --> <!-- ====================================================== --> <?php if ($site = "www.aviatorsauction.com") { ?> <link href="../templates/tsgcalpha/css/aviatorsauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> </head> However when I go to any of the URLs it always sees www.aviatorsauction.com. I have used both $_SERVER['SERVER_NAME']; and $_SERVER['HTTP_HOST']; Any help would be greatly appreciated. Thank you and God bless, Johnathan Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/ Share on other sites More sharing options...
crabfinger Posted January 8, 2010 Share Posted January 8, 2010 Try this and see if it works, p.s. switch is there for a reason <?php function get_css($baselink='../templates/tsgcalpha/css/',$hostname=NULL) { $hostname = $hostname === NULL ? $_SERVER['SERVER_NAME'] : $hostname; switch($hostname) { case "www.agcooponline.com": case "www.agliberty.com": case "www.harvestauction.com": case "www.harvestauction.net": case "www.harvestauction.org": case "www.harvestauction.biz": case "www.harvest2market.com": $css_file = "harvestauction.css"; break; case "www.hobbiesauction.com": $css_file = "hobbiesauction.css"; break; case "www.toolsforauction.com": case "www.tooleagle.com": case "www.tooltwister.com": $css_file = "toolsforauction.css"; break; case "www.aviatorsauction.com": $css_file = "aviatorsauction.css"; break; } return $baselink . $css_file; } ?> <head> <link rel="stylesheet" href="<?php print get_css(); ?>" type="text/css" /> </head> Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-990770 Share on other sites More sharing options...
johnathanamber Posted January 8, 2010 Author Share Posted January 8, 2010 @crabfinger,I will certainly give that a try later. Curious though, the code that I posted, can that work? Thank you and God bless, Johnathan Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-991350 Share on other sites More sharing options...
crabfinger Posted January 8, 2010 Share Posted January 8, 2010 @crabfinger,I will certainly give that a try later. Curious though, the code that I posted, can that work? Thank you and God bless, Johnathan I'm sure yours could work, I don't know because I would never write something that long to do something so easy. oh and try this before the code print $_SERVER['SERVER_NAME'] Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-991355 Share on other sites More sharing options...
johnathanamber Posted January 9, 2010 Author Share Posted January 9, 2010 @crabfinger, THank you for the solution, it works so far, I will delve more into it this weekend. I have not used switch a ton as of yet... we'll see what happens. I am really curious though why my previous code didn't work. Thank you and God bless, Johnathan Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-991588 Share on other sites More sharing options...
tomdchi Posted January 9, 2010 Share Posted January 9, 2010 That is some crazy code you got there. It didn't work because your really not doing anything. Your opening and closing your php tags and setting the $site var but thats it. In order for it to work right you would need it to look like. <?php if ($site = "www.agcooponline.com") { echo '<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />'; } elseif ($site = "www.agliberty.com") { echo '<link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" />'; } // etc.... ?> If you had to code it this way you would use if elseif instead of all if's. That way when it finds a match it stops looking and doesn't have to spend time searching through the others. But crabfinger is right, this is the sort of thing switch was made for. Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-991600 Share on other sites More sharing options...
optikalefx Posted January 9, 2010 Share Posted January 9, 2010 your just not echoing out. <?php if ($site = "www.agcooponline.com") { ?> <link href="../templates/tsgcalpha/css/harvestauction.css" rel="stylesheet" type="text/css" media="all" /> <?php } ?> should be <?php if ($site = "www.agcooponline.com") { ?> echo "<link href='../templates/tsgcalpha/css/harvestauction.css' rel='stylesheet' type='text/css' media='all'>" /> <?php } ?> But please don't do this, re write it, your code is very very messy. Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-991608 Share on other sites More sharing options...
crabfinger Posted January 9, 2010 Share Posted January 9, 2010 to the last two posters you will notice that after the if statement the op uses a php closing tag so that if the data is selected properly it will display that html. I do it all the time when I don't want to put huge amounts of code into a variable. What the op was having a problem with was not that it was not displaying, in fact it was, it was just displaying the wrong one. Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-991721 Share on other sites More sharing options...
johnathanamber Posted January 18, 2010 Author Share Posted January 18, 2010 Hey everyone, OK, what was posted works great! Thanks guys. I actually used the same code to switch the Google Analystics code for each URL. That also works well. <?php function get_css($baselink='../templates/tsgcalpha/css/',$hostname=NULL) { $hostname = $hostname === NULL ? $_SERVER['SERVER_NAME'] : $hostname; switch($hostname) { case "www.agcooponline.com": case "www.agliberty.com": case "www.harvestauction.com": case "www.harvestauction.net": case "www.harvestauction.org": case "www.harvestauction.biz": case "www.harvest2market.com": $css_file = "harvestauction.css"; break; case "www.hobbiesauction.com": $css_file = "hobbiesauction.css"; $analytics = "hbaanalytics.html"; break; case "www.toolsforauction.com": case "www.tooleagle.com": case "www.tooltwister.com": $css_file = "toolsforauction.css"; break; case "www.aviatorsauction.com": $css_file = "aviatorsauction.css"; break; } return $baselink . $css_file; } ?> I have one more question... I need the $baselink to show the URL WITHOUT the 'www.'. What is the best way to do this? The reason is due that if you enter the URL without the 'www.' then the CSS files, and also the Google Analystics, won't load. This obviously won't work. Thank you and God bless, Johnathan Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-996956 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 OP: There was nothing MAJORLY wrong with your first code.. The only thing you did wrong was the use of '=' and not '==' in your if statements. = is for assignment, whereas == is for comparision so essentially you were saying if ($site = 'somesite') which in ALL cases is true because you are assigning 'somesite' to $site; I bet if you had of viewed your output source code ALL of the stylesheets would have been included onto your page.. All the other comments saying that you aren't echoing anything and all your doing is opening and closing PHP tags is incorrect. Your code WOULD have worked, although it is long, drawn out and messy. crabfingers solution is alot cleaner.. Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-996961 Share on other sites More sharing options...
johnathanamber Posted January 18, 2010 Author Share Posted January 18, 2010 @Buddski, Thank you for the info... any ideas on my last question... I have one more question... I need the $baselink to show the URL WITHOUT the 'www.'. What is the best way to do this? The reason is due that if you enter the URL without the 'www.' then the CSS files, and also the Google Analystics, won't load. This obviously won't work. And you were correct about all of the code displaying on the site at the same time. Thank you and God bless, Johnathan Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-997018 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 You could just substr the first 4 characters off I guess.. $baselink = substr($baselink,4); it might not work on some servers but again Im not 100% certain.. Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-997023 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 $newurl = substr($baseurl, 4, strlen($baseurl)); echo $newurl; // Example result: site.com/test.html That should extract anything after the www. Note it must contain " www. " or it will overwrite the url. You can check for the www using strpos if required. Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-997024 Share on other sites More sharing options...
crabfinger Posted January 19, 2010 Share Posted January 19, 2010 Unless when you are executing the function you pass a different $baselink to the function other than the default you shouldn't have a www. at the front Quote Link to comment https://forums.phpfreaks.com/topic/187667-autochoosing-a-css-file-based-on-url/#findComment-998123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.