Drupal Cache & Stylesheet Switch - drupal

I'm using a very simple Stylesheet Switch by php. It was fine all along but days ago I turn on Caching mode and now it only work for login user. If turn off Caching mode, it will work again for both user.
Basically the code looks like this
In the page.php header
<?php
if(isset($_COOKIE['style'])){
$style=$_COOKIE['style'];
} else {
$style='green';
}
?>
<link type="text/css" rel="stylesheet" href="/css/<?php echo $style ?>.css">
It switch by
Blue
In the switch.php
<?php setcookie('style', $_GET['style'], time()+31536000);
header('Location:'.$HTTP_SERVER_VARS['HTTP_REFERER']);
?>
I did many research but couldn't find the right way. Please help if you can. Thank you

Hmm, I don't see why you can't just use a client-side style switcher, as in http://www.alistapart.com/articles/alternate/. There are other methods of doing it purely client-side, but it seems a bit overkill to request an entire new page to switch styles.
Also, caching creates a static page to serve up in lieu of dynamically creating a new page for every hit, so the cached page is probably getting served up to whoever isn't getting the style switching.

From my understanding/experience standard drupal caching is only for non-logged in users. There is at least one module that allows for authenticated user caching, but it's not in Core, authcache:
http://drupal.org/project/authcache
An old article that explains druapl caching techniques. Still has some good information in there: http://n0tablog.wordpress.com/2007/11/19/drupal-caching/

Related

XAMPP - Does not load latest changes to Style Sheet

I'm building a website which I am using XAMPP to test. I am really struggling with the design because whenever I have refreshed the localhost webpage in the browser, the design changes do not seem to have been applied. Last time I finished implementing, I closed XAMPP and my HTML/CSS editor, and when I've opened them and loaded the page again, some of the changes that had previously not been applied were now applied to the page.
I tried to change these values again and refresh the page, to see if it was finally working as expected, and the latest changes failed to apply again.
I have literally no idea what is going on, but it would seem that the changes can only be applied when XAMPP is newly started and the page is loaded for the first time. Can anyone suggest why this might be?
Thanks in advance,
Mark
You could try a cache busting technique where you include your CSS file. Add a ?v=<some numbers> at the end of your CSS file's location.
Something like:
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/stackoverflow/all.css?v=e031e80c3d8b">
You could use PHP's time function to generate the hash for you. As it's a Unix timestamp it will change each time you refresh.
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/stackoverflow/all.css?v=<?php echo time(); ?>">
Though I wouldn't consider this a long term solution. Without additional info it's hard to make any possible XAMPP related suggestions.
As mentioned in the comments this may be a cache problem. You can use this simple PHP code to add timestamp version automatically without having to set numbers each time you make an update:
Define the timestamp variable
<?php
$timestamp = date("YmdHis"); // output: 20150715164614
?>
Add timestamp to files
<link rel="stylesheet" type="text/css" href="mystyle.css?v=<?php echo $timestamp;?>">
Alternatively, you can disable caching altogether by sending these PHP headers
header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
quite an annoying little problem with numerous threads with suggestions for solutions/workaround. Emptying the browser cache (Safari) worked for a while for me but simply restarting the Apache servers from the Xampp manager after making the CSS stylesheet changes seemed to work best. I repeated a couple of times to test and after It even seemed to get the developer tools window to work on the fly consistently which it was not doing in XAMPP at all prior. Hard refresh appeared to have no effect interestingly, hope it helps!
I was having this issue too and I'm posting this answer as soon as I found the solution. Having read this thread before (thanks to the earlier answers provided), I knew it had something to do with the cache. Glad I found it now
Access your Developers tools through Inspect and select the Network menu toolbar above. Next, click on Disable cache and reload your webpage.. TADA!!!
Unfortunately, it seems I cannot post images
rename the folder and change path in your css file

how to block css from external access

