Jump to content

[SOLVED] array_keys help


Ryokotsusai

Recommended Posts

Hi,

I have a simple function that is supposed to count the # of times the user's ip comes up in a txt log and continue based on that number,

but for some reason, the array_keys refuses to find the ip in the array (and at the moment the one i am looking for is the only one there)

 

this code:

	# Now read files to array
$ip = $_SERVER['REMOTE_ADDR'];
$b = $p = 0;
$btxt = file($uco->btxt);
$ptxt = file($uco->ptxt);
foreach($btxt as $bun) { list($btime[$b], $bip[$b]) = explode(",",$bun); $b++; }
foreach($ptxt as $pun) { list($ptime[$p], $pip[$p]) = explode(",",$pun); $p++; }
unset($b,$p);

# Count Instances of your ip in the txt files
if(is_array($pip)) {$y = array_keys($pip,$ip);}
if(is_array($bip)) {$z = array_keys($bip,$ip);}
$p=0;
if(is_array($y)) {
	foreach($y as $a) {
		echo 'got this far';
		echo $ptime[$a];
		if($ptime[$a] > (time()-(15*60)))
		{$p++;}
	}
}
echo $p."\n<br>\n";
print_r($pip);
echo "\n<br>\n";
print_r($y);
echo "\n<br>\n";
print_r(array_keys($pip,$ip));
echo "<br>\n".$ip;

 

returns this:

0
<br>
Array
(
    [0] => 65.32.58.50

    [1] => 65.32.58.50

    [2] => 65.32.58.50

    [3] => 65.32.58.50

    [4] => 65.32.58.50

    [5] => 65.32.58.50

    [6] => 65.32.58.50

    [7] => 65.32.58.50

    [8] => 65.32.58.50

    [9] => 65.32.58.50

    [10] => 65.32.58.50

    [11] => 65.32.58.50

    [12] => 65.32.58.50

    [13] => 65.32.58.50

    [14] => 65.32.58.50

    [15] => 65.32.58.50

    [16] => 65.32.58.50

    [17] => 65.32.58.50

    [18] => 65.32.58.50

    [19] => 65.32.58.50

    [20] => 65.32.58.50

    [21] => 65.32.58.50

    [22] => 65.32.58.50

    [23] => 65.32.58.50

    [24] => 65.32.58.50

    [25] => 65.32.58.50

    [26] => 65.32.58.50

)

<br>
Array
(
)

<br>
Array
(
)
<br>
65.32.58.50

 

are there limits on array_keys?

or is there a specific reason that this wont work?

 

(i have it reporting all errors and notices, and it says nothing)

Link to comment
Share on other sites

i was using keys 'cause it returns the 'location(s)' in the chosen array in array form, which i use later, for 2 things, counting how many times it comes up, and i use the specific location to get the time which is in another array under the same id

 

Link to comment
Share on other sites

OK that function just looks a mess, what are you trying to do ?

 

count the occurrences of the IP's from a text file, right ?

 

basic example

<?php
$data = file_get_contents ("ips.txt");
$dataArray = explode(",",$data);
$IPcounter = array();
foreach($dataArray as $IPS)
{
if(array_key_exists($IPS, $IPcounter))
{
	$IPcounter[$IPS] = $IPcounter[$IPS] +1;
}else{
	$IPcounter[$IPS] = 1;
}
}

$search = "127.0.0.1";
echo "$search found {$IPcounter[$search]} times";

echo "<pre>";print_r($IPcounter);

?>

 

192.168.1.100,192.168.1.100,192.168.1.100,192.168.1.100,192.168.1.100,127.0.0.1,192.168.1.100,192.168.1.100,192.168.1.2,127.0.0.1,127.0.0.1,127.0.0.1

 

127.0.0.1 found 4 times

 

Array

(

    [192.168.1.100] => 7

    [127.0.0.1] => 4

    [192.168.1.2] => 1

)

Link to comment
Share on other sites

the problem there is I am not only getting the IP i am getting the time it was entered as well...

 

http://admin.gravro.com/admin/db/p.txt is the file its reading from ( the format can be different )

 

it gets each line then does this:

 

foreach($ptxt as $pun) { list($ptime[$p], $pip[$p]) = explode(",",$pun); $p++; }

 

so that i can use the time and IP later, here is the entire function:

# # # # # # # # # # # # # # # # # # # # # # # #
#	Login Failed
# # # # # # # # # # # # # # # # # # # # # # # #

function log_failed() {
global $uco;

$ip = $_SERVER['REMOTE_ADDR'];

# Write this failed attempt first
	if(is_writable($uco->ptxt)) {
		$string = time().','.$ip."\n";
		file_put_contents($uco->ptxt,$string,FILE_APPEND);
	} else {
		echo("Script Error");
	}

# Now read files to array
$b = $p = 0;
$btxt = file($uco->btxt);
$ptxt = file($uco->ptxt);
foreach($btxt as $bun) { list($btime[$b], $bip[$b]) = explode(",",$bun); $b++; }
foreach($ptxt as $pun) { list($ptime[$p], $pip[$p]) = explode(",",$pun); $p++; }
unset($b,$p);

# Count Instances of your ip in the txt files
if(is_array($pip)) {$y = array_keys($pip,$ip);}
if(is_array($bip)) {$z = array_keys($bip,$ip);}
$p=0;
if(is_array($y)) {
	foreach($y as $a) {
		echo 'got this far';
		echo $ptime[$a];
		if($ptime[$a] > (time()-(15*60)))
		{$p++;}
	}
}

echo $p."\n<br>\n";
print_r($pip);
echo "\n<br>\n";
print_r($y);
echo "\n<br>\n";
print_r(array_keys($pip,$ip));
echo "<br>\n".$ip;

# If 3 or more instances are from the last 15 minutes, add your name to the banlist
if($p >= 3) {
	if(is_writable($uco->btxt)) {
		$str = (time()+(15*60)).','.$ip."\n";
		file_put_contents($uco->btxt,$str,FILE_APPEND);
		$msg = "<br />3 failed attempts to login, you have been banned for 15 min";
	} else {
		$msg = "Script Error";
	}
} else {
	$msg = '';
}

return $msg;
}

 

(it never gets to 'got this far')

 

if i used the one you posted would i need to run one for the time and one for the ip?

Link to comment
Share on other sites

if i used the one you posted would i need to run one for the time and one for the ip?

 

as i said its a

basic example

 

heres a complete solution

<?php
$data = file_get_contents ("data.txt");
$IPArray = explode("\n",$data);
$IPcounter = array();
foreach($IPArray as $IPS)
{
list($Time, $IP) = explode(",",$IPS);
if(array_key_exists($IP, $IPcounter))
{
	$IPcounter[$IP]['IP'] = $IPcounter[$IP]['IP'] +1;
}else{
	$IPcounter[$IP]['IP'] = 1;
}
$IPcounter[$IP]['Times'][] = $Time;
}

$search = "65.32.58.50";
echo "$search found {$IPcounter[$search]['IP']} times, at the follow times<br>";

foreach($IPcounter[$search]['Times'] as $times)
{
echo date ("F j, Y, g:i a", $times);
echo "<br>";
}

?>

 

1188345112,65.32.58.50
1188345256,65.32.58.50
1188345514,65.32.58.50
1188345605,65.32.58.50
1188345676,65.32.58.50
1188345751,65.32.58.50
1188345794,65.32.58.50
1188345805,65.32.58.50
1188346081,65.32.58.50
1188346182,65.32.58.50
1188346237,65.32.58.50
1188346257,65.32.58.50
1188346291,65.32.58.50
1188346322,65.32.58.50
1188346424,65.32.58.50
1188346454,65.32.58.50
1188346498,65.32.58.50
1188346535,65.32.58.50
1188346705,65.32.58.50
1188346784,65.32.58.50
1188346810,65.32.58.50
1188346826,65.32.58.50
1188346849,65.32.58.50
1188347022,65.32.58.50
1188347111,65.32.58.50
1188347429,65.32.58.50
1188347452,65.32.58.50
1188387152,65.32.58.50
1188387162,65.32.58.50
1188387230,65.32.58.50
1188387663,65.32.58.50
1188512902,65.32.58.50

 

65.32.58.50 found 32 times, at the follow times

August 29, 2007, 12:51 am

August 29, 2007, 12:54 am

August 29, 2007, 12:58 am

August 29, 2007, 1:00 am

August 29, 2007, 1:01 am

August 29, 2007, 1:02 am

August 29, 2007, 1:03 am

August 29, 2007, 1:03 am

August 29, 2007, 1:08 am

August 29, 2007, 1:09 am

August 29, 2007, 1:10 am

August 29, 2007, 1:10 am

August 29, 2007, 1:11 am

August 29, 2007, 1:12 am

August 29, 2007, 1:13 am

August 29, 2007, 1:14 am

August 29, 2007, 1:14 am

August 29, 2007, 1:15 am

August 29, 2007, 1:18 am

August 29, 2007, 1:19 am

August 29, 2007, 1:20 am

August 29, 2007, 1:20 am

August 29, 2007, 1:20 am

August 29, 2007, 1:23 am

August 29, 2007, 1:25 am

August 29, 2007, 1:30 am

August 29, 2007, 1:30 am

August 29, 2007, 12:32 pm

August 29, 2007, 12:32 pm

August 29, 2007, 12:33 pm

August 29, 2007, 12:41 pm

August 30, 2007, 11:28 pm

 

As a side note, were here to help, not write the code for you..

 

personally it depends on the day i have had (today: full of stress)

Link to comment
Share on other sites

Thank you for explaining that, I am kind of new to using both arrays and file system functions, and it never would have crossed my mind to explode the arrays more than once  :P

 

As a side note, were here to help, not write the code for you..

and even though i am appreciative of your help, i felt this was rude and uncalled for, as I only asked for help and not in any of my replies did i ever ask for someone else to write something for me, and in reply to the code you posted all i asked was for anyone to explain it further, because as i stated, I am new to arrays and file system functions.

 

 

Link to comment
Share on other sites

I was not aimed solely at you, i have had a lot of post and PM's asking how do in intergreat this and that.. and i seams to of targetted you..

 

i apologise for my last post (just a stressful week) lots on and no one was helping you so i updated the script but as i was posting it i thought why the hell am i doing this i have too much to do already..

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.