Click to See Complete Forum and Search --> : PHP Script To Force Users?


Darker
10-21-2006, 11:35 AM
Hello..Since I Just Learnt That With PHP It Can Be Done Much Easier I Would Like To Ask How?

First Of All Here Is My Question: http://www.webdeveloper.com/forum/showthread.php?t=125268 (Read The 3 Posts)

Read The Above Topic To Understand What My Question Is, I Just Wanna Do It With PHP And Not JavaScript Since It Seems Easier And Faster To Me...
Hope Someone Out There Can Help Me.

carlh
10-21-2006, 01:59 PM
why would you want to force someone to vote?

Taschen
10-21-2006, 02:39 PM
Having read your other posts I would have to agree with:
why would you want to force someone to vote?

But I guess ours not to reason why. The simplest solution would be to register a session variable on completion of voting and only to allow access if the session is present and contains the correct value token. Think of it like logging someone in only the login is your voting form.

It could go a little like this:

session_start(); //Must be called first

if(isset($_SESSION['voted'])){
switch($_SESSION['voted']){
case true:
//do something
break;
default;
//You must go and vote
break;
}
}else{
//Please go and vote
}

bokeh
10-21-2006, 02:58 PM
Hello..Since I Just Learnt That With PHP It Can Be Done Much Easier I Would Like To Ask How?

First Of All Here Is My Question: http://www.webdeveloper.com/forum/showthread.php?t=125268 (Read The 3 Posts)

Read The Above Topic To Understand What My Question Is, I Just Wanna Do It With PHP And Not JavaScript Since It Seems Easier And Faster To Me...
Hope Someone Out There Can Help Me.Why do you use a capital letter of the start of every word?

Regarding the question it's not possible. The web is an annonomous medium and it is not possible to reliably recognise everyone on repeat visits.

felgall
10-21-2006, 03:44 PM
The only way to reliably recognise each person when they return is to set the site up so that it requires everyone to login to visit. You will then know it is the same person because they used the same login that they set up previously.

pcthug
10-21-2006, 07:23 PM
Although not flawless, the most common method is to set a cookie when a user votes. Then every time a user attempts to view your website check if the said cookie exists on there computer. If it doesn't, redirect them to the voting page. Otherwise, fulfill there original request.

The Little Guy
10-21-2006, 11:44 PM
Or you could save there IP address in a database, and check to see if there IP address is already in the database. If not force them to vote.

This is a not so good way since users IP addresses can change often/sometimes, but it is one way of doing things. I feel it is better than cookies on computers, since some browsers will delete all cookies after the browser is shut down, Mozilla has that option, and some people use it.

pcthug
10-22-2006, 12:25 AM
The downside of using an IP address as a detection method is that if the user's IP address changes they will have to vote again. And as some user's ISP's will hand them a different IP address every time they connect to the Internet, returning user's may become quite aggravated at the fact that they are forced to re-vote upon return. User's whom delete there cookies should be used to the common mishaps of not maintaing set cookies.

pcthug
10-22-2006, 04:27 AM
<?php
/*
execute the following code after a user has successfully voted

*/
$key = '7HJ8';

setcookie('has_voted', $key, (time() + 86400*365), '/', getDomain());

function getDomain() {
if ( isset($_SERVER['HTTP_HOST']) ) {
// Get domain
$dom = $_SERVER['HTTP_HOST'];
// Strip www from the domain
if (strtolower(substr($dom, 0, 4)) == 'www.') { $dom = substr($dom, 4); }
// Check if a port is used, and if it is, strip that info
$uses_port = strpos($dom, ':');
if ($uses_port) { $dom = substr($dom, 0, $uses_port); }
// Add period to Domain (to work with or without www and on subdomains)
$dom = '.' . $dom;
} else {
$dom = false;
}
return $dom;
}
?>


<?php
/*
use the following code to check if avalid cookie exists on the user's computer

*/
$key = '7HJ8';

if(!isset($_COOKIE['hav_voted']) || $_COOKIE['hav_voted'] != $key))
{
header('Location: /path/to/poll.php');
exit;
}
?>

bokeh
10-22-2006, 04:37 AM
some user's ISP's will hand them a different IP address every time they connect to the InternetIt would seem that with connections from certain ISPs (AOL being one I believe) the IP changes with ever single request for another page.

felgall
10-22-2006, 04:39 AM
You should NEVER try to force your visitors to do anything - at least not if you want them to come back to your site for another visit.