POll Based on cookies for intranet

Get help with installation and running phpwcms here. Please do not post bug reports or feature requests here.
Post Reply
caramello
Posts: 70
Joined: Thu 23. Sep 2004, 21:43

POll Based on cookies for intranet

Post by caramello »

Hellowww.

For one of my customers i run an intranet site, thats is hosted externaly. Therefor a POLL based on IP adresses wont realy work due to the fact that al of the 1000 intranet users share the same public IP address.

The logical action seemed to be to rewrite the cnt part 89 for ....tadada COOKIES ! Yes that should work in this case. It works pretty well and needs some cleanup on my behalve. My foremost problem is that once a poll vote has been sent, i set a cookie, but the first time that i reopen the page the poll is on, it still shows me the poll vote form. I need to refresh the page once and then it sees the cookie.

Can someone tell me why this occurs , where i need / should set and check the cookie if it exists.

Here's my replacement code fo cnt89.article.inc.php

Code: Select all

<?php
/*************************************************************************************
   Copyright notice
   
   (c) 2002-2010 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
  
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license 
   from the author is found in LICENSE.txt distributed with these scripts.
  
   This script is distributed in the hope that it will be useful, but WITHOUT ANY 
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
/////////////////////////////////////////////////////////////////////////////////*/

// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day.");
}
// ----------------------------------------------------------------

if($_COOKIE[unie_poll]) {
	// $CNT_TMP .= "Cookie found";
	$action = 'result';
}
elseif(!$_COOKIE[unie_poll] && !isset($_POST['poll'])) {
	$Month = 2592000 + time(); 
	//this adds 30 days to the current time 
	// setcookie(AboutVisit, date("F jS - g:i a"), $Month);
	
	setcookie(unie_poll, $crow["acontent_id"], $Month);	
	// $CNT_TMP .= "No cookie no poll posted";
	$action = 'show';
}
elseif(!$_COOKIE[unie_poll] && isset($_POST['poll'])) {
	// $CNT_TMP .= "No cookie poll posted";
	$action = 'save';
}


// If POLL posted 
if($_POST['poll']){
	$Month = 2592000 + time(); 
	//this adds 30 days to the current time 
	// setcookie(AboutVisit, date("F jS - g:i a"), $Month);
	
	setcookie(unie_poll, $crow["acontent_id"], $Month);	

}



// content type 89: poll 		jens
$poll_html			= "";
$poll_image			= @unserialize($crow["acontent_image"]);
$poll_text			= @unserialize($crow["acontent_text"]);
$poll_form			= @unserialize($crow["acontent_form"]);
$poll_choice_count	= 0;
// $remoteIP			= getRemoteIP();




$value = $crow["acontent_id"];




