ghurty Posted August 8, 2007 Share Posted August 8, 2007 This is a out of my depth, and I am not sure if it is even php related, but perhaps someone can help. I have a script that calculates sunset and sunrise based on a premade list of cities. As of now, I have it that it defaults to New York, and a user can change it to display a different city's info. But it does not save this information. Is there a way, (perhaps by using a cookie) that I can have it remember the users last location? Thanks The code I have so far to default it to New York: if (isSet($_REQUEST["activelocation"])) { $activelocation = $_REQUEST["activelocation"]; } else { $activelocation = "New York, NY"; Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/ Share on other sites More sharing options...
ToonMariner Posted August 8, 2007 Share Posted August 8, 2007 http://uk.php.net/manual/en/function.date-sunrise.php Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318010 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 Yep as u said u can use cookies. if(isset($_COOKIE['location'])){ $location = $_COOKIE['location']; //if the cookie is set, get the value } else{ setcookie('location', 'new york', time()+60*60*24*30); //set the cookie to the default value and //make it expire after aprox. 1 month } Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318011 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 Thank You, From my understanding, this tells it to read the cookie, and if there is no cookie, then to put the default location (new york). But how do I tell it to write a cookie if the user changes to a different city from the dropdown list I have premade. Thanks Dropdown list: <form name="mainform" action="index.php" method="get"> <input type="hidden" name="option" value="com_luach" /> <select name="activelocation" onchange="submit();"> <?php fillLocationList($activelocation); ?> </select> Yep as u said u can use cookies. if(isset($_COOKIE['location'])){ $location = $_COOKIE['location']; //if the cookie is set, get the value } else{ setcookie('location', 'new york', time()+60*60*24*30); //set the cookie to the default value and //make it expire after aprox. 1 month } Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318016 Share on other sites More sharing options...
tibberous Posted August 8, 2007 Share Posted August 8, 2007 Before anything else... if($_GET['activelocation']) setcookie('location', $_GET['activelocation'], time()+60*60*24*30); Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318030 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 So I set it up like this if($_GET['activelocation']) setcookie('location', $_GET['activelocation'], time()+60*60*24*30); if (isSet($_COOKIE['location'])){ $activelocation = $_COOKIE['location']; //if the cookie is set, get the value } else{ setcookie('location', 'New York, NY', time()+60*60*24*30); //set the cookie to the default value and //make it expire after aprox. 1 month However, now what it does is when I choose a new location it reloads the page, but doesnt put the new location their, rather, the last location I had, and then when I change it again, it does the one I had just had it on!? Thanks Before anything else... if($_GET['activelocation']) setcookie('location', $_GET['activelocation'], time()+60*60*24*30); Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318047 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 make smth like: if($_GET['activelocation']) $activelocation = $_GET['activelocation']; setcookie('location', $activelocation, time()+60*60*24*30); } elseif(isset($_COOKIE['location'])){ $activelocation = $_COOKIE['location']; //if the cookie is set, get the value } else{ setcookie('location', 'New York, NY', time()+60*60*24*30); } So u make progressive checking. If the user has changed the location then set the cookie. If not the first then check if the cookie exists to set the location. If not any of both set the cookie to its default value. Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318055 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 When I try inserting that, it now gives me "Parse error: parse error, unexpected '}'" in this line: } elseif(isset($_COOKIE['location'])){ Thank you make smth like: if($_GET['activelocation']) $activelocation = $_GET['activelocation']; setcookie('location', $activelocation, time()+60*60*24*30); } elseif(isset($_COOKIE['location'])){ $activelocation = $_COOKIE['location']; //if the cookie is set, get the value } else{ setcookie('location', 'New York, NY', time()+60*60*24*30); } So u make progressive checking. If the user has changed the location then set the cookie. If not the first then check if the cookie exists to set the location. If not any of both set the cookie to its default value. Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318060 Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 When I try inserting that, it now gives me "Parse error: parse error, unexpected '}'" in this line: count the { it should match the } Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318061 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 i forgot the '{' if($_GET['activelocation']){ Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318064 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 edit: sorry double posted Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318065 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 Thank you. That worked. But now I am running into two problems. 1) If I erase the cookie so that it is as if it is the first time a user is accessing the site, it gives an error, and does not display the information, HOWEVER, once i refresh, it will the display properly. 2) Once the cookie is set, and a user visits the site, it displays everything properly, but gives the error: Notice: Undefined index: activelocation in ...... HOWEVER, if I attempt to change the city using the drop down list, then no error appears at all. Thank You for having patience with me. For the record, this is the current code if($_GET['activelocation']){ $activelocation = $_GET['activelocation']; setcookie('location', $activelocation, time()+60*60*24*30); } elseif(isset($_COOKIE['location'])){ $activelocation = $_COOKIE['location']; //if the cookie is set, get the value } else{ setcookie('location', 'New York, NY', time()+60*60*24*30); } Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318072 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 Well I fixed the first problem by adding the following inserting after this: } else{ setcookie('location', 'New York, NY', time()+60*60*24*30); This" $activelocation = "New York, NY"; But I am still having the second problem, I know what causing it, but how do I tell it to on run that line, only if a change was made? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318083 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 Write both the errors u get and which lines they belong to. For the second error be sure u have the right name for the $_GET['activelocation']. Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318084 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 I got rid if the first error, but not the second. The error is: Notice: Undefined index: activelocation in /...../...php on line 32 which refers to this line: if($_GET['activelocation']){ The error only happens in some cases, for example. If I am just accessing the page, it displays the error, (but continues to load the page) however, if I then go and choose from the drop down list a new city, it loads the page perfectly fine with out the error. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318090 Share on other sites More sharing options...
Fadion Posted August 8, 2007 Share Posted August 8, 2007 change it to: if(isset($_GET['activelocation'])){ Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318091 Share on other sites More sharing options...
ghurty Posted August 8, 2007 Author Share Posted August 8, 2007 That did it! Thanks a million. Quote Link to comment https://forums.phpfreaks.com/topic/63812-solved-how-to-use-the-script-to-read-what-user-last-choice-was/#findComment-318092 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.