PHP COOKIES help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
refugee13
Forum Newbie
Posts: 2
Joined: Tue Dec 28, 2004 9:08 am

PHP COOKIES help

Post by refugee13 »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have the following page which is a basic voting script, utilizing PHP COOKIES to determine if the user has voted already (avoidind multiple votes).  However, when installed, the votes will not "take", meaning the page just refreshes and nothing happens.  This is after clearing my cookies in browser.  Is there a problem with my Cookie establishment? (thanks for your time in advance!)

Code: Select all

<?

session_start();

// Including config

include "include/config.php";



// Including AdoDB

include $adodb_path.'adodb.inc.php';

$conn = ADONewConnection ($basetype);

$conn->Pconnect ($host, $log, $pass, $db) or die("I cannot connect to the database cause ". $conn->ErrorNo(). " " . $conn->ErrorMsg());   



if (!empty($_SESSION['logged'])) {

	// Ïðîâåðêà âûñòàâëåííûõ êóêîâ äëÿ ôîòîêîíêóðñà è âûñòàâëåíèå ôëàãîâ

	if (isset($_COOKIE['pcontest']) && isset ($_POST['ic_id']) && isset($_POST['mark'])) {

		$mark = $_POST['mark'];

		$ic_vote = $_POST['ic_id'];

		$str_photo = $_COOKIE['pcontest'] . "," . $ic_vote;

		$q = $conn->Execute ("update $icons_table set mark=mark+$mark, votes=votes+1 where icon_id='$ic_vote'");

		if (!mysql_error()) {

			setcookie ("pcontest", $str_photo, time()+3600*24*365);

			$message = "Thank you for your vote!";

			$u = $conn->Execute ("update $icons_table set rate=mark/votes where icon_id=$ic_vote");

		} else {

			$message = "Error happened during DB access! Contact administrator.";

		}



	} elseif (isset($_COOKIE['pcontest'])) {

			$str_photo=$_COOKIE['pcontest'];

	} else { 

		$str_photo="0";

		setcookie ("pcontest", "0");

	}



	header("Pragma: no-cache");

	header("Cache-Control: no-cache, must revalidate");

}

?>



<?

if (!empty($_POST['login']) && !empty($_POST['pass'])) {

	$check = $conn->Execute ("select count(*) from $users_table where login='".$_POST['login']."' and password=password('".$_POST['pass']."') and approve='1'");

	if ($check->fields[0] == '1') {

		$lu = $conn->Execute ("select user_id, name from $users_table where login='".$_POST['login']."' and password=password('".$_POST['pass']."')");

		$_SESSION['id_user'] = $lu->fields['user_id'];

		$_SESSION['logged'] = $_POST['login'];

		$_SESSION['name'] = $lu->fields['name'];

	}

}

?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

	<title><?=$name_ser?></title>

	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

	<link rel="stylesheet" href="style.css">

</head>

<body> 

<? include "header.php"; ?>



<?



if (!empty($_SESSION['logged'])) {

	$conn->execute ("update $users_table set last_logged='".time()."' where user_id='".$_SESSION['id_user']."'");





$check_icon = $conn->Execute ("select icon_id, login, file, rate, hits, votes from $icons_table, $users_table where $icons_table.approve='1' and $icons_table.icon_id='$ic_id' and $users_table.user_id=$icons_table.id_user");

if ($check_icon->RecordCount() == 1) {

?>

<taBLE border="1" bgcolor="#F1F9FA" width="<?=(100 / $icons_per_row);?>%" align=center><TR><TD>

<table align="center" cellpadding="5" width="100%" border="0">

<tr><td align="center"><br>

<?

if (!empty($message))

	echo $message."<br><Br>";

?>

<table cellpadding="5">

<tr>

	<Td align="right"><a href="redirect.php?id=<?=$check_icon->fields['icon_id'];?>" target="_blank"><img src="<?=$check_icon->fields['file'];?>" border="0"></a></TD>

	<td align="left">Name: <?=$check_icon->fields['login'];?><br>

	<?=date("Y-m-d", strtotime($q->fields['data']));?><br>

Clicks: <?=$check_icon->fields['hits'];?><br>

</td>

</tr>

</table>

</TD></TR></table>

<br><div align="center">

<?

if ($check_icon->fields['rate'] != 0 and $check_icon->fields['votes'] > 3) {

	$count = $conn->Execute("select rate from $icons_table group by rate having rate >= '".$check_icon->fields['rate']."' order by $icons_table.rate desc");

	echo "Rating: #<strong>". $count->RecordCount();

	echo "</strong><br><br>Current Rank: <strong>". $check_icon->fields['rate'];

} else 

	echo "More votes needed";

?></strong><br><br>

<?

$check = explode(",", $str_photo);



if (!in_array ($ic_id, $check)) {

?>

<form action="vote.php" method="post">

<input type="Hidden" name="ic_id" value="<?=$ic_id?>">

<select name="mark" size="1"  onchange="this.form.submit()">

<option value="">Rate It!

<option value="1">1

<option value="2">2

<option value="3">3

<option value="4">4

<option value="5">5

</select>

</form>

</div>

<?

} else 

		echo "<strong>You have already voted for this icon!</strong>";

?>

</td></tr>

</table>



<?

}



} else {

?>

<br>

<br>

<div align="center"><strong>You must be a member to vote for icons.<br><br>

Please, <a href="register.php">register</a> or login:</strong></div>



<form action="vote.php" method="post">

<input type="Hidden" name="ic_id" value="<?=$_GET['ic_id']?>">

<table cellpadding="2" align="center">

<tr>

	<td align="right"><strong>Login:</strong></td>

	<td><input type="Text" name="login"></td>

</tr>

<tr>

	<td align="right"><strong>Password:</strong></td>

	<td><input type="password" name="pass"></td>

</tr>

<tr><td colspan="2" align="center"><br>

<input type="Submit" name="log" value="Login now"></td></tr>

</table>

</form>



<?

}

?>





<? include "footer.php"; ?>



</body>

</html>

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

at first sight

Code: Select all

if (isset($_COOKIE['pcontest']) && isset ($_POST['ic_id']) && isset($_POST['mark'])) {
are you sure you don't want !isset($_COOKIE['pcontest'] ?
refugee13
Forum Newbie
Posts: 2
Joined: Tue Dec 28, 2004 9:08 am

WOW! Me Wonders how that happened!

Post by refugee13 »

Well, that actually allows the vote to work now...which is great, but it also allows the same user to vote multiple times, within the same session, but perhaps I haven't written it right to allow for this? Thank you so much so far!!
timvw wrote:at first sight

Code: Select all

if (isset($_COOKIE['pcontest']) && isset ($_POST['ic_id']) && isset($_POST['mark'])) {
are you sure you don't want !isset($_COOKIE['pcontest'] ?
Post Reply