if(!isset($_COOKIE[unie_poll])) {

	if(!isset($poll_form["ip"])) {
		$poll_form["ip"] = array();
	}

	$poll_id 	= isset($_POST["hidden_acontent_id"]) ? intval($_POST["hidden_acontent_id"]) : 0;
	$poll_count	= max(count($poll_form["choice"]), count($poll_image["images"]));


	if($poll_id == $crow['acontent_id'] && isset($_POST["poll"])) {
		$poll_choosen 						= intval($_POST["poll"]);
		$poll_form["count"][$poll_choosen] += 1;
		$poll_form["ip"][] = $remoteIP; 
		$poll_choice_count = $poll_form["count"][$poll_choosen];
			
		$sql  = "UPDATE ".DB_PREPEND."phpwcms_articlecontent ";
		$sql .= "SET acontent_form	='".aporeplace(serialize($poll_form))."' ";
		$sql .= "WHERE acontent_id = ".$poll_id." LIMIT 1";
		@mysql_query($sql, $db);
		$value = $crow["acontent_id"];
		

		$poll_total_votes = 0;
		foreach($poll_form["count"] as $key => $value) {
			$poll_total_votes += $value;
		}
		if($poll_total_votes > 0) {
			$poll_html .= '<div style="padding : 5px;"><table cellpadding="0" cellspacing="0" border="0">';
		
			for($key = 0; $key < $poll_count; $key++)
			{
				$poll_html .= "\n<tr>\n\t<td>";
				$poll_do_br = '';
				$poll_form["choice"][$key] = isset($poll_form["choice"][$key]) ? trim($poll_form["choice"][$key]) : '';
				if(!empty($poll_form["choice"][$key])) {
					$poll_html .= html_specialchars($poll_form["choice"][$key]);
					$poll_do_br = '<br />';
				}
				if(is_array($poll_image["images"][$key]) && count($poll_image["images"][$key]))
				{
					$poll_html .= $poll_do_br;
					$poll_html .= showPollImage($poll_image["images"][$key]);
				}
				$barWidth = round(($poll_form["count"][$key] / $poll_total_votes * 100), 0);
				$poll_html .= "</td>\n\t".'<td class="pollBarCell">';
				$poll_html .= '<span class="pollBarBegin"><!-- --></span>';
				$poll_html .= '<span class="pollBarMain" style="width:'.$barWidth.'px;"><!-- --></span>';
				$poll_html .= '<span class="pollBarPercent">'.$barWidth."%</span></td>\n</tr>\n";
			}
			$poll_html .= "</table></div>";
		}


		$CNT_TMP .= '<div class="';
		// $CNT_TMP .= empty($poll_text['poll_buttonstyle']) ? 'defaultPollClass' : $poll_text['poll_buttonstyle'];
		$CNT_TMP .= "\">\n";
		// $CNT_TMP .= headline($crow["acontent_title"], $crow["acontent_subtitle"], $template_default["article"]);
		$CNT_TMP .= "<h3>POLL</h3>";
		$CNT_TMP .= "<p><b>".$crow["acontent_title"]."</b></p>";
		$CNT_TMP .= "<p>Bedankt voor het stemmen ! <br/></p>";
		$CNT_TMP .= $poll_html;
		$CNT_TMP .= "</div>";
		// exit();
		
	}

	/* if(isset($_COOKIE['pollvote2']) && $_COOKIE['pollvote2'] == $value) {

		$CNT_TMP .= "<p style=\"color : #3366cc;margin-left : 10px;\"> Er is al gestemd vanaf deze computer</p>";
	} */

	else {

	$value = $crow["acontent_id"];
	$CNT_TMP .= '<div class="';
	$CNT_TMP .= empty($poll_text['poll_buttonstyle']) ? 'defaultPollClass' : $poll_text['poll_buttonstyle'];
	$CNT_TMP .= "\">\n";
	// $CNT_TMP .= headline($crow["acontent_title"], $crow["acontent_subtitle"], $template_default["article"]);
	$CNT_TMP .= "<h3 >POLL</h3>";
	$CNT_TMP .= "<p><b>".$crow["acontent_title"]."</b></p>";


	if (isset($_COOKIE[unie_poll]) && $_COOKIE[unie_poll] == $crow["acontent_id"]) {
		$CNT_TMP .= "<p style=\"color : #3366cc;margin-left : 10px;\"> Er is al gestemd vanaf deze computer</p>";
		$poll_total_votes = 0;
		foreach($poll_form["count"] as $key => $value) {
			$poll_total_votes += $value;
		}
		if($poll_total_votes > 0) {
			$poll_html .= '<table cellpadding="0" cellspacing="0" border="0">';
		
			for($key = 0; $key < $poll_count; $key++)
			{
				$poll_html .= "\n<tr>\n\t<td>";
				$poll_do_br = '';
				$poll_form["choice"][$key] = isset($poll_form["choice"][$key]) ? trim($poll_form["choice"][$key]) : '';
				if(!empty($poll_form["choice"][$key])) {
					$poll_html .= html_specialchars($poll_form["choice"][$key]);
					$poll_do_br = '<br />';
				}
				if(is_array($poll_image["images"][$key]) && count($poll_image["images"][$key]))
				{
					$poll_html .= $poll_do_br;
					$poll_html .= showPollImage($poll_image["images"][$key]);
				}
				$barWidth = round(($poll_form["count"][$key] / $poll_total_votes * 100), 0);
				$poll_html .= "</td>\n\t".'<td class="pollBarCell">';
				$poll_html .= '<span class="pollBarBegin"><!-- --></span>';
				$poll_html .= '<span class="pollBarMain" style="width:'.$barWidth.'px;"><!-- --></span>';
				$poll_html .= '<span class="pollBarPercent">'.$barWidth."%</span></td>\n</tr>\n";
			}
			$poll_html .= "</table>";
		}
	} elseif(!isset($_COOKIE[unie_poll]) && (is_array($poll_form["choice"]) && count($poll_form["choice"])) || (is_array($poll_image["images"]) && count($poll_image["images"]))) {

		$form_name  = "form_".randpassword(6);
		$poll_html .= '<form action="'.FE_CURRENT_URL.'" method="post" name="'.$form_name.'" id="'.$form_name.'">';
		$poll_html .= '<table cellpadding="0" cellspacing="0" border="0">';
			
		for($key = 0; $key < $poll_count; $key++) {
		
			$poll_html .= "\n<tr>\n\t".'<td class="pollRadioCell">';
			$poll_html .= '<input type="radio" name="poll" value="'.$key.'" />';
			$poll_html .= "</td>\n\t";
			$poll_html .= '<td class="pollInfo">';
			
			$poll_do_br = '';
			
			$poll_form["choice"][$key] = isset($poll_form["choice"][$key]) ? trim($poll_form["choice"][$key]) : '';
			if(!empty($poll_form["choice"][$key])) {
				$poll_html .= $poll_form["choice"][$key];
				$poll_do_br = '<br />';
			}
			if(is_array($poll_image["images"][$key]) && count($poll_image["images"][$key])) {
				$poll_html .= $poll_do_br;
				$poll_html .= showPollImage($poll_image["images"][$key]);
			}
			$poll_html .= "</td>\n</tr>";
		}
		$poll_html .= "</table>\n";
		$poll_html .= '<input type="submit"';
		$poll_html .= empty($poll_text['poll_buttontext']) ? '' : ' value="'.html_specialchars($poll_text['poll_buttontext']).'"';
		$poll_html .= ' class="pollbtn" />';
		$poll_html .= '<input type="hidden" value="'.$crow['acontent_id'].'" name="hidden_acontent_id" />';
		$poll_html .= "</form>";
	}

	$CNT_TMP .= $poll_html;
	$CNT_TMP .= "\n</div>\n";
	}

	unset($poll_image);
	unset($poll_text);
	unset($poll_form);

// end:
	// $CNT_TMP .= "Bedankt voor het stemmen !";

} // End check if cookie is set;

