Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/97291-how-the-cookie-crumbled/
Share on other sites

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))
}

 

 

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.

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.