mrheff Posted March 21, 2008 Share Posted March 21, 2008 hi guys im a relative novice and have a had good rummage through previous posts befor begging ahem asking for help, the problem lies herein, im making a small cart system for my bros shop i wrote a review system for it thats okies and got my product catalog all in place and have now moved on to the frontend fun, i figure a cart is pretty simple and plotted away to go ahead relying a table for each user, i didnt want to have them make an account befor buying anything so wanted to generate an id for the cart/basket table entry using their ip and random string of alpha numerics (ensuring the id would not be picked up by someone elses) place this in a cookie so i didnt have to use massive urls etc,i also wanted the site to remember the cookie for a week or so, incase they came back at any point. so i got about making the cookie setting in a statement a lil like this (artists impression) if (isset $_cookie[basketid]) { $yourbasketid = $_cookie[basketid]); } else { echo "ohw your a new user lets create a basket fo you bitch"; setcookie(basketid,$ip.$randomcode,time (bloody ages)) } i got my version of this going ok, since it was self reliant i could test it standalone... i did much to my pleasure hazzaaahhh it was picking me giving my id made by the ip and the rand string, but not giving it to me if if refreshed, then it would show me the basketid i was assigned, untill resetting my cookies, brilliant i thought..... .... but no. when i included the file to my header ready to develop my basket/cart system it would only pick up the variables but not assign them... ... sucky... i could go to the standalone file set the cookies and then pick them up from the page which i included from but never have them be set by the page i needed to the job. I checked all the read a writes, all 777 for testing,, should be fine i thought, my next thought was ok maybe it cant be included so inserted the code into the page from which it was called, no avail so next step is ask the guys here, any thoughts would be much appreciated. hmm my header file is also included, ill try it my index.php and report back // <-- just trried no joy. thanks for your time, heff. Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/ Share on other sites More sharing options...
Northern Flame Posted March 21, 2008 Share Posted March 21, 2008 I may be wrong, but every time i see a cookie called, the word cookie is always in caps, i dont know if thats mandatory, but you may need to change that. You also need quotes around basketid, also, you never set a time on the cookie, if you want a week, do this: if (isset $_COOKIE['basketid']) { $yourbasketid = $_COOKIE['basketid']); } else { echo "ohw your a new user lets create a basket fo you bitch"; setcookie('basketid',$ip.$randomcode,time+60*60*24*7, (bloody ages)) } Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497889 Share on other sites More sharing options...
mrheff Posted March 21, 2008 Author Share Posted March 21, 2008 i will give that a bash mate thanks that was reet quick leek. Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497897 Share on other sites More sharing options...
mrheff Posted March 21, 2008 Author Share Posted March 21, 2008 no joy with that im afraid, thanks though man. here is my full code, the idea is scan for cookie, if no cookie then give variable (ip + key) to make basketid this code works fine when i use it as a page, when i include it like this include (cookie.php); it can pick the cookie up when it is set however it refuses to set it if there is not one preasent, any ideas guys? <?php if(isset($_COOKIE['lastVisit'])) { $last = $_COOKIE['lastVisit']; echo "Welcome back! <br> You last visited on ". $last; echo "".$basketid.""; } else { $Month = 2592000 + time(); //this adds 30 days to the current time $ip = $_SERVER['REMOTE_ADDR']; //get correct ip setcookie(lastVisit, date("F jS - g:i a"), $Month); function createRandomPassword() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } $key = createRandomPassword(); $ipkey = $ip.$key; setcookie(basketid,"$ipkey" , $Month); echo "hmm weve set up your basket cookie"; } ?> thank ye kindly for your time. Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497946 Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 I think you have to set it like this: setcookie('lastVisit', date("F jS - g:i a"), $Month); Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497948 Share on other sites More sharing options...
mrheff Posted March 21, 2008 Author Share Posted March 21, 2008 ok i ammended that to it, with no ill effects however it still only works whe i acces the file cookie.php but refuses when its included, wierd huh? anybody think there is another way to include thatlets this work?? Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497951 Share on other sites More sharing options...
mrheff Posted March 21, 2008 Author Share Posted March 21, 2008 maybe i should send it to cookie.php if none is set, then do one of the header location() type deals back to the page once the cookie was set? whatcha wreckon? Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497952 Share on other sites More sharing options...
mrheff Posted March 22, 2008 Author Share Posted March 22, 2008 my workaround seems to work ok, but casues a screen flikker on firstload,which is a tad ugly and im sure any gui expert would say "err the user would think something is going wrong ere", alas it will surfice, If any one <em>does</em> have any ideas i'd really appreciate to hear your suggestions i might try to set the cookie earlier on a through page so as the screen does so much flikker as stall as it loads this might look a touch better, Cheers for your time and please just hit me up with any ideas. Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497977 Share on other sites More sharing options...
mrheff Posted March 22, 2008 Author Share Posted March 22, 2008 so for those of you who might like to know the way i bodged my way through this problem here goes this part goes in the page you want to see if theres a basket set up or not <? if(isset($_COOKIE['lastVisit'])) { echo "".$basketid.""; } else { print "<meta http-equiv=\"refresh\" content=\"0;URL=cookje.php\">"; echo "one moment please, one of our slaves is just fetching you a basket";// displays as the page stalls to refresh new url } ?> and heres the cookje.php that does the work and wont let me use it the way iwant.. i broke up my orignal so it was only the if clause left in cookje.php, and put the actual else if clause (which is above) in the index as explained earlier.. <? if(isset($_COOKIE['lastVisit'])) { echo "".$basketid.""; } else { print "<meta http-equiv=\"refresh\" content=\"0;URL=cookje.php\">"; echo "one moment please, one of our slaves is just fetching you a basket";// displays as the page stalls to refresh new url } ?> sorry abou the spacing i unashamedly ripped that part and havent formatted it, sorry. so that was rubbish workaround,,, at least it works i suppose, but its still really bugging me why i couldnt just include it, this is my last deperate please, give a man a hand guys, we all know this is a bodge, ad no one wants to be a cowboy, yeeehaaaw, all yours quick-draw-cincinatti-heff Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497986 Share on other sites More sharing options...
rofl90 Posted March 22, 2008 Share Posted March 22, 2008 time is time()+60*60*24*7*52 = a year. Quote Link to comment https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/#findComment-497988 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.