if(isset($_COOKIE[unie_poll])) {
	$poll_id 	= isset($_POST["hidden_acontent_id"]) ? intval($_POST["hidden_acontent_id"]) : 0;
	$poll_count	= max(count($poll_form["choice"]), count($poll_image["images"]));
	$poll_total_votes = 0;
		foreach($poll_form["count"] as $key => $value) {
			$poll_total_votes += $value;
		}
		if($poll_total_votes > 0) {
			$poll_html .= '<div style="padding : 5px;"><table cellpadding="0" cellspacing="0" border="0">';
		
			for($key = 0; $key < $poll_count; $key++)
			{
				$poll_html .= "\n<tr>\n\t<td>";
				$poll_do_br = '';
				$poll_form["choice"][$key] = isset($poll_form["choice"][$key]) ? trim($poll_form["choice"][$key]) : '';
				if(!empty($poll_form["choice"][$key])) {
					$poll_html .= html_specialchars($poll_form["choice"][$key]);
					$poll_do_br = '<br />';
				}
				if(is_array($poll_image["images"][$key]) && count($poll_image["images"][$key]))
				{
					$poll_html .= $poll_do_br;
					$poll_html .= showPollImage($poll_image["images"][$key]);
				}
				$barWidth = round(($poll_form["count"][$key] / $poll_total_votes * 100), 0);
				$poll_html .= "</td>\n\t".'<td class="pollBarCell">';
				$poll_html .= '<span class="pollBarBegin"><!-- --></span>';
				$poll_html .= '<span class="pollBarMain" style="width:'.$barWidth.'px;"><!-- --></span>';
				$poll_html .= '<span class="pollBarPercent">'.$barWidth."%</span></td>\n</tr>\n";
			}
			$poll_html .= "</table></div>";
		}

	$value = $crow["acontent_id"];
	$CNT_TMP .= '<div class="';
	// $CNT_TMP .= empty($poll_text['poll_buttonstyle']) ? 'defaultPollClass' : $poll_text['poll_buttonstyle'];
	$CNT_TMP .= "\">\n";
	// $CNT_TMP .= headline($crow["acontent_title"], $crow["acontent_subtitle"], $template_default["article"]);
	$CNT_TMP .= "<h3 style=\"height : 16px;\">POLL</h3>";
	$CNT_TMP .= "<p>".$crow["acontent_title"]."</p>";
	$CNT_TMP .= "<p style=\"color : red;\">Je hebt al gestemd op deze poll.<br/></p>";
	$CNT_TMP .= $poll_html;
	$CNT_TMP .= "</div>";
}




