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

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.