I want to set a cookies to get the users only vote once in 4 categories (sad, enraging, funny, cool). I'm a beginner so I know my code is very inconvenient but it is my first try and so it doesen't matter to me if I have a few lines more or less. For some reason the browser won't load the vote.php files (that are called when the user clicks one voting option). I just can't find the error...maybe someone sees it immediatly? [Before I inserted the cookie thing the code files loaded perfectly, so the calling isn't the matter.]
<?php
require_once('connect.php');
$newsid = $_POST['id'];
if (isset($_POST['submit'])) {
if(isset($_COOKIE['votecookie']) && $_COOKIE['votecookie'] !== 'sad') {
if($_COOKIE['votecookie'] == 'cool') {
$query = mysql_query('UPDATE `index` SET cool=cool-1 WHERE id = {$newsid}');
}
if($_COOKIE['votecookie'] == 'funny') {
$query = mysql_query('UPDATE `index` SET funny=funny-1 WHERE id = {$newsid}');
}
if($_COOKIE['votecookie'] == 'sad') {
$query = mysql_query('UPDATE `index` SET enraging=enraging-1 WHERE id = {$newsid}');
}
setcookie('votecookie', 'sad');
$query = mysql_query('UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'');
$hosts = $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = 'news.php?id={$newsid}&sad=1';
header('Location: http://$hosts$uris/$extras');
exit;
}
if(!isset($_COOKIE['votecookie']) {
setcookie('votecookie', 'sad');
$query = mysql_query('UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'');
$hosts = $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = 'news.php?id={$newsid}&sad=1';
header('Location: http://$hosts$uris/$extras');
exit;
}
if(isset($_COOKIE['votecookie']) && $_COOKIE['votecookie'] == 'sad') {
$hosts= $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = 'news.php?id={$newsid}&sad=0';
header('Location: http://$hosts$uris/$extras');
exit;
}
}
else {
$hosts= $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = 'news.php?id={$newsid}&sad=0';
header('Location: http://$hosts$uris/$extras');
exit;
?>
If someone is interested in the answer: This is how I did it now (I had a few more mistakes in my code and an error in reasoning).
<?php
require_once('connect.php');
$newsid = $_POST['id'];
if (isset($_POST['submit'])) {
if(isset($_COOKIE[$newsid]) && $_COOKIE[$newsid] != 'sad') {
if($_COOKIE[$newsid] == 'cool') {
$query = mysql_query("UPDATE `index` SET cool=cool-1 WHERE id = '{$newsid}'");
}
if($_COOKIE[$newsid] == 'funny') {
$query = mysql_query("UPDATE `index` SET funny=funny-1 WHERE id = '{$newsid}'");
}
if($_COOKIE[$newsid] == 'enraging') {
$query = mysql_query("UPDATE `index` SET enraging=enraging-1 WHERE id = '{$newsid}'");
}
$query = mysql_query("UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'");
setcookie($newsid, "sad", time() + (10 * 365 * 24 * 60 * 60));
$hosts = $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = "news.php?id={$newsid}&sad=1";
header("Location: http://$hosts$uris/$extras");
exit;
}
if(!isset($_COOKIE[$newsid])) {
$query = mysql_query("UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'");
setcookie($newsid, "sad", time() + (10 * 365 * 24 * 60 * 60));
$hosts = $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = "news.php?id={$newsid}&sad=1";
header("Location: http://$hosts$uris/$extras");
exit;
}
if(isset($_COOKIE[$newsid]) && $_COOKIE[$newsid] == 'sad') {
$hosts= $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = "news.php?id={$newsid}&sad=0";
header("Location: http://$hosts$uris/$extras");
exit;
}
}
else {
$hosts= $_SERVER['HTTP_HOST'];
$uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extras = "news.php?id={$newsid}&sad=0";
header("Location: http://$hosts$uris/$extras");
exit;
}
?>