Page 1 of 1

PHP + Forms + Bad Luck

Posted: Thu Aug 08, 2002 5:23 am
by BrandonKB
Hey,
I'm new to PHP, and have been looking at both phpcomplete and the php.net online docs to help me along the way. I have most of my problems fixed, so no errors are generated when the page is loaded, but i'm missing two desirable effects, they are: 1. To read from the file, scanning for the IP address of the user, and 2. To write multiple IP addresses to the text file. I am going to paste my rather sloppy looking code, please, if you can, help me out. BTW, this code is used to log IPs and read them from a file, i'm using it to prevent cheating on a poll.

<?php
$filename = "ipdata.txt";
$fp = fopen($filename, "w+b");
$offset = 0;
while ($searchip = fgets($fp, 4096)); {
$query_str = '$HTTP_SERVER_VARS["REMOTE_ADDR"]';
$pos = ;
if ($pos == $HTTP_SERVER_VARS["REMOTE_ADDR"])
{ echo 'You have voted once, and cannot vote again'; }
elseif ($pos !== $HTTP_SERVER_VARS["REMOTE_ADDR"])
{ echo 'You have submitted your only allowed vote, below are the results';
fclose($fp); }
else ($pos !== $HTTP_SERVER_VARS["REMOTE_ADDR"]) ;
{ $fd = fopen($filename, "w+b");
fwrite($fd, $HTTP_SERVER_VARS["REMOTE_ADDR"]); }
fclose($fd); }
?>

what are the permissions

Posted: Thu Aug 08, 2002 8:33 am
by gte604j
what are the permissions of the file?

also have you looked at the html page source to see if there are any errors that are in the source that dont show in the browser?

is there a reason for the ';' on the else statement?

Code: Select all

<?php 
$filename = "ipdata.txt"; 
$fp = fopen($filename, "w+b"); 
$offset = 0; 
while ($searchip = fgets($fp, 4096)) &#123; 
	$query_str = '$HTTP_SERVER_VARS&#1111;"REMOTE_ADDR"]'; 
	$pos = ; 
	if ($pos == $HTTP_SERVER_VARS&#1111;"REMOTE_ADDR"]) &#123; 
		echo 'You have voted once, and cannot vote again'; 
	&#125; elseif ($pos !== $HTTP_SERVER_VARS&#1111;"REMOTE_ADDR"]) &#123; 
		echo 'You have submitted your only allowed vote, below are the results'; 
		fclose($fp); 
	&#125;
else ($pos !== $HTTP_SERVER_VARS["REMOTE_ADDR"]);

Code: Select all

&#123; 
		$fd = fopen($filename, "w+b"); 
		fwrite($fd, $HTTP_SERVER_VARS&#1111;"REMOTE_ADDR"]); 
	&#125; 
	
	fclose($fd); 
&#125; 
?>

Posted: Thu Aug 08, 2002 10:21 am
by fatalcure
you do realize that you dont have $pos set to anything, so your script will never find the IP...b/c $pos will never equal REMOTE_ADDR; and if your searching for an IP stored in the file, why are u comparing $pos anyway? Isn't the value stored in $searchip?
your if statements are a little messed up too...it will never do the writing part.

You need to go have a look at files and if/else statements.

Posted: Thu Aug 08, 2002 1:10 pm
by BrandonKB
In reply to what you said about my if/else statements, the file is written to, and the permissions are set so you can read/write. The problem is that it wont write multiple IP addresses.

Posted: Thu Aug 08, 2002 6:00 pm
by BrandonKB
I think i found the ultimate problem of the script:

while ($searchip = fread($fp, 4096)); {
if ($searchip == $HTTP_SERVER_VARS["REMOTE_ADDR"])


because reading that file, it's not going to be equal to it, it's going to contain that persons IP address, so, if anyone has any new suggesstions, please help me with that part.

The second big problem, which is the fact that the file is not writing multiple IP addresses, it has the ability to only store one. This, i'm not sure why its happening, but, the error is contained somewhere in this part of the script:

else {
$fd = fopen($filename, "w+b");
fwrite($fd, $HTTP_SERVER_VARS["REMOTE_ADDR"]);
fclose($fd); }