Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Variable and fwrite 1

Status
Not open for further replies.

Tuck007

Technical User
Apr 29, 2002
105
US
I understand how to write to a file. I'm using the file to store username:password in that format. I got this example from WebMonkey. My question is, how do I get me content variable to format with multiple varibales from my add form? Here is the code I am using. The form passes $newuser and $newpass:
Code:
<html>
<head>
<title>Add User</title>
</head>
<body>
<?php
if(isset( $submit ) && ($newpass == $cnewpass)) 
{ 
//If the Submitbutton was pressed do: 

$filename = '../login/f6kr2yb.txt';
$somecontent = $newuser, &quot;:&quot;, $newpass&quot;;

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it.
    if (!$fp = fopen($filename, 'a')) {
         print &quot;Cannot open file ($filename)&quot;;
         exit;
    }

    // Write $somecontent to our opened file.
    if (!fwrite($fp, $somecontent)) {
        print &quot;Cannot write to file ($filename)&quot;;
        exit;
    }
    
    print &quot;<center>Success, wrote ($somecontent) to file ($filename)</center>&quot;;
    
    fclose($fp);
					
} else {
    print &quot;The file $filename is not writable&quot;;
}
}
else{
?>
<form name=&quot;Add_User&quot; action=&quot;&quot; method=&quot;get&quot;>
<input type=&quot;hidden&quot; name=&quot;SID&quot; value=&quot;<?php echo $SID; ?>&quot;>
<table align=&quot;center&quot;>
<tr>
<td align=&quot;center&quot; colspan=&quot;2&quot;><b>Add User</b></td>
</tr>
<tr>
<td>Username :</td><td><input type=&quot;text&quot; name=&quot;newuserid&quot;></td>
</tr>
<tr>
<td>Password :</td><td><input type=&quot;password&quot; name=&quot;newpass&quot;></td>
</tr>
<tr>
<td>Confirm Password :</td><td><input type=&quot;text&quot; name=&quot;cnewpass&quot;></td>
</tr>
<tr>
<td align=&quot;center&quot; colspan=&quot;2&quot;><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Add User&quot;> <input type=&quot;reset&quot; name=&quot;reset&quot; value=&quot;Cancel&quot;></td>
</tr>
</table>
</form>
</body>
</html>
.:TUCK:.
 
there is a typo here, not enough &quot;
$somecontent = $newuser, &quot;:&quot;, $newpass&quot;;
if you only want
username:password then it should be this:
$somecontent=&quot;$newusere:$newpass&quot;; ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
if this is used:
Code:
$somecontent = $newuser, &quot;:&quot;, $newpass&quot;;
error occures:
Parse error: parse error, unexpected ',' in D:\ on line 13

if this is used:
Code:
$somecontent = &quot;$newuser:$newpass&quot;;
Response is:
Success, wrote (username:password) to file (../login/f6kr2yb.txt) .:TUCK:.
 
:) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
One tiny other thing I think I should have included:

How should I work in a new line?
Code:
$somecontent = &quot;\r$newuser:$newpass&quot;;
.:TUCK:.
 
like when you add the next line?

when you do this:
if (!fwrite($fp, $somecontent)) {
change it to add a newline:
if (!fwrite($fp, $somecontent.&quot;\n&quot;)) {

so when you append (fopen($file,'a')), it'll write on the next line :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR Said:
so when you append (fopen($file,'a')), it'll write on the next line

I don't think I want to do that. Using '\n' in my explode, it would read the last line as empty when reading, right?. This is from my auth validation, when $auth = 1, user has been validated:
Code:
<?php
$filename = &quot;f6kr2yb.txt&quot;;
$fp = fopen($filename, &quot;r&quot;);
$file_contents = fread($fp, filesize($filename));
fclose($fp);
$line = explode(&quot;\n&quot;, $file_contents);
$i = 0;
while($i <= sizeof($line)) {
	$data_pair = explode(&quot;:&quot;, $line[$i]);

	if (($data_pair[0] == $username) && ($data_pair[1] == $password)) {
		$auth = 1;
		break;
	}
	else {
		$auth = 0;
	}
	$i++;
	if($i == sizeof($line)){
	break;
	}
}
?>
.:TUCK:.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top