Jump to content

Writing to a text file


starvator

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/206785-writing-to-a-text-file/
Share on other sites

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!');

?>

 

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

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.?

// 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.

// 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.