Forum Moderators: coopster

Message Too Old, No Replies

Setting cookies

         

dkin

4:57 am on May 16, 2005 (gmt 0)

10+ Year Member



I am confused on this certain situation, I would like to set cookies but only if there is no table row resembling the same values.

So, if the cookies are already set, the script ends, if they are not and there is no row I want a row inserted and cookies set, If the cookies are not set but there is a table row the script exits.

This is my full script so far. The part giving me problems is the cookies at the end.


<?php
error_reporting(E_ALL);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Cookie Test</title>
</head>
<body>
<?php
$dbHost = "";
$dbUser = "";
$dbPass = "";
$dbName = "";
//connect to the mysql server
$link = @mysql_connect($dbHost, $dbUser, $dbPass);
//return success or failure of connection
if(!$link)
{
//report error
echo "Could not connect to server";
exit;
}
//select the database
if(!@mysql_select_db($dbName))
{
//report error
echo "Could not select database";
exit;
}
#Find users ISP
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
#Connect to rewards table
$uresult = mysql_query("SELECT * FROM rewards", $link) or die ("query 1: " . mysql_error());
$urow = mysql_fetch_array($uresult);
#Begin countdown function
function getTimeUntil($currentTime, $destTime)
{
$timeDiff = $destTime - $currentTime;
if ($timeDiff >= 1)
{
$numHours = floor($timeDiff/60/60);
$hourRemainder = $numHours % (60 * 60);
$numMinutes = floor($timeDiff/60);
$minuteRemainder = $numHours % 60;
$numSeconds = $timeDiff;
if ($numHours >= 1)
{
$output = $numHours . ' hours ';
if ($hourRemainder >= 1)
{
$output .= ($numMinutes % 60) . ' minutes ';
}
} else {
$output = $numMinutes . ' minutes ';
}
$retArray = array('total_hours' => $numHours,
'total_minutes' => $numMinutes,
'total_seconds' => $numSeconds,
'output' => $output);
return $retArray;
}
}
$timeToWait = getTimeUntil($urow['date'], time());
$aFormattedString = $timeToWait['output'];
#Check if all cookies are set
if (isset($stat1) && isset($stat2) && isset($stat3) && isset($stat4))
{
#Display elapsed time if row is found
echo 'Only '.$aFormattedString.' have passed, please wait 24 hours.';
exit;
}
#Check if user has been written to db for clicking same user link
if ($urow['isp'] == $hostname && $urow['ip'] == $_SERVER['REMOTE_ADDR'] && $urow['rewarded'] == $rewarded)
{
#If so activate
#Display elapsed time if row is found
echo 'Only '.$aFormattedString.' have passed, please wait 24 hours.';
exit;
#Close initial If Statement.
}
else
{
#If not, write to the database
$date = time();
$wiresult = mysql_query("INSERT INTO rewards (ip, rewarded, date, isp, agent) VALUES ('$_SERVER[REMOTE_ADDR]', '$rewarded', '$date', '$hostname', '$_SERVER[HTTP_USER_AGENT]')", $link);
if (isset($wiresult))
{
echo 'User Rewarded.';
setcookie("stat1", "$_SERVER[REMOTE_ADDR]", time() + 86400);
setcookie("stat2", gethostbyaddr($_SERVER['REMOTE_ADDR']), time() + 86400);
setcookie("stat3", "$_SERVER[HTTP_USER_AGENT]", time() + 86400);
setcookie("stat4", "$rewarded", time() + 86400);
exit;
}
}
?>
</body>
</html>

Stormfx

5:09 am on May 16, 2005 (gmt 0)

10+ Year Member



A cookie is a header call. You cannot print text before sending a cookie unless you're using output buffering. Either move the echo statement after the cookie functions, or enable output buffering.

dkin

5:17 am on May 16, 2005 (gmt 0)

10+ Year Member



output buffering?

dcrombie

11:41 am on May 16, 2005 (gmt 0)



output buffering [google.com]