Page 1 of 1

Cookie problem, cookie doesn't want to be used again.

Posted: Fri Aug 30, 2002 2:56 am
by Skywalker
I want to see if the cookie still is ther, if it's not ther enymore ther then it has to make a cookie, else it must use the old one that has been made earlier.

My problem is, how can I see if the Cookie still ther or not?

What do I have to use, isset or empty or what ever?
I want to do this with php.

if Cookie is ther then use it, else go to main page to re enter the user name en make a new cookie.

Who can help me with this?

Posted: Fri Aug 30, 2002 2:59 am
by twigletmac
If your cookie is called 'mycookie' then to check if it is set you would do something like:

Code: Select all

if (isset($_COOKIEї'mycookie'])) {
    // cookie is set
} else {
    // cookie not set
}
Mac

Posted: Fri Aug 30, 2002 3:02 am
by Skywalker
Ok thenk you, I am going to try it out :D

Posted: Fri Aug 30, 2002 3:22 am
by Skywalker
I now have this, but it doesn't make a cookie when ther is no cookie? what is wrong?

Code: Select all

<?php 

$NewNaam=$_POSTї"NewNaam"];

if (isset($_COOKIEї'Naam'])) {
$Naam = "$NewNaam";


} else {

	$NewNaam=$_POSTї"NewNaam"]; 
 	setcookie("Naam",$NewNaam,time()+40000,"/test/");
	$Naam = "$NewNaam"; 
}


?>

Posted: Fri Aug 30, 2002 3:54 am
by twigletmac
Try this and refresh the page each time because (setcookie() info):
php manual wrote:Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter.

Code: Select all

<?php  

$NewNaam = $_POSTї'NewNaam'];
//setcookie('Naam', '', time()-1000);

if (isset($_COOKIEї'Naam'])) { 
	$Naam = $NewNaam; 
	echo 'cookie exists';
} else { 
   $cookieset = setcookie('Naam', $NewNaam, time()+40000); 
   if (!empty($cookieset)) {
	   $Naam = $NewNaam;  
	   echo 'new cookie set';
   } else {
	   echo 'error - cookie could not be set';
   }
} 

?>
If you want to delete the cookie again just uncomment the

Code: Select all

//setcookie('Naam', '', time()-1000);
line.

Mac

Posted: Fri Aug 30, 2002 4:28 am
by Skywalker
The problem now is, the cookie is made, it is still ther, but when you are plasing an order and you want to go back to the order list, It only shows that the cookie exists and doesn't show the name enymore, wher it should be shown. The name must be shown again, after a second visit.

Her is my compleet sorce code.


<?php

$NewNaam = $_POST['NewNaam'];

//setcookie('Naam', '', time()-1000);

if (isset($_COOKIE['Naam'])) {
$Naam = $NewNaam;
echo 'cookie exists' ;
} else {
$cookieset = setcookie('Naam', $NewNaam, time()+40000);
if (!empty($cookieset)) {
$Naam = $NewNaam;
echo 'new cookie set';
} else {
echo 'error - cookie could not be set';
}
}


// Hier wordt Cookie gemaakt als hij nog niet bestaant
?>


<?php /*-------------------------------------------------------------------------------------------------------------------*/?>

<?php /*-------------------------------------------------------------------------------------------------------------------*/?>

<?php require_once('Connections/Verbinding.php'); ?>
<?php
mysql_select_db($database_Verbinding, $Verbinding);
$query_Recordset2 = "SELECT * FROM nieuws ORDER BY NieuwsID DESC";
$Recordset2 = mysql_query($query_Recordset2, $Verbinding) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

mysql_select_db($database_Verbinding, $Verbinding);
$query_Recordset1 = "SELECT * FROM produkten ORDER BY NaamProdukt ASC";
$Recordset1 = mysql_query($query_Recordset1, $Verbinding) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<?php /*-------------------------------------------------------------------------------------------------------------------*/?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>| Bikkertjes - intranet website |</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body background="Interface-bestellijst.jpg" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<table width="96%" border="0">
<tr>
<td colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td height="31" colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="4"> <div align="left"><strong></strong></div></td>
<td width="132"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Main
menu</strong></font></td>
<td colspan="3"><strong></strong></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td height="754" colspan="2" valign="top"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">-
<a href="index.htm">Main</a></font><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><br>
<font color="#FFFFFF">- <a href="nieuws.php" target="_self">Nieuws</a><br>
- <a href="Frietzak.php" target="_self">Frietzak</a><br>
- <a href="product-list.php" target="_self">Bestellen</a><br>
- <a href="login.htm" target="_self">Admin</a></font></font></td>
<td width="4" valign="top">&nbsp;</td>
<td width="475" valign="top">
<p>&nbsp;</p>


<p><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Hallo

<?php ($_COOKIE["Naam"] && !$_GET[$Naam]); //Cookie ophalen met functie $_Cookie["Naam"] && !$_GET[Variable ophalen]
print ("<font face=\"verdana\" size=1 color=\"white\"> $Naam\n"); //Print de Variable $Naam op beeldscherm.
?>

</strong></font></p>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td bgcolor="#000000"><table width="475" border="0" cellpadding="1" cellspacing="1">
<tr bgcolor="#333333">
<td width="128"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">ProduktID</font></td>
<td width="168"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">NaamProdukt</font></td>
<td width="108"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">Prijs</font></td>
<td width="82"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">Bestel</font></td>
</tr>
<?php do { ?>
<tr>
<td bgcolor="#333333"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_Recordset1['ProduktID']; ?></font></td>
<td bgcolor="#666666"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_Recordset1['NaamProdukt']; ?></font></td>
<td bgcolor="#666666"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row_Recordset1['Prijs']; ?></font></td>
<?php /*-------------------------------------------------------------------------------------------------------------------*/?>
<td bgcolor="#666666">
<?php

$item_id= $row_Recordset1['ProduktID']; //Haalt ProduktID op uit recordset en stopt hem in $item_id
$user_name= $Naam; //Haalt ProduktNaam op uit recordset en stopt hem in $item_name

echo "<font face=\"verdana\" size=1 color=\"white\" ><a href='http://localhost/test/bestellijst.php?P ... >Bestel</a>";

?>

</td>
<?php //Maakt een link die zich zelf herhaalt op moment van uitvoeren. Parameters zijn $item_id en $item_name ?>
<?php //De parameters worden mee gestuurd naar de pagina bestellijst.php daar zullen ze worden opgehaald met $_POST ?>
<?php /*-------------------------------------------------------------------------------------------------------------------*/?>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<td width="475" valign="top">&nbsp;</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td height="10" colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td height="10" colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td height="10" colspan="2">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
</table>
</body>
</html>

<?php
mysql_free_result($Recordset2);

mysql_free_result($Recordset1);
?>

Posted: Fri Aug 30, 2002 8:19 am
by mikeq
Okay,

Your line $NewNaam = $_POST['NewNaam']; is getting a variable passed from a form I believe, second time someone goes to the page there will be nothing in it as that wasn't passed.

Next

if (isset($_COOKIE['Naam'])) {
$Naam = $NewNaam;
echo 'cookie exists' ;

Yes the cookie exists but you are setting $Naam = $NewNaam and $NewNaam doesn't have anything in it.

Try $Naam = $_COOKIE['Naam'];

I think you will find that works.