Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

PHP help

Started by lexus727 Jun 25, 2006 at 7:04 PM 5 replies 1k views
Original Post
lexus727
lexus727
hey, im brand new 2 this site, but i desperatly need some php help, i didnt write this at all, i cant do php, but i need this desperatly on my site, herz teh error Warning: fclose(): supplied argument is not a valid stream resource in /home/elementa/public_html/lexus/process.p hp on line 10 AND the code $values) { echo $keys . " | " .$values; echo "
\n"; } ?> thanks a bunch guys,
jflanglois
jflanglois
It should probably be something like:

<?$name = $_POST['name'];$site = $_POST['site'];$mail = $_POST['email'];// write data$f = 'data.txt';$handle = fopen($f, 'a+');fwrite($handle, $name . " | " . $site . " | " . $mail . " | " . "\n");fclose($handle);//dump it$open = file($f);foreach ($open as $keys => $values) {echo $keys . " | " .$values;echo "
\n";}?>


Read the documentation.


jfl.
lexus727
lexus727
awesome, i just want it so that the user doesnt get redirected to the php, just gets a java error thats its been posted, herz the post code


Name:



Site Name:



eMail:




thanks an enourmes amount!!

~lexus
jflanglois
jflanglois
You would be better off just redirecting to the calling page from process.php. Ask yourself if it really is necessary to have a &#106avascript message box.
lexus727
lexus727
and how would i do that ( i told u that i dont know anything about php) thanks alot

~lexus
jflanglois
jflanglois
It would look something like this (not tested):
<head><?php  if( isset( $_GET['sender'] ) )    echo "<meta http-equiv=\"refresh\" content=\"2;url=" . $_GET['sender'] . "\">";?></head>

And your form code would be:


The 2 in the above is the number of seconds you stay on process.php, where you can tell the user that the information has been posted.
Shiny
Shiny
I guess I should make one or two points just in case:

1) IO routines are best done fast -- but when you're debugging, who cares about fast -- as long as they work. Hence, it's usually a good idea to wrap fopen calls inside conditionals.

So, with $f = 'data.txt';

if($file = fopen($f,"a+")
{
// if we managed to open $f, the resulting pointer to a file will be stored in $file.
// we can then manipulate data.txt through its pointer -- $file.
// If it didn't work, $file will return false and this conditional block never gets executed.

}
else
{
echo "Failed to open $f.";
}

So -- the point about reading the PHP docs is important -- if you don't know how fopen works (and it is quite simple really) then chances are when you use it...you'll get strange errors.
The reason you want to check if the fopen succeeded is this: if it didn't, any calls that refer to that file...will die -- and give you a message like the one you got.

In fact, the message you got means precisely this: That you tried to open/create a file for appending but that the operation was unsuccessful and the file resource (pointer to file) is NULL or false. Hence, when you tried to write to it...it complained, because the place you wrote to didn't exist (in PHP's view, anyway).

That said, while it is OK to rely on an implicitly returned file resource (pointer to file)...and thus use function calls like so fopen("blah.txt","r");...without assigning a result ($newfile = fopen("blah.txt","a+");)...it is a bad idea!

Why? Because in your example: fwrite($f, 'a+',$name . " | " . $site . " | " . $mail . " | " . "\n"); -- means this: write $name . " | " . $site . " | " . $mail . " | " . "\n" into file $f -- which is actually just a string saying 'data.txt' and not a file handle...furthermore, you've got 'a+' in the fwrite call which isn't required or will even let it work :)

So, go read the docs -- and while you are there, check out the Header() function.
Example -- header("Location: http://www.google.com"); -- sends HTTP headers telling the browser to redirect to google. HTTP headers themselves are a lengthy subject -- but the header function lets you do all sorts of cool stuff (refresh, redirect, put cookies etc).

~Shiny.
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.