Someone had try to steal my css file to use at his site. Can i somehow block CSS file from external access, but not damage my site? Somehow through htaccess or something. Thanks for your advice, any help appreciated.
Generate your css with PHP like style.css.php and in the code accept a token which refreshes every second. Your main page will include it using the current token src="style.css.php?token=abc123". If the token is valid, it serves it up. If the token is expired, it doesn't.
Dumbest solution ever. Endless workarounds, but might help against that guy who wants to steal your CSS. Worth a try and a good laugh.
Step 1: Create a table css_security_force in your database with one column token
Step 2: Create a cron that runs every second to update the token (now THAT's secure):
UPDATE css_security_force SET token = md5( NOW() );
Step 3: In your PHP head grab the token from the css_security_force table and set it as a variable $token then reference that token in the CSS link:
<link rel="stylesheet" href="style.css.php?token=<?=$token?>" />
Step 4: In your style.css.php file, grab the token from the database and check it against the query param.
if ($_GET['token'] == $token) {
echo <<<CSS
body {
background-color : yellow;
color : pink;
}
CSS;
} else {
echo 'Stop stealing my CSS!!!!!!';
}
Sit back and watch your foe's plans crumble before him.
EDIT
I got curious and made a working demo (Took it down after a couple days of 1 second db updates for sanity purposes, but you can easily recreate with the source posted below).
Here is the source on Github
No. This makes no sense. Your CSS file must be available to anyone who can view your site if you want your pages styled.
Who cares if someone uses your style?
Simply put you cannot block it because the browsers have to have access to them. It is just one of those things you should not worry about. If it were possible, many sites like Facebook and Twitter would be blocking it.

How to include HTML contents from another site? I have access to both sites

I have a site which is using DNN (DotNetNuke) as a content management system. I am using another site for my event registrations. I have sent them my template; which displays the basics including a hover menu with many different items in it.
Issue is - as I update the menu on my site using DNN, I need it to be reflected on the site using my template - without me having to send them a new file. Anyone have suggetsions on how to approach this?
I don't want to send the events provider all of the DNN DLLs as well as my database login information in order to render the menu.
I created a page on my site that is something like 'menu.aspx' - this produces the menu in HTML format, however it has tags like in it that I'd like to remove before serving it to them.
What is the best approach for this? Do I need to write a custom server control using XMLHttp? Can I accomplish this in Javascript?
Any advice much appreciated.
Thank you!
If both sites are hosted on the same domain (eg site1.domain.com and site2.domain.com) you can use JavaScript and XmlHttpRequest to insert code from one site to another. Otherwise, the Same Origin Policy prevents you from using AJAX.
If they're not on the same domain but you have access to the page on their website, you can simply include there a JS script from your site :
<script type="text/javascript" src="http://yoursite.com/code.js"></script>
In the JS, simply document.write() what you want on the page. This way, you can easily change the content of the page on their site without having to send them a new file.
Finally, you can also use an iframe on their site, pointing to a page on yours.
EDIT: As Vincent E. pointed out, this will only work if they're on the same domain - my bad.
If you are unwilling or unable to use frames, then I would set up an ashx on your DNN server which renders the menu (if you've got it in a user control all the better, as you can just instatiate it and Render it directly to the output stream) and then just make an Ajax call to that from your events page and insert it directly into the DOM.
Here's a quick and hacky jquery-based example of the events page end of things:
<script type="text/javascript">
function RenderMenu(data)
{
$('#Menu').html(data);
}
$(document).ready(function() {
$.ajax({
type : 'GET',
url : 'http://localhost/AjaxHandlers/Menu.ashx',
data : '',
success : RenderMenu,
});
});
</script>
You'll want an empty div with the ID 'Menu' on the page where you want your menu to sit, but apart from that you're good to go.
If for whatever reason you can't get the menu HTML in an isolated way, then you'll need to do some text processing in RenderMenu, but it's still do-able.
I am not a web expert, so don't shoot me.
Can't you just put their registration form into an iFrame in DNN ?

XHTML template programming

Its been a while since I've done any hard core web design, so I have a question for you. I'm pretty sure that there is a feature of xhtml that allows you to create a template and include that template in your other xhtml pages. What I'm trying to do is create a "main links" template that allows any page to reference the main pages of the site. I figured a template would be better to use as it would allow easy editing of those links. If I were to just hard code them on all the pages, that would be a nightmare if I had to change something.
Btw, this site is only being done in xhtml, css and javascript. I could have used something more beefy like jsf, but I just didn't feel like it.
If anyone has any ideas on this, or suggestions on a better way to do it, it would be great.
Thanks,
Robbie
You're probably looking at doing something with includes, which isn't a part of XHTML but rather a server-side technology such as PHP or ASP.
See this W3School's articles on PHP include() and require() and ASP Including files.
As far as I know, there isn't anything in XHTML, CSS and/or JavaScript that does what you want.
You could extend the DTD with entities (as described at http://xml.silmaril.ie/authors/includes/) but you'll find browser support as poor as it is for XInclude.
If you want to use any kind of templating, then you need to look to other technologies which you can set up to output XHTML rather then doing anything intrinsic to XHTML itself (at least if you want to approach the problem sanely).
http://allmyfaqs.net/faq.pl?Include_one_file_in_another is a good summary of the options available.
Do you mean something like asp.net master pages?
You can use AJAX or Serverside includes to include parts of your template. However, XHTML does not support this by default. You have to write code yourself. Most websites have a template which is included serverside.
In php you can do this:
page.php
<?php ob_start(); ?>
your page
<?php
$content = ob_get_contents();
ob_end_clean();
$title="pagetitle";
require("template.php");
?>
template.php
Use <?php echo $title; ?> where you want your title
Use <?php echo $content; ?> where you want your content
If you really want to stay away from server side technologies that can offer the include functionality you want, perhaps you can find an IDE / editor that supports templates or includes or something similar. I have no experience with this myself, so I cannot recommend specific software, but I know that there are IDEs / editors that offer the functionality you look for.

How do tools like the web dev toolbar get the entire css file of a site?

The web dev toolbar for Firefox is quite an impressive tool.
What I am completely clueless about is how does this tool get the css stylesheet file of a site? This is hosted on a host which is secure etc. I am completely stumped.
I must be thinking about this in all the wrong way.
Thanks
The client (in this case Firefox) has to download the CSS file in order to render the page correctly. The plugin (in this case Firebug) simply requests the proper URL and the browser it gets it--most likely from the cache. There is no real mystery here.
In every HTML file, there's a link to the CSS stylesheet, or else the browser wouldn't know where to find it, thus losing the ability to render the page correctly.
It's in the form of <link rel="stylesheet" type="text/css" href="theme.css">,
I'd like to add that regardless of whether the host is 'secure' or not, it still is presenting the file to the client.
Unless, of course, you're looking at a XML file. Then you need to consult the XSL which'll tell you where the stylesheet is.

Resources