...

View Full Version : Cookies



Mikael
08-22-2006, 11:56 AM
When I click on a number, I like it to switch from 0 to 1 or vice versa. Now both numbers gets switched. Also, when I reload the page, the numbers switch.

I only like the number that I click on to switch. And reloading shouldn't affect the numbers. I think that looking at the code simply explains what I try to do.

What am I doing wrong?

The filename is test.php

<html>
<body>

<?php function uSwitch($cid)
{
if (isset($_COOKIE[$cid]))
if ($_COOKIE[$cid]=="0")
setcookie($cid, "1", time()+7200);
else
setcookie($cid, "0", time()+7200);
else
setcookie($cid, "1", time()+7200);
}
?>

<?php function uPrint($cid)
{
if (isset($_COOKIE[$cid]))
echo $_COOKIE[$cid];
else
echo '0';
}
?>

Welcome!<br>
<br>
user1: <a href="test.php" onClick="<?php uSwitch("u1"); ?>">
<?php uPrint("u1");?></a><br>
user2: <a href="test.php" onClick="<?php uSwitch("u2"); ?>">
<?php uPrint("u2");?></a>

</body>
</html>

Fumigator
08-22-2006, 03:06 PM
You're trying to call a php function from a clientside event (onclick). PHP doesn't work like that.

How PHP works (http://www.php.net/manual/en/introduction.php)

You could always just use Javascript to manipulate your cookies, if you have no other reason to use PHP.

Just one of many Google results (http://www.webreference.com/js/column8/functions.html)

mlseim
08-22-2006, 03:27 PM
All I can think of is using two files ...

This file first checks for an existing cookie, or starts it out at zero.
It then calls the next script that sets or resets. You need to have
that second script otherwise the variable is retained and a refresh will
change it back. This way, a refresh will not affect the values.

This one I called "test2.php" ...

<?php
session_start();
function uPrint($cid){
$id=$_SESSION[$cid];
if(!$id){echo "0";}
else{echo $_SESSION[$cid];}
}
?>
<html>
<body>

Welcome!<br>
<br>
user1: <a href="test3.php?cid=u1">
<?php uPrint("u1");?></a><br>
user2: <a href="test3.php?cid=u2">
<?php uPrint("u2");?></a>


</body>
</html>

This is the second file ... the one that does the switching ...

This one I called "test3.php" ...

<?php
session_start();
$cid = $HTTP_GET_VARS['cid'];

if($_SESSION[$cid] == 0){
$_SESSION[$cid]=1;
}
else{$_SESSION[$cid]=0;
}
header ("location: test2.php");
?>

Mikael
08-22-2006, 06:13 PM
mlseim, your files aren't working :/ but I had good use of your idea! ^^ thanks =)

test.php



<?php function uPrint($cid)
{
if (isset($_COOKIE[$cid]))
echo $_COOKIE[$cid];
else
echo '0';
}
?>

<html>
<body>
Welcome!<br>
<br>
user1: <a href="test2.php?cid=u1&num=<?php uPrint("u1");?>"><?php
uPrint("u1");?></a><br>
user2: <a href="test2.php?cid=u2&num=<?php uPrint("u2");?>"><?php
uPrint("u2");?></a>

</body>
</html>



test2.php



<?php
$num=$_REQUEST["num"];
$cid=$_REQUEST["cid"];
if (isset($cid))
{
if($num=="1")
setcookie($cid,"0",time()+7200);
else
setcookie($cid,"1",time()+7200);
}
header ("location: test.php");
?>

mlseim
08-23-2006, 02:40 PM
Mikael ...

Glad it gave you an idea. Perhaps the "session" is not
configured the same for you as it is for me (with my webhost).

The key is having two files.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum