Cache Problem I think

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Cache Problem I think

Post by edhen »

HI,

i seem to have a slight problem in regards to my script. In my site i have a flash header version and a html version. I also have a button up top to revert to either flash or html depending whether or not the users ip is in the DB, The way it works is someone presses the button to view the site in html and it will add there ip in the database then it will check wether or not there ip is listed and if so then it will skip the flash html code and display the html code then if they click revert to flash then there ip will be deleted from the db and display the flash header and ignore the html version code....

Ok so my problem is that when a user clicks revert to html for example it will, then if they click a link it will sometimes revert back to flash then i press refresh then it will go back to html as it should. i check the db and it works as normal.. it seems the output has a mind of its own.. so is this a cache problem on either client or server side? and if so how could i overcome this?... btw i have played with it on my local server and it was working fine there, it seems to be my external site that has this issue..
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Cache Problem I think

Post by Jonah Bron »

Sounds like it might be. To check, try disabling caching with:

Code: Select all

header('Cache-Control: no-cache, must-revalidate');
Just a note about your implementation. IP addresses change, so that way is not very reliable. Using cookies would be better I think. Plus, you don't have to deal with the load of searching the database every time someone loads a page.
User avatar
myister
Forum Newbie
Posts: 12
Joined: Tue Sep 28, 2010 7:03 am
Location: Ohio
Contact:

Re: Cache Problem I think

Post by myister »

Agreed! Cookies would the way to go.
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: Cache Problem I think

Post by edhen »

Thanks for the fast response... the reason i chose db instead of cookies was clearly because i never actually used cookies before (mind you ive been playing around with php for a while but never went out my way to experiament with cookies Till now lol :) ) also there would only be a few listed in the db which would be cleaned every month and i wouldnt think it would cause too much load to check a couple of rows, it was really about the user on my site at the time and i guess ppl with dynamic ip's could be the same as cookies set to off on ppls browser (which i do understand is rare aswell)... but after your input i decided to head in the cookies direction and start experiamenting with them and the results are just as good (and cleaner quick coding)... But the problem still exists and i find that it is only IE causing the problem mentioned above, also i find with these cookies set it wont actually be in effect untill the page is reloaded. Say i click HTML version then it will set the cookie but will still display the Flash version untill i navigate somewhere else or refresh the page (actually ill have to goto the address bar and press enter to refresh as it would ask me to resend the data with the standard way of refreshing)...

so at the end of the day i find that IE is caching the pages, but turning of caching altogether would cause problems as I dont want the flash header to have to relead on every page. also with the cookies, is there a way to refresh the page or make it in efffect on the same script load? or will i have to post the form (button) to an external script then send them back? I hope this makes sense ill send the code upon request if it would help your understanding off my situtaion...

thanks again!

EDIT: sorry i took the header cache wrong and it does infact fix the problem, but this only leaves the problem with having to refresh after clicking the button..

EDIT 2: Ok so i got it 100% working by using

Code: Select all

header("Location: ". $_SERVER['PHP_SELF'] ); 
but is this actually the clean way of doing it? Kinda doesnt seem so to me...
User avatar
myister
Forum Newbie
Posts: 12
Joined: Tue Sep 28, 2010 7:03 am
Location: Ohio
Contact:

Re: Cache Problem I think

Post by myister »

I recently ran into a problem some what like this with a Video project I was working on. I was using the no-cache header and then soon for out that IE doe no honor the no-cache header, But they do honor the time limit that can be placed on a cache. Though that info may not help have you tried cleaning your cache on your local machine before trying again.

Cookies are really easy to use. Here is an idea may be an over kill but it should work. Keep in mind I am not much more experienced in PHP as well.

1. Someone goes to your site and is found that that have to choose between flash and html.
-Option 1 They clcik HTML which will give them a cookie called 'htmluser'. Then they are redirected to the index page where another check is made before it loads.
( This code is not accurate just an example )

Code: Select all

if (issest($_cookie['htmluser']));{

echo "Your are viewing HTML";
   } else {
echo "You are viewing Flash";
   }
Correct me if I am wrong but a cookie can be update or overwritten, so you could even add a link on very page that could change it from Flash to HTML and Vis Vesa.

EDIT: Just something else i was thinking about. If The user dos have cookies disabled for what ever reason then they would have to do extra work just to veiw the site. instead of using cookie you could use sessions as well. This would eliminate and trouble on the user end and make the site do the work just in case a user does not have cookies enabled. Sessions are very easy to use as well and almost like cookies but stored on server side.! Just om thoughts I was having
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: Cache Problem I think

Post by edhen »

Thanks for your input myister.. your code example is how i implemented my function... You are right about cookies being easy too use... it didnt take me long to work with them and have it working... I am familiar with sessions and i have used them on a number of occasions and will more than likely use that as a backup for the cookies with in the if statement.. so ppl with cookies enable will have both a cookie and session (unless we can determine if there browser has cookie enabled or not then i could make another statement to choose the right option for the user, but i dont think this is possible but i could be wrong) then when they leave and come back they will still be remembered to what there preference was... but ppl with out cookies will still have the session through out the time there browser is opened but will have to reclick html version upon each revisit and i guess this is a users preference and knows the consequences of having cookies off. 9/10 times i wouldn't find ppl needing to revert to HTML version as modern internet speeds are adequite to most content on the internet. But this is just a little neat feature i wish to intergrate in my site for both learning and making a robust site. Thanks again and your help is appreciated, Im just hoping theres no more bugs in such a short simple script sometimes a long script i will find easier to debug lol.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Cache Problem I think

Post by Jonah Bron »

You said you had to refresh the page. That's probably because you might have put the checking code for the cookie before setting the cookie.

I think you should just stick with cookies. If users disable functionality (ie cookies), they disable functionality (ie choice memory).
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: Cache Problem I think

Post by edhen »

Hi Jonah,

I just removed the header just to double check and make sure but it still requires a page refresh. I have set the cookie before the check and this has to be true because i am doing the check inside the html tag where as the set cookie is during the application load before the template, ie. i click form button to html/flash which the action is php self then the script will check that its been set then it will either add the cookie or remove the cookie (depending on selection) then the template loads while the content checks whether or not cookie is set or not to display either a flash header or html header.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Cache Problem I think

Post by Jonah Bron »

Maybe you need to create another variable that you set when you set the cookie, and check it too with OR (||). That way, if that variable is true OR the cookie is set, display the HTML header.
edhen
Forum Newbie
Posts: 21
Joined: Fri Jul 16, 2010 11:26 am
Location: Adelaide, Australia

Re: Cache Problem I think

Post by edhen »

Ok after much brain storming and tiredness, I think i may just stick with how i have it atm with the header location code to refresh the page as it even say on php.net that cookies are available on next page load.. I am also going with jonah direction and only using cookies... there's a number of reasons why i decided to do it like this, Because 1. its a small feature that shouldn't need more processing power than it needs, 2. its cleaner and easier to read for me then having more if's and else when the same result was achieved by the other way, 3. people would select html upon entry to home page making the page refresh almost irrelevant. I also played around with the idea of the variable but i was just getting tired off playing around with it trying to get the variable outside the class etc. BUT i even tried it outside a class and just put it in the if statement and still was getting weird results and wasn't adding up too me (maybe im a bit too tired and will prob be banging my head tomorrow in stupidity) besides it works with how it is and it looks cleaner... but your help is much appreciated and i believe using a variable backup for a more advance script would be more adequate...


Again thanks!
Post Reply