Jump to content

problem with cookies


arbitter

Recommended Posts

I really feel like I'm spamming this forum, with my many questions. Though I've really searched this one out, and can't find the problem.

 

Setting the cookie is no problem, I'll give you the files:

 

cookie.php

<?php
include('header.php');
?>
<for action='cookie2.php' method='post'>
<input type="radio" name="kleur" value="green" /> Groen
<br />
<input type="radio" name="kleur" value="blue" /> Blauw
<br />
<input type="submit" />
</form>

</html>

 

 

cookie2.php

<?php
$kleur = $_POST['kleur'];
$expir = time()+ 60 * 60 * 24 * 60;
setcookie("uploadskleur",$kleur,$expir);

?>

 

 

This is all good, because when I check my browsers cookies I see the right value.

 

Now when I retrieve the cookie data, all goes fine too!

I fI echo the cookie, I get 'green' or 'blue'.

 

Now, I want to implement this in a file, and here's the real problem:

<?php
include('../header.php');
$sort2 = $_GET["sort"];
$_SESSION["sort"] =  $_GET["sort"];
$sort = $_SESSION["sort"];

$_SESSION["month"] = $_GET["month"];

if(isset($_COOKIE['uploadskleur'])){
$dekleuris = $_COOKIE['uploadskleur'];
$kleurachtergond= $dekleuris;}

else{
$kleurachtergrond = '#cdaae7';}
$kleurletter = 'white';
?>

 

This "$kleurachtergrond" I will use later on in the page to show which items are active.

So if there's no cookie set, no problem, the second "$kleurachtergrond" shows (it's a light-purple). But If my cookie is set, it is transparant. I've tried doing it with eg #111111 , I've tried with or without apostrophes, I've tried without the semilicons ( {}, and ofcourse then I wrote one single line after the if), I've tried many possibilities, and I can't seem to get to the solution...

The if(isset[blabla)) works, because I've tried it with solely echoing the cookie and that worked..

 

This is my first cookie, so I was delighted that my browser got a cookie from my site, and I would be even more delighted if it worked!

Link to comment
Share on other sites

Looks like you have typed the variable name wrong on this line:

 

$kleurachtergond= $dekleuris;

 

Should it be $kleurachtergrond like it is in the ELSE section? So the whole thing is

 

include('../header.php');

$sort2 = $_GET["sort"];
$_SESSION["sort"] =  $_GET["sort"];
$sort = $_SESSION["sort"];

$_SESSION["month"] = $_GET["month"];

if(isset($_COOKIE['uploadskleur']))
{
$dekleuris = $_COOKIE['uploadskleur'];
$kleurachtergrond= $dekleuris;
}
else
{
$kleurachtergrond = '#cdaae7';
}

$kleurletter = 'white';

Link to comment
Share on other sites

if you encounter this kind of problem

try to debug the problem by print out the variables

try to copy and paste the variable from the error line

 

debugging is trying to look for value, if they are the proper value

 

 

the best programmer is not coding, they are debuggers

try to find out the run time problem, and solve them

coding is simple, finding problem it complex, try to learn to be a debugger (tester)

 

the main role of debugging is look for correct values in the variable, (use copy paste from error line, and echo that variable in php, echo all variable from that line)

Link to comment
Share on other sites

Oh my bad, I put in the wrong file here.

 

<?php
include('../header.php');
$sort2 = $_GET["sort"];
$_SESSION["sort"] =  $_GET["sort"];
$sort = $_SESSION["sort"];

$_SESSION["month"] = $_GET["month"];

if(isset($_COOKIE['uploadskleur'])){
$dekleuris = $_COOKIE['uploadskleur'];
$kleurachtergond= $dekleuris;}

else{
$kleurachtergrond = '#cdaae7';}
$kleurletter = 'white';
?>

 

SO no problem there

 

 

I also see I mde a type in my <form>, but it is correct here. Sorry about that

 

 

 

And at the last reply; this is weird, when i type

if(isset($_COOKIE['uploadskleur'])){
$dekleuris = $_COOKIE['uploadskleur'];
$kleurachtergond= 'white';}

else{
$kleurachtergrond = '#cdaae7';}
$kleurletter = 'white';
?>

 

The colour isn't white but it's also transparant...

So I guess there's something wrong with my if and else...

But what then?

Link to comment
Share on other sites

What the hell is this?

 

$sort2 = $_GET["sort"];
$_SESSION["sort"] =  $_GET["sort"];
$sort = $_SESSION["sort"];

 

Shorter and easier to read:

 

$sort = $_SESSION["sort"] =  $_GET["sort"];

 

The same goes for:

 

$dekleuris = $_COOKIE['uploadskleur'];
$kleurachtergond= $dekleuris;

 

$kleurachtergond = $_COOKIE['uploadskleur'];

 

PS I know about "copy-on-write" still doesn't mean it can't be clean.

Link to comment
Share on other sites

What the hell is this?

 

$sort2 = $_GET["sort"];
$_SESSION["sort"] =  $_GET["sort"];
$sort = $_SESSION["sort"];

 

Shorter and easier to read:

 

$sort = $_SESSION["sort"] =  $_GET["sort"];

 

The same goes for:

 

$dekleuris = $_COOKIE['uploadskleur'];
$kleurachtergond= $dekleuris;

 

$kleurachtergond = $_COOKIE['uploadskleur'];

 

PS I know about "copy-on-write" still doesn't mean it can't be clean.

 

Yeah I know..

But I'm rescripting all my pages, and then I recode it all (check everything, see if I can shorten anthing, if something is useles, if something is missing, put it in a good lay-out,..) and then it should all be better :P Because that was written in the beginning days of my scripting and I wanted to be sure it worked, so using $_POST[] and $_GET[] made me feel more sure :P

Link to comment
Share on other sites

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.