Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello so I have this csv check that breaks when the zipcode isn't in the csv. pretty logical because my csv file is really big (around 12000 lines). but I don't know what else to do.
so I'm using cookies because I want to show the results in my javascript table.
the code should work like this:
$zipcode = "5483 XK";
$housenumber = 46;

if zipcode + housenumber combo is in db send a true cookie

if they don't exist send false cookie

can someone help me
// csv code werkt wel tot ik de set cookie gebruik om
      $csv_file = "NewPostcodesCheckt.csv";

      // Open the file for reading
      $file_handle = fopen($csv_file, "r");

      // Read the contents of the file
      while (($row_data = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
        if ($row_data[0] == $Zipcode && $row_data[1] == $Housenumber ) {
            // User input found in the CSV
            setcookie('cookie_test', 'true' , time() + 1000);
            break;
        }else{
          //user not found in csv
          //doordat de false geen break kan doen word de response headers to big
           setcookie('cookie_test', 'false', time() + 1000);


        }

      }
      // Close the file handle
      fclose($file_handle);


What I have tried:

the example above is what I tried. I also tried giving both a break but then they would only check the first row

this is the error that you get:
net::ERR_RESPONSE_HEADERS_TOO_BIG
Posted
Updated 21-May-23 22:17pm
v2

1 solution

Erm ... you look at each row, and either set a cookie and exit (if the user is found on this row.), or set a cookie and continue to process the loop if not.

So, assuming users are unique and you have say 1000 rows of CSV data, you will set between 1 and 1000 cookies. Since cookies are part of the response header, the sheer volume of cookies is probably what is causing the error.

I'd suspect that the else case should not be inside the loop, but only executed once if the user is never found in the data.
 
Share this answer
 
Comments
Rebecca2002 22-May-23 4:23am    
true but how would I write it then? because I do need the users to know if its in the csv or not. if I just say:

// Read the contents of the file
while (($row_data = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
if ($row_data[0] == $Zipcode && $row_data[1] == $Housenumber ) {
// User input found in the CSV
setcookie('cookie_test', 'true' , time() + 1000);
break;
}

}
setcookie('cookie_test', 'false' , time() + 1000);

it would always hit false
Richard Deeming 22-May-23 4:27am    
Use a variable, and set the cookie outside of the loop.
$found = false;
while (($row_data = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
    if ($row_data[0] == $Zipcode && $row_data[1] == $Housenumber ) {
        // User input found in the CSV
        $found = true;
    }
}
if ($found) {
    setcookie('cookie_test', 'true' , time() + 1000);
} else {
    setcookie('cookie_test', 'false', time() + 1000);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900