0

I'm trying to set a main page to reload with a query string so it doesn't come from a cached file. the page is index.php, but i'd like to, whenever the page loads, hava a unique string appended to it (like the answer in the comments by Rocket to a previous question of mine suggests).

I'd like to integrate it into the php that authenticates logins.

Is something like this a good idea

<?php

require_once('login-config.php');

//use javascript cookie to detect if the page is coming from hash
//and if so, redirect to a new url with a ?<?=time()?> query sting

if ( !isset$($_COOKIE['login_cookie'] ) 
{

//render login-form page

} 
else 
{

//redirect to the content page

}

The javascript cache/cookie check from this page (as follows)

var uniqueKey = '<%= SomeUniqueValueGenerator() %>';
var currentCookie = getCookie(uniqueKey);
var isCached = currentCookie !== null;
setCookie(uniqueKey); //cookies should be set to expire 
                      //in some reasonable timeframe

I suppose I should do something like this,

//best guess is that I update this uniqueKey every time I update the page
//and want it not to load from cache
var uniqueKey = 'v1.1';
var currentCookie = getCookie(uniqueKey);
var isCached = currentCookie !== null;

if (!isCashed){      
    setCookie(uniqueKey);  
    window.location'?".time().";'
} else {
    window.location'?".time().";'
}

but I am a little unsure exactly how to bring it all together.

Thanks

Community
  • 1
  • 1
1252748
  • 14,597
  • 32
  • 109
  • 229
  • Why do you want to use javascript? You can do this in the PHP code. – Scott Saunders May 31 '12 at 15:32
  • I don't have any preference where it gets done. I just have some users that cannot access the page, though, once they've cleared the cache, the problem is gone. I'm looking for the easiest way to get their browsers _not_ to load from cache. def open to any other suggestions! – 1252748 May 31 '12 at 15:34

2 Answers2

1

This won't necessarily stop the browser from caching the page, your better off sending no cache headers...

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
fire
  • 21,383
  • 17
  • 79
  • 114
0

I believe this PHP code will do what you want:

if ( !isset$($_COOKIE['login_cookie'] ) 
{
    //render login-form page
}
else 
{
    //redirect to the content page
    header("Location: index.php?t=" . time());
    exit();
}
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • that will work for the page i'm redirecting to, but i'm talking about even giving the index a ?query_string so that when they just go to the main page for login, typing for example www.site.com/dir/ they actually get taken to www.site.com/dir/index.php?847283479 thanks! – 1252748 May 31 '12 at 15:43
  • If you want the page to never be cached, use the no cache headers. fire's answer covers them. – Scott Saunders May 31 '12 at 15:47