Writing arrays into text files.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ReidRagang
    New Member
    • Mar 2010
    • 1

    Writing arrays into text files.

    I'm trying to set up a basic username/password login page and changing the password via writing back to a text file. Here is the code for the page.

    Code:
    <?php
    	//Confirmation for Changed Password
    
    	// Retrieve lists of username and password session variables
    	session_start();
    	$u_list = $_SESSION["u_list"];
    	$p_list = $_SESSION["p_list"];
    
    	print "<html>";
    	print "<body>";
    	print "<BODY bgcolor='#66ff66'>";
    	print "<center>";
    	print "<h1><b>Your Password has<br>Been Updated</b></h1>";
    	Print "<br><br><br><br><br><br><hr>";
    	print "<A HREF='a4_main.php'>Return to Main Page</A> | <A HREF='a4.php'>Logout</A>";
    
    	// Write lists of usarnames and passwords (with new password) back to text file
    	$fp = fopen("textfile.txt", "w");
    	for ($x=0; $x<count($u_list); $x++)
    	{
    		fputs($fp,$u_list);
    		fputs($fp,"\n");
    		fputs($fp,$p_list);
    		fputs($fp,"\n");
    	}
    	fclose($fp);
    	
    	print "</body>";
    	print "</html>";
    
    ?>

    When I change the password, and logout of the site, the text file "textfile.t xt" becomes blank. Help anyone?
    Last edited by Dormilich; Mar 11 '10, 05:11 AM. Reason: Please use [code] tags when posting code
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Have you verified that the data in your variables is correct?
    Try putting something like this around line #8.
    [code=php]echo '<pre>';
    var_dump($u_lis t);
    var_dump($p_lis t);
    echo '</pre>';[/code]


    Also, if you are going to be using a flat-file approach, you might want to consider using CSV. - PHP has two handy functions that work with them: fgetcsv and fputcsv.

    Comment

    • philipwayne
      New Member
      • Mar 2010
      • 50

      #3
      Not sure but you do have a problem with your loop.

      Code:
       for ($x=0; $x<count($u_list); $x++)
          {
              fputs($fp,$u_list);
              fputs($fp,"\n");
              fputs($fp,$p_list);
              fputs($fp,"\n");
          }
      If your $u_list and $p_list are really arrays your not indexing them.

      Code:
      $count = count($u_list);
       for ($x=0; $x<$count; $x++)
          {
              fputs($fp,$u_list[U][B][$x][/B][/U]);
              fputs($fp,"\n");
              fputs($fp,$p_list[U][B][$x][/B][/U]);
              fputs($fp,"\n");
          }
      Also I'd like to suggest serialization rather then the above poster's fgetcsv how ever either will work. If there not arrays then your trying to count something that shouldn't be, which would still return 1 on anything other then null which is a 0.

      Also do note I stuck the function outside the for loop, if you leave it in the function will be ran every time.

      Comment

      Working...