Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Storing php cookie values in csv file on a new line each time program is run

For a leaderboard for a game I am trying to use a csv file to store the cookie value for username and the corresponding user’s score. The code seems to be functional when its runs first but when another user completes the quiz the line of the csv file written with the previous user’s username and score is overwritten and replace with the current user’s username and score.

<?php session_start();

 $scoreCookie = $_COOKIE['scoreCookieToPost'];

 $leaderboard_data = array (
 $_COOKIE['usernamecookie'],
 $scoreCookie
);

$file = fopen("leaderboarddata.csv","w");


fputcsv($file, $leaderboard_data);


fclose($file);

?>

What changes would need to be made so each new submission would be put in a new row in the csv file and the first line would say Username,Score.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You would want to use the append mode of fopen ‘a’.

<?php session_start();

 $scoreCookie = $_COOKIE['scoreCookieToPost'];

 $leaderboard_data = array (
 $_COOKIE['usernamecookie'],
 $scoreCookie
);

$file = fopen("leaderboarddata.csv","a"); // a


fputcsv($file, $leaderboard_data );


fclose($file);

?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading