Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Cookie Session help

Status
Not open for further replies.

refugee13

Programmer
Dec 28, 2004
6
0
0
US
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!)[/color red]:

Code:
<?

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" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]

<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>
 
i didn't study this too long, but it looks like your if...else statements may be closed improperly

Your code:
Code:
<option value="5">5
</select>
</form>
</div>
<?
} else 
        echo "<strong>You have already voted for this icon!</strong>";
?>
</td></tr>
</table>
<?
}
} else {
?>
<br>
<br>
Should Probably Be:
Code:
<option value="5">5
</select>
</form>
</div>
<?
} else { // <-- This { bracket
        echo "<strong>You have already voted for this icon!</strong>";
?>
</td></tr>
</table>
<?
}  //closes with this one
} else {  // <--- and where is this else coming from?  I can't tell exactly, but it looks like you have to Else's for one If.  That just won't work
?>
<br>
<br>

If I run across a problem I can't figure out for more than fifteen minutes or so, odds are I made a mistake with brackets!
 
Hello, sorry for the delay in response. That else statement is in reply to:
Code:
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'];

	}

}

Above, did I miss brackets there?? Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top