starvator Posted July 5, 2010 Share Posted July 5, 2010 I have a form with the following code: <form name="verification" action="verification1.php" method="post"> Code 1: <input type="text" name="code1" /><br> Code 2: <input type="text" name="code2" /><br> <input type="checkbox" name="agree" value="yes" /> I agree to the terms<br> <input type="submit" value="Submit" /> And a verification1.php with this code: <?php // Include Session Object (Which includes Other Files and Initializes all objects) include('include/session.php'); // Get the user information from Database $userinfo = $database->getUserInfo($session->username); // Store results If Correct: if($_POST['code1'] != "chicken"){ exit('Code 1 Mismatch<br />'); } if($_POST['code2'] != "dog"){ exit('Code 2 Mismatch<br />'); } if(!isset($_POST['agree'])){ exit('You did not agree to the Terms & Conditions'); // If you get this even if you click, change != "yes" to != "checked" } $filename = "textfile.txt"; $f = fopen($filename, 'a+'); # Check: http://php.net/fopen fclose($f); # Close file. echo('Success!'); ?> I do not get any errors, it sais success but nothing is writen to the file. What is wrong? Thanks! I tried changing $userinfo = $database->getUserInfo($session->username); to $userinfo = $session->username but this did not work Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2010 Share Posted July 5, 2010 You are opening and closing the file, but not writing to it. Did you proof-read your code to make sure it was attempting to write anything to the file? Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081410 Share on other sites More sharing options...
starvator Posted July 5, 2010 Author Share Posted July 5, 2010 You are opening and closing the file, but not writing to it. Did you proof-read your code to make sure it was attempting to write anything to the file? Yes, it is writing, i forgot to put that in here... sry... <?php // Include Session Object (Which includes Other Files and Initializes all objects) include('include/session.php'); // Store results If Correct: if($_POST['code1'] != "chicken"){ exit('Code 1 Mismatch<br />'); } if($_POST['code2'] != "dog"){ exit('Code 2 Mismatch<br />'); } if(!isset($_POST['agree'])){ exit('You did not agree to the Terms & Conditions'); // If you get this even if you click, change != "yes" to != "checked" } $filename = "textfile.txt"; $f = fopen($filename, 'a+'); # Check: http://php.net/fopen fwrite($f, "\n",$session->username); fclose($f); # Close file. echo('Success!'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081412 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 The fwrite function takes 3 parameters: the pointer to the opened file, the string to be written, and the maximum length of the string to be written. You have <?php fwrite($f, "\n",$session->username); ?> Your third parameter is a string, which, most likely, will get treated as the number zero, so you're not writing anything. I believe you want <?php fwrite($f, "\n" . $session->username); ?> i.e. concatenate the "\n" and the string. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081441 Share on other sites More sharing options...
starvator Posted July 5, 2010 Author Share Posted July 5, 2010 The fwrite function takes 3 parameters: the pointer to the opened file, the string to be written, and the maximum length of the string to be written. You have <?php fwrite($f, "\n",$session->username); ?> Your third parameter is a string, which, most likely, will get treated as the number zero, so you're not writing anything. I believe you want <?php fwrite($f, "\n" . $session->username); ?> i.e. concatenate the "\n" and the string. Ken PERFECT! wow it works!! now, if it is not too complicated, is there a way to check and see if a username is already in the file and if it is it will not be written again? Thanks!!!! (like if $session->username = one entire line, then echo you have already submitted your username.? Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081443 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 Use a database instead of a flat file. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081445 Share on other sites More sharing options...
starvator Posted July 5, 2010 Author Share Posted July 5, 2010 Use a database instead of a flat file. Ken I can't because i am limited to so few databases and tables because im using a free host. Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081449 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2010 Share Posted July 5, 2010 // Get the user information from Database $userinfo = $database->getUserInfo($session->username); ^^^ you apparently already have a table of user information in a database, why are you even messing around with a file? If you are just verifying acceptance of some condition, you would just add a 'verification' or a 'status' column to your user table. Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081452 Share on other sites More sharing options...
starvator Posted July 5, 2010 Author Share Posted July 5, 2010 // Get the user information from Database $userinfo = $database->getUserInfo($session->username); ^^^ you apparently already have a table of user information in a database, why are you even messing around with a file? If you are just verifying acceptance of some condition, you would just add a 'verification' or a 'status' column to your user table. It's because i have a login and verification system put in place but this is for a giveaway. I will be having giveaways and the user needs to find the secret codes, put them in and they get entered in the giveaway. I do not have enough tables to be able to do this. Quote Link to comment https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/#findComment-1081454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.