[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 05-09-2006, 01:35 PM   #1 (permalink)
NamePros Regular
 
asgsoft's Avatar
 
Join Date: Sep 2005
Location: At Home
Posts: 846
45.10 NP$ (Donate)

asgsoft is just really niceasgsoft is just really niceasgsoft is just really niceasgsoft is just really niceasgsoft is just really nice


Unique IP

I have a poll script but the only problem it has is that it will let me add more than one vote, I used a basic ip detection but that didn't work. Is there a way of getting the users real IP?

This is what I have right now:

PHP Code:
<?php
include 'config.php';
mysql_connect("$host", "$user", "$pass") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
$ip = $_SERVER['REMOTE_ADDR'];
if (
trim($ip) == '') {
    
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
if (
trim($ip) == '') {
    
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

$query = mysql_query("select * from vote where ip='$ip'");
$countbans = mysql_num_rows($query);
if(
$countbans > 0) {
include
"results.php";
echo(
"You have Already Voted!");
}else{

$action = $_GET['action'];
if(!isset(
$action) || empty($action) || $action !== "addvote"){
echo(
"<form name='form1' method='post' action='?action=addvote'>
                    <table width='100%' border='0' cellspacing='2' cellpadding='0'>
                      <tr>
                        <td colspan='2'><div align='left'>Do you like my site?</div></td>
                      </tr>
                      <tr>
                        <td width='19%'><input type='radio' name='vote' value='ex'></td>
                        <td width='81%' bgcolor='#F4F4F4'>It's Excellent!</td>
                      </tr>
                      <tr>
                        <td><input type='radio' name='vote' value='co'></td>
                        <td bgcolor='#F4F4F4'>Pretty Cool</td>
                      </tr>
                      <tr>
                        <td><input type='radio' name='vote' value='ok'></td>
                        <td bgcolor='#F4F4F4'>OK</td>
                      </tr>
                      <tr>
                        <td><input type='radio' name='vote' value='ha'></td>
                        <td bgcolor='#F4F4F4'>Hate It!</td>
                      </tr>
                      <tr>
                        <td></td>
                        <td><input type='submit' name='Submit' value='Vote!'></td>
                      </tr>
                    </table>
                  </form>"
);
}elseif(
$action == "addvote"){
//show result
echo "<p><h4>Thank you For Voting</h4></p>";
$vote = $_POST['vote'];
include
'config.php';
$ip = $_SERVER['REMOTE_ADDR'];
if (
trim($ip) == '') {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
if (
trim($ip) == '') {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
mysql_connect("$host", "$user", "$pass") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
mysql_query("INSERT INTO `vote` (`vote` , `ip` ) VALUES ('$vote', '$ip')");
}
}
?>
asgsoft is offline  
Old 05-10-2006, 03:19 AM   #2 (permalink)
NamePros Member
 
Join Date: May 2005
Posts: 85
135.00 NP$ (Donate)

MikeBigg is on a distinguished road


In what way didn't it work?

I think using IP address detection alone will not work 100%, primarily because of the way AOL uses proxies to route all their internet traffic. An AOL user is not guaranteed to have the same ip address for different images on a page, let alone two page requests.

There is also the issue of 2 people from the same IP address. Companies may well route all their employees through a single router with one IP address. Frequently the forwarded-for and client-ip fields will not be filled in as it is considered a security risk (even though IP addresses and machine names get sent out in email headers from the same organisations).

Using cookies as well may help.

I'd be very interested to see what you come up with in the end.

Regards,

Mike
MikeBigg is offline  
Old 05-10-2006, 09:58 AM   #3 (permalink)
JFS
NamePros Regular
 
JFS's Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 760
56.85 NP$ (Donate)

JFS is just really niceJFS is just really niceJFS is just really niceJFS is just really niceJFS is just really niceJFS is just really nice


Code:
function validip($ip) { 
   if (!empty($ip) && ip2long($ip)!=-1) { 
       $reserved_ips = array ( 
       array('0.0.0.0','2.255.255.255'), 
       array('10.0.0.0','10.255.255.255'), 
       array('127.0.0.0','127.255.255.255'), 
       array('169.254.0.0','169.254.255.255'), 
       array('172.16.0.0','172.31.255.255'), 
       array('192.0.2.0','192.0.2.255'), 
       array('192.168.0.0','192.168.255.255'), 
       array('255.255.255.0','255.255.255.255') 
       ); 

       foreach ($reserved_ips as $r) { 
           $min = ip2long($r[0]); 
           $max = ip2long($r[1]); 
           if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false; 
       } 
       return true; 
   } else { 
       return false; 
   } 
} 


function getip() { 
   if (validip($_SERVER["HTTP_CLIENT_IP"])) { 
       return $_SERVER["HTTP_CLIENT_IP"]; 
   } 
   foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) { 
       if (validip(trim($ip))) { 
           return $ip; 
       } 
   } 
   if (validip($_SERVER["HTTP_X_FORWARDED"])) { 
       return $_SERVER["HTTP_X_FORWARDED"]; 
   } elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) { 
       return $_SERVER["HTTP_FORWARDED_FOR"]; 
   } elseif (validip($_SERVER["HTTP_FORWARDED"])) { 
       return $_SERVER["HTTP_FORWARDED"]; 
   } elseif (validip($_SERVER["HTTP_X_FORWARDED"])) { 
       return $_SERVER["HTTP_X_FORWARDED"]; 
   } else { 
       return $_SERVER["REMOTE_ADDR"]; 
   } 
}
this gets you the real ip from the user and validat its, hope it helps
__________________
Joćo Fernandes Silva
Selling :
19P.ORG - ARCADEHITS.ORG - AZIAN.NET - COREFANS.COM - CTUTORIALS.NET - DEDISEEK.COM - HITCHECK.COM - HOST15.COM - HOSTCUSTOMER.COM - LARGETIPS.COM - SCRIPTCANDY.COM - VISUALBOOK.NET - VOXVPS.COM / .NET - WALLPAPERSARENA.COM
JFS is offline  
Old 05-11-2006, 06:59 AM   #4 (permalink)
 
BillyConnite's Avatar
 
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,107
47.00 NP$ (Donate)

BillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant futureBillyConnite has a brilliant future

Wildlife Parkinson's Disease Parkinson's Disease
Yes, in what way do you mean that you didn't get an IP?
This should work in php:
Quote:
if($_SERVER[HTTP_X_FORWARD_FOR]){
$ip = $_SERVER[HTTP_X_FORWARD_FOR];
}else{
$ip = $_SERVER[REMOTE_ADDR];
}
echo $ip;
All the best, Rhett.
__________________
<?php if(1===1){ $computer="fine."; }else{ $computer="broken."; } echo "Your computer is ".$computer; ?>
BillyConnite is offline  
Old 05-11-2006, 09:28 AM   #5 (permalink)
NamePros Regular
 
asgsoft's Avatar
 
Join Date: Sep 2005
Location: At Home
Posts: 846
45.10 NP$ (Donate)

asgsoft is just really niceasgsoft is just really niceasgsoft is just really niceasgsoft is just really niceasgsoft is just really nice


Quote:
Originally Posted by BillyConnite
Yes, in what way do you mean that you didn't get an IP?
This should work in php:


All the best, Rhett.
Does that mean I can get the actual IP even if they are behind a proxy and it only makes it one vote per PC?
asgsoft is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 08:26 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85