?>
I hope someone can help me before i smash more keyboards. I'm running out of spares. ;-)
A website is a concept not a digital brochure ! Vectrus Internet Solution
User avatar
Oliver Georgi
Site Admin
Posts: 9888
Joined: Fri 3. Oct 2003, 22:22
Contact:

Re: POll Based on cookies for intranet

Post by Oliver Georgi »

your PHP has lot of bugs and needs cleanup. Check error reporting. This is highly injectable.

The better combination would be to use cookie and session.
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
caramello
Posts: 70
Joined: Thu 23. Sep 2004, 21:43

Re: POll Based on cookies for intranet

Post by caramello »

i know it's buggy, it's in dev version, better yet a workaround for an intranet locked away behind a fw. I've locked it in a room and threw away the room for a while. In time i'll get back on it. I need the poll for a couple of websites once in a while and I do think the currently supplied one isn't quite up-to-date for modern day use.

So to get back to the answer, i couldn't use sessions as it get's killed when the browser closes right ? I know for a fact that cookies don't get deleted on exit on this network as explorer settings are network settings applied by policy.
A website is a concept not a digital brochure ! Vectrus Internet Solution
User avatar
Oliver Georgi
Site Admin
Posts: 9888
Joined: Fri 3. Oct 2003, 22:22
Contact:

Re: POll Based on cookies for intranet

Post by Oliver Georgi »

Don't get me too serious ;)

In general cookies are deleted on browser quit – but is based on setting when the cookie should expire. And session ends in general. So it is the combination of both you need.
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
User avatar
baklavoni
Posts: 170
Joined: Mon 23. Oct 2006, 00:48
Location: Bosnia and Herzegovina

Re: POll Based on cookies for intranet

Post by baklavoni »

Oliver,
since it is mentioned above,... Did you planned to upgrade Poll CP to little bit modern one?
I think it would be nice that we can choose inside CP the way results are displayed (% or total number of votes). Right now, I modify cnt89.article.inc.php... ;)
And, i think usefull idea is option to use pictures as poll choices ;)

Also, it would be nice if Poll CP would have templates, so we can make and use different templates :)


Only my ideas,... :)
There is no page like home page...
Post Reply