[SOLVED] how to write to a file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

[SOLVED] how to write to a file?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

i am worried about the refresh

Post 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
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

just remember that the referrer may not exist.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

What is referrer?

Post by saumya »

What is referrer? and how to check that?Help me with code Please.I am very new here.
Thanks
Saumya
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_SERVER['HTTP_REFERER']

know that it is an optional component of a request header.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

but what to do?

Post by saumya »

well I got, it returns the url in string, but what to dowith that?
Saumya
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

code please

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

warning: stripos() is php5 only.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that was to make sure others finding the thread know it :roll:
Post Reply