Page 1 of 2

[SOLVED] how to write to a file?

Posted: Wed Feb 09, 2005 12:02 am
by saumya
friends I am new to php and trying to write to a file through this.I am using windows2000 iis and phph 5.0.

Now I have page having a form like below (2index.php)

Code: Select all

<html>
 <head>
  <title>Welcome to PHP world.</title>
 </head>
 <body>
 <?php
$today = date("F j, Y, g:i a");
print($today.'<br>');
print('There are '.date('t').' days in this month.');
 ?>
 <center>
 2 index
<form action="2hello.php" method="post">
 <p>Enter your Name: <input type="text" name="name" /></p>
 <p>Your Extension: <input type="text" name="extension" /></p>
 <p><input type="submit" value="Enter" /></p>
</form>
</center>
</body>
</html>


Now I have another page which writes to a file and displays all.The code for the Page (2hello.php) is like below

Code: Select all

<html>
<head>
  <title>Welcome</title>
</head>
<body>
<?php
print('Hello '.$_POST&#1111;'name'].' .<br>');
$fp = fopen("guests.txt", "r+b");
  $theUser = 'Person - '.$_POST&#1111;'name'].". ExtNo- ".$_POST&#1111;'extension']." .<br>";
  $guests = fread($fp,filesize("guests.txt"));
  $search = stripos($theUser,$guests);
  if($search=== false)&#123;
  print("can not find any match<br>");
  print("$search<br>")  ;
  fputs($fp,$theUser);
  &#125; else&#123;
  print("Match found.<br>") ;
  &#125;

  readfile('guests.txt');
fclose($fp);

?>
</body>
</html>
But the problem is when I press refresh it adds up the preveous form again in the text file.And yes if the file is empty then for the first time when I try to enter something and view then it gives me the follwing error message

Warning: fread() [function.fread]: Length parameter must be greater than 0. in D:\Saumya\PHP\2hello.php on line 10
can not find any match

So how to get rid of all this.
Thank you
Saumya




feyd | USE THE FORMATTING

Posted: Wed Feb 09, 2005 12:07 am
by feyd
fread must have a non-zero second argument. If the file was just created, with no data, the the filesize will return zero. Do not call fread if the filesize is zero.

i am worried about the refresh

Posted: Wed Feb 09, 2005 12:20 am
by saumya
Well thank you.I will look into it.But the most scariest thing is the refresh.If I refresh the data is added again.I do not want that.Please help.Tried a lot.
Saumya

Posted: Wed Feb 09, 2005 12:48 am
by thegreatone2176
if you want data to stop being posted on refresh check the referer since you have a separate html page where its submitted from.

Posted: Wed Feb 09, 2005 12:49 am
by feyd
just remember that the referrer may not exist.

What is referrer?

Posted: Wed Feb 09, 2005 1:23 am
by saumya
What is referrer? and how to check that?Help me with code Please.I am very new here.
Thanks
Saumya

Posted: Wed Feb 09, 2005 1:31 am
by feyd
$_SERVER['HTTP_REFERER']

know that it is an optional component of a request header.

but what to do?

Posted: Wed Feb 09, 2005 1:52 am
by saumya
well I got, it returns the url in string, but what to dowith that?
Saumya

Posted: Wed Feb 09, 2005 1:56 am
by anjanesh
Or send your page to another middle page first for validation and then redirect to 2hello.php.

Code: Select all

<form action="validatehello.php" method="post">  
...
</form>
In validatehello.php, after validation do :

Code: Select all

<META HTTP-EQUIV="Refresh" CONTENT="0;url=2hello.php">
If the browser does not automatically redirect you, then click <A HREF="2hello.php">here</A>
You must give the line "If the browser does not automatically redirect you, then click..." in case the browser doesnt allow redirection.

Posted: Wed Feb 09, 2005 1:08 pm
by Joe
anjanesh wrote:Or send your page to another middle page first for validation and then redirect to 2hello.php.

Code: Select all

<form action="validatehello.php" method="post">  
...
</form>
In validatehello.php, after validation do :

Code: Select all

<META HTTP-EQUIV="Refresh" CONTENT="0;url=2hello.php">
If the browser does not automatically redirect you, then click <A HREF="2hello.php">here</A>
You must give the line "If the browser does not automatically redirect you, then click..." in case the browser doesnt allow redirection.
Hmm not everyone allows meta refreshes on their local computer so you should think about using header() instead. Just remember to place it before any initial output.

code please

Posted: Fri Feb 11, 2005 9:15 am
by saumya
code please.CAn any one check my code and correct it.I am very new in PHP and start going ,this is my first script so it would be great if some body can help me get going.
Thank you
saumya

Posted: Fri Feb 11, 2005 11:18 am
by anjanesh
After looking through your code I was wondering how, on hitting the refresh button will the data be entered again because you are checking the existence of data in the files and you're writting only if its not present. Even if you refresh and rePOST the data should not be rewritten.
It seems you got the parameters wrong for stripos : int stripos ( string haystack, string needle [, int offset] )
Change

Code: Select all

$search = stripos($theUser,$guests);
to

Code: Select all

$search = stripos($guests,$theUser);
http://in.php.net/manual/en/function.stripos.php

Posted: Fri Feb 11, 2005 11:27 am
by feyd
warning: stripos() is php5 only.

Posted: Fri Feb 11, 2005 11:33 am
by anjanesh
feyd wrote:warning: stripos() is php5 only.
saumya wrote:friends I am new to php and trying to write to a file through this.I am using windows2000 iis and phph 5.0.

Posted: Fri Feb 11, 2005 11:34 am
by feyd
that was to make sure others finding the thread know it :roll: