PHP + Forms + Bad Luck

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BrandonKB
Forum Newbie
Posts: 3
Joined: Thu Aug 08, 2002 5:23 am

PHP + Forms + Bad Luck

Post 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); }
?>
gte604j
Forum Newbie
Posts: 4
Joined: Tue Aug 06, 2002 10:58 am

what are the permissions

Post 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; 
?>
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post 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.
BrandonKB
Forum Newbie
Posts: 3
Joined: Thu Aug 08, 2002 5:23 am

Post 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.
BrandonKB
Forum Newbie
Posts: 3
Joined: Thu Aug 08, 2002 5:23 am

Post 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); }
Post Reply