Page 1 of 2

Problem using strpos to evaulate if data exists on fie

Posted: Sun Oct 01, 2006 1:26 pm
by akimm
My code is as the title suggest, anyone know where the error is in this thing.

Code: Select all

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$check = strpos(filesize($fr)) == $ip;
if(file_exists('iplog/ip.txt')) {
$fp = fopen('iplog/ip.txt', 'a');
if($fp && $fr && $check) {
	#just a testfor me to know its reading and writing
echo "your ip exists on this list already!";
} else  {
fwrite($fp, $ip);
fclose($fp);
	}
  }
?>

Posted: Sun Oct 01, 2006 1:31 pm
by miro_igov

Code: Select all

$check = (strpos(filesize($fr)) == $ip) ? true : false;
[/quote]

Posted: Sun Oct 01, 2006 1:34 pm
by akimm
Ah, nice. I shall try it..

Thank You!

Posted: Sun Oct 01, 2006 1:38 pm
by akimm
I tried the ternary, no luck yet. this is how I formatted the code around ternary, is this how you intended it?

Code: Select all

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$check = (strpos(filesize($fr)) == $ip) ? true : false;
if(file_exists('iplog/ip.txt')) {
$fp = fopen('iplog/ip.txt', 'a');
if($fp && $fr && $check=true) {
	#just a testfor me to know its reading and writing
echo "your ip exists on this list already!";
} else  {
fwrite($fp, $ip);
fclose($fp);
	}
  }
?>

Posted: Sun Oct 01, 2006 1:47 pm
by akimm
I am not really knowledgable with this operator, but I tried different conbinations of ?true, $check?=true , $check=?true, $check="?true" et cetera. Some caused errors, before the error in the logic could be shown, and others later would error with this message:

Warning: Wrong parameter count for strpos() in /nfs/cust/8/25/05/650528/web/journal06/journal.php on line 323

Anyone know where this code is going wrong?

Posted: Sun Oct 01, 2006 1:48 pm
by John Cartwright
miro_igov wrote:

Code: Select all

$check = (strpos(filesize($fr)) == $ip) ? true : false;
what does filesize() have anything to do with finding a needle? I personally do something along the lines of:

Code: Select all

/*
 * Load the entire file into an array
 * If file cannot be opened end script execution
*/
$ips = file('iplog/ip.txt') or die('Cannot open "iplog/ip.txt"');

/*
 * Check for the existance of the user's ip (considering it is optional)
 * Check user's IP against ip log
*/
if (isset($_SERVER['REMOTE_ADDR']) && in_array($_SERVER['REMOTE_ADDR'], $ips)) {
   echo 'Your ip exists';
} else {
   //write to file
}
You are aware of the unrealiability of using IP addresses correct?

Posted: Sun Oct 01, 2006 2:11 pm
by akimm
I understand they can change, this logging is sort of an education adventure.

However it does have real purposes. I am using the IP to track unique visits, so if someone gets by me I won't mind, one more visitor :-)!

But thank you, I wish I had the ability to solve this problem as simplyas you have.

Posted: Sun Oct 01, 2006 3:35 pm
by Ollie Saunders
Ternary is not big or clever. Generally its just makes things more complicated than they need to be. In fact here it’s totally redundant.

Code: Select all

$check = (strpos(filesize($fr)) == $ip) ? true : false;
is a superfluous way of saying

Code: Select all

$check = strpos(filesize($fr)) == $ip;
EDIT: Just noticed that was what you were doing before, which was quite correct
I am using the IP to track unique visits
A whole building (picture a 70 storey skyscraper) can be connected to the internet via a single IP. Other users, with dynamic IPs, could visit your site 3 different times with 3 different IPs.

"So what is the best why to record unique visits ole?", Sadly there is no perfect solution. Cookies combined with sessions are reasonable. Cookies are susceptible to user's clearing or disabling cookies.

Posted: Sun Oct 01, 2006 4:18 pm
by onion2k
ole wrote:Ternary is not big or clever. Generally its just makes things more complicated than they need to be.
I find they make the code more readable. Preferable to lots of if..else statements anyway.

Posted: Sun Oct 01, 2006 4:33 pm
by Ollie Saunders
I find they make the code more readable. Preferable to lots of if..else statements anyway.
Chris Shiflett would like to disagree. I've lent his book to a friend but there's an excellent example in there that demonstrates how security vulnerabilities can be hidden by the ternary op. Personally I think a section in the PHP manual sums up my dislike of the ternary op.

Posted: Sun Oct 01, 2006 4:57 pm
by twigletmac
ole wrote:Personally I think a section in the PHP manual sums up my dislike of the ternary op.
Never understand why anyone stacks ternaries - or why they often get used in the middle of a string.

Mac

Posted: Sun Oct 01, 2006 5:06 pm
by Ambush Commander
I can understand the second case. It's a pain to switch out of concatenation and bust out an if block.

Posted: Mon Oct 02, 2006 5:17 am
by Mordred
A good idea is to have iplog/ip.txt unreachable by http (best thing is to keep it above the web root)

Posted: Mon Oct 02, 2006 12:55 pm
by akimm
how do you go about making it above webroot? That is, I have my webbie on a server, which is not my own. So I can't actually achieve this right?

Posted: Mon Oct 02, 2006 1:05 pm
by Luke
you should be able to... I use shared hosting, and every host I've had has allowed me to create directories and files above the root... what files are listed by default when you log in via ftp? probably something like tmp, cgi-bin, htdocs or www or wwwroot and some other stuff?