work_it_work Posted January 22, 2009 Share Posted January 22, 2009 Hey, I'm trying to develop an website with web announcements and i'm stuck... It's a php based website. Example of what i want to do: Visitor go to website, search for an announcement, finds it, then see all the details in the announcement_details_page, on this page is a link "save this announcement" which stores the announcement to that computer if it's clicked. Assuming the visitor save a few announcements. Then, other day he returns to the website, goes to "My saved announcements" page, and there finds the list with of the saved announcements, he may want to visit again or just simply delete one, two or all of them . Also if one announcement is no longer available the link for that particular announcement is no longer active and it displays something like (announcement expired) If the visitor clear his web browser data (history, cookies, other stuff), the "My saved announcements" page will be empty. As far as i can see this is based on a cookie session. Can anyone help me with this? Any already made scripts over the internet, just ready for use? any idea, anyone did this before? please help! It's one of the last things i have to make to the website so i can just put it online. Thanks in advance!!! Quote Link to comment Share on other sites More sharing options...
Zhadus Posted January 22, 2009 Share Posted January 22, 2009 Link it to his account in your database. If you don't have them login, then it really can't be saved indefinitely. Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 23, 2009 Author Share Posted January 23, 2009 it can be done without log-in session. i've seen this on the web, but i don't know how to do it here is an example: http://suchen.mobile.de/fahrzeuge/details.html?id=97775697 just click park vehicle, then you will have it saved and it can be viewed in "my car park" section no log-in needed . . . Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 23, 2009 Author Share Posted January 23, 2009 any suggestions? no one did this before? Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 anyone? please help! have mercy Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 Just use a cookie to reference the announcements id Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 ok, but how to do that? don't you have an example or something? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 <?php $seven_days = (60*60*24*7); $a_id = 1;//this would be your announcement id setcookie("saved_announcement", $a_is, time()+$seven_days); Does that make any sense? look here for more info on php and cookies Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 don't you have a elaborated example ? please Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 Without seeing any code from how you display or find your announcments there's not too much i can do Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 i think it's working... i got something into my cookie there was an error here with $a_id and $a_is: <?php $seven_days = (60*60*24*7); $a_id = 1;//this would be your announcement id setcookie("saved_announcement", $a_is, time()+$seven_days); changed to <?php $seven_days = (60*60*24*7); $a_id = 1;//this would be your announcement id setcookie("saved_announcement", $a_id, time()+$seven_days); now i must figure it out how to get the $a_id from cookie and post it into a my_saved_listing page... any idea on that? Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 and i want to set the cookie life-time forever or at leas 12 months what's the combination then? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 <?php if(isset($_COOKIE['saved_announcement')) { echo $_COOKIE['saved_announcement'];//prints the value of the cookie if it's set } seven days was 60*60*24*7 60 seconds * 60 minutes * 24 hours * 7 days 60*60*24*365 365 days Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 great!! it's working thank you very much for your help trough here. the other problem is that i only can store 1 $a_id in cookie... example: i have saved first announcement that has $a_id = 25 and in cookie i have the 25 stored. if i go to other announcement which has $a_id = 3 and click save, the cookies is modified, and i olny have $a_id = 3 instead of both $a_id = 3 and $a_id = 25... any idea how to store multiple $a_id`s in cookie ? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 Personally I'd build the string like a CSV (comma seperated values) so if you have a saved cookie and want to add another run the following code; <?php //adding another announcement $new_a_id = 10; if(isset($_COOKIE['saved_announcements'])){ $a_ids = explode(",", $_COOKIE['saved_announcements']); forech($a_isd as $a_id_check) { if($new_a_id == trim($a_id_check)) { header("Location: a-page-because-that-announcement-is-already-saved.php"); exit; } } $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id"; $year = (60*60*24*365); setcookie("saved_announcement", $new_a_array, time()+$year); } Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 ok, i understand, but how can i use this script? replace with the first one or put it below the first one? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 <?php //adding another announcement $new_a_id = 10; $year = (60*60*24*365); if(isset($_COOKIE['saved_announcements'])){ $a_ids = explode(",", $_COOKIE['saved_announcements']); forech($a_isd as $a_id_check) { if($new_a_id == trim($a_id_check)) { header("Location: a-page-because-that-announcement-is-already-saved.php"); exit; } } $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id"; setcookie("saved_announcement", $new_a_array, time()+$year); } else { setcookie("saved_announcement", $new_a_id, time()+$year); That would be your code for saving so you can replace the old one with that (enabling you to store multiple announcments) Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 here is the save_listing.php file content: <?php //adding another announcement $new_a_id = $_GET['id']; $year = (60*60*24*365); if(isset($_COOKIE['saved_announcements'])){ $a_ids = explode(",", $_COOKIE['saved_announcements']); forech($a_isd as $a_id_check) { if($new_a_id == trim($a_id_check)) { header("Location: a-page-because-that-announcement-is-already-saved.php"); exit; } } $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id"; setcookie("saved_announcement", $new_a_array, time()+$year); } else { setcookie("saved_announcement", $new_a_id, time()+$year); ?> and the url that commands the save_listing page and psot the id number is : localhost/save_listing.php?id=30 (i've put a value manual just to see it works) now the surprise..... Parse error: syntax error, unexpected T_AS in C:\wamp\www\tataee\tatae\save_listing.php on line 7 first time worked fine the first little script... now it doesn't .... Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 Speeling mistake on my part, but to learn you really want to start doing the detective work yourself.... Line 7 - forech($a_isd as $a_id_check) { it should read - foreach($a_isd as $a_id_check) { Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 works now, but it has the same result as first one... the id is replaced in cookie instead of adding a comma separator and the new id after it.... :-\ Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 ok, change it to this anjd see what prints on the screen; <?php //adding another announcement $new_a_id = 10; $year = (60*60*24*365); if(isset($_COOKIE['saved_announcements'])){ $a_ids = explode(",", $_COOKIE['saved_announcements']); foreach($a_isd as $a_id_check) { if($new_a_id == trim($a_id_check)) { header("Location: a-page-because-that-announcement-is-already-saved.php"); exit; } } $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id"; var_dump($new_a_array); exit; setcookie("saved_announcement", $new_a_array, time()+$year); } else { setcookie("saved_announcement", $new_a_id, time()+$year); Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Remember, cookies cannot be accessed till a page reload to show the new version. Also remember to use the domain and path parameters in setcookie also to get accurate results each time. You may try unsetting the original cookie then resetting the new like so: <?php //adding another announcement $new_a_id = $_GET['id']; $year = (60*60*24*365); if(isset($_COOKIE['saved_announcements'])){ $a_ids = explode(",", $_COOKIE['saved_announcements']); forech($a_isd as $a_id_check) { if($new_a_id == trim($a_id_check)) { header("Location: a-page-because-that-announcement-is-already-saved.php"); exit; } } $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id"; setcookie("saved_announcement", $new_a_array, time()-$year); setcookie("saved_announcement", $new_a_array, time()+$year); } else { setcookie("saved_announcement", $new_a_id, time()+$year); ?> And see if that helps out. Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 26, 2009 Author Share Posted January 26, 2009 i get the same results with the last script... it replaces the old id with a new one... to see the cookie content i use "Temporary Internet Files" folder and open the cookie each time after i execute the url, and i see the progress. i have no idea about this : "Also remember to use the domain and path parameters in setcookie() also to get accurate results each time." my cookie looks content look like this: AM_language en localhost/tataee/tatae/ 1536 1947570560 29988765 2083161920 29982730 * saved_announcement 3333335 ==> this is the row that changes every time when i execute the url with new id... localhost/tataee/tatae/ 1536 4002539648 30056187 1980308448 29982762 * Quote Link to comment Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 It may be worth a try to follow this: http://www.aeonity.com/frost/php-setcookie-localhost-apache It describes how to setcookies while using localhost since they are touchier. And I am not sure how accurate the temp files will be, but yea. Quote Link to comment Share on other sites More sharing options...
gevans Posted January 26, 2009 Share Posted January 26, 2009 Have you changed the id that you're sending, because the script checks to see if the id exists, and if it does it wont 're-add' it (you dont want the same announcement saved twice). Remember, cookies cannot be accessed till a page reload to show the new version. Also remember to use the domain and path parameters in setcookie also to get accurate results each time. You may try unsetting the original cookie then resetting the new like so: <?php //adding another announcement $new_a_id = $_GET['id']; $year = (60*60*24*365); if(isset($_COOKIE['saved_announcements'])){ $a_ids = explode(",", $_COOKIE['saved_announcements']); forech($a_isd as $a_id_check) { if($new_a_id == trim($a_id_check)) { header("Location: a-page-because-that-announcement-is-already-saved.php"); exit; } } $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id"; setcookie("saved_announcement", $new_a_array, time()-$year); setcookie("saved_announcement", $new_a_array, time()+$year); } else { setcookie("saved_announcement", $new_a_id, time()+$year); ?> And see if that helps out. Those parameters are option, though deleteing and re-assigning the cookie as premsio has done in the code above is a good idea. Quote Link to comment 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.