Problem using strpos to evaulate if data exists on fie

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

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Problem using strpos to evaulate if data exists on fie

Post 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);
	}
  }
?>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

$check = (strpos(filesize($fr)) == $ip) ? true : false;
[/quote]
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Ah, nice. I shall try it..

Thank You!
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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);
	}
  }
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I can understand the second case. It's a pain to switch out of concatenation and bust out an if block.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

A good idea is to have iplog/ip.txt unreachable by http (best thing is to keep it above the web root)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
Post Reply