Monday 15 June 2015

Allow 2 duplicate ip addresses maximum php csv -



Allow 2 duplicate ip addresses maximum php csv -

i have issue processing script. allow 2 duplicate ip addresses maximum in csv file, prevent spamming , take consideration user create error in form fill. cant seem reference $ip variable correctly in script, or there might missing altogether. here code snippet far:

<?php #variable declarations (filename , post vars) go here $counter = 0; if (file_exists($filename)) { $file = fopen($filename, "a"); while($data = fgetcsv($filename)){ if(isset($data[$ip])){ $counter++; continue; if((isset($data[$ip])){ $counter++; if($counter == 2){ echo ""; } } } } ##file write goes here } ?>

any help on appreciated,

jim

you have read elements in array first , after have number of occurrences of each ip address ready, should go ahead writing file (a separate file may be?).

you can first prepare array ip indexes , rows corresponding ip value attributes key. done -

$csvarray = str_getcsv(file_get_contents('mycsvfile.csv')); foreach($csvarray $eachrow) { //prepare ip indexed array every detail row attribute of corresponding ip key. $properlyformattedarray[$eachrow[5]][] = $eachrow; }

you array -

array(['92.27.21.171'] => [0] => array("mr","test","davis","07972889989","01159174767","92.27.21.171"), [1] => array("mr","bob","jones","07998998008","01159174767","92.27.21.171"), ... ['92.27.21.172'] => ... )

once have array, loop on it, , write @ max 2 rows every ip.

foreach($properlyformattedarray $ip => $details) { $count = 0; foreach($details $eachdetail) { if($count<2) { //write $eachdetail file $count++; } } }

but, in case, order of info (compared input file) changed, , duplicates written in consecutive rows in csv file (not sure if okay it).

php csv ip

No comments:

Post a Comment