theycallmepj Posted April 24, 2006 Share Posted April 24, 2006 I have set up this script to change the size of an image on our website, the size of the image depends on the users screen resolution width. The script first looks to see if there is a cookie set, if not it runs this java script. The javascript detects the screen resolution, sets a cookie, and then refreshes the page. It sets a cookie so the page does not have to refresh every time the user visits the page.[code]<?if(isset($HTTP_COOKIE_VARS["users_resolution"])) $screen_res = $HTTP_COOKIE_VARS["users_resolution"];else //means cookie is not found set it using Javascript{?><script language="javascript"><!--writeCookie();function writeCookie(){ var today = new Date(); var the_date = new Date("December 31, 2023"); var the_cookie_date = the_date.toGMTString(); var the_cookie = "users_resolution="+ screen.width; var the_cookie = the_cookie + ";expires=" + the_cookie_date; document.cookie=the_cookie location = '/';}//--></script><?}Some Code....//Script that takes the users screen width and displays image accordinglyif ($screen_res < 1024)//If the screen width is less than 1024 this will shrink the image{ echo "<center><img width=300 src=\"random/rotator.php\"></center>";} else {//This is for screen widths greater than 1024 echo "<center><img src=\"random/rotator.php\"></center>"; }?>[/code]My problem is, if a user has cookies disabled, the page just keeps refreshing. Is there a way where if it refreshes three times it will set [code]$screen_res = 1024[/code]Thanks Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/ Share on other sites More sharing options...
rab Posted April 24, 2006 Share Posted April 24, 2006 Theres some things that need fixed.[code]<?if(isset($HTTP_COOKIE_VARS["users_resolution"])) $screen_res = $HTTP_COOKIE_VARS["users_resolution"];else //means cookie is not found set it using Javascript{?>[/code]Your leaving an open bracket. If you want the javascript code to be executed then you should do...[code]<?if(isset($HTTP_COOKIE_VARS["users_resolution"])){ $screen_res = $HTTP_COOKIE_VARS["users_resolution"];}else //means cookie is not found set it using Javascript{echo '<script language="javascript"><!--writeCookie();function writeCookie(){var today = new Date();var the_date = new Date("December 31, 2023");var the_cookie_date = the_date.toGMTString();var the_cookie = "users_resolution="+ screen.width;var the_cookie = the_cookie + ";expires=" + the_cookie_date;document.cookie=the_cookielocation = "/";}//--></script>';}?>ect...[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30237 Share on other sites More sharing options...
theycallmepj Posted April 24, 2006 Author Share Posted April 24, 2006 Sorry, I have all the brackets correct in my web page, I missed it when I copied it to here Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30238 Share on other sites More sharing options...
rab Posted April 24, 2006 Share Posted April 24, 2006 It still doesnt work? Try $_COOKIE['screen res']; Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30248 Share on other sites More sharing options...
theycallmepj Posted April 24, 2006 Author Share Posted April 24, 2006 [!--quoteo(post=368131:date=Apr 24 2006, 04:05 PM:name=rab)--][div class=\'quotetop\']QUOTE(rab @ Apr 24 2006, 04:05 PM) [snapback]368131[/snapback][/div][div class=\'quotemain\'][!--quotec--]It still doesnt work? Try $_COOKIE['screen res'];[/quote]Yes it works, if the user has cookies enabled, it works great. But if the user has cookies disabled, it keeps trying to find a cookie when there isn't one, and if there isn't a cookie, then it tries to set one, but if the user has cookies disabled then it can't set a cookie, then the script keeps repeating its self. Like an infinite loopI need a way to make it so when the user has cookies disabled, it will set $screen_res to 1024. I don't know if there is a script to detect that, or a script to make it only loop 3 times and then just set $screen_res to 1024. Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30254 Share on other sites More sharing options...
rab Posted April 24, 2006 Share Posted April 24, 2006 run it through a loop then when you go to check it again, if the cookie is set, use that screen res. If not use a default or just load the certin image everytime Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30271 Share on other sites More sharing options...
theycallmepj Posted April 25, 2006 Author Share Posted April 25, 2006 How would I go about setting a loop up instead of if and else statements Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30682 Share on other sites More sharing options...
rab Posted April 25, 2006 Share Posted April 25, 2006 [code]<?function check_res(){ if(isset($HTTP_COOKIE_VARS["users_resolution"])) // checks { return 0; }else{ // if not found then setting it echo '<script language="javascript"><!--writeCookie();function writeCookie(){var today = new Date();var the_date = new Date("December 31, 2023");var the_cookie_date = the_date.toGMTString();var the_cookie = "users_resolution="+ screen.width;var the_cookie = the_cookie + ";expires=" + the_cookie_date;document.cookie=the_cookielocation = "/";}//--></script>'; if(isset($HTTP_COOKIE_VARS["users_resolution"])) // checking if it was set { return 0; }else{ return 1; } }return 1;}if(check_res() == 0){ $screen_res = $HTTP_COOKIE_VARS["users_resolution"];}else{//use default}?>[/code]Not the best thing, but its something i whipped up in 2 minutes Quote Link to comment https://forums.phpfreaks.com/topic/8292-stop-loop-with-cookies/#findComment-30697 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.