SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Help with PHP??
-
Sep 11, 2001, 01:35 #1
- Join Date
- Jul 2001
- Location
- Tennessee
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Help with PHP??
I have just moved my site to a unix platform. Previously, I had an asp page where I was able to read a file, and based on the contents redirect to another page. The asp page had no html content, it just did the redirect. If the contents of the file was 1 it redirected to one.htm, 2 to two.htm and so on. Once I read the file I incremented and rewrote the file.
I think I should be able to do the same thing in PHP, but I don't know how to open a file, read a file, write a file or code the redirect. Does any one have a basic script that does this? Or perhaps send me to a site that where I can find one.
I just used PHP for the first time today!
Thanks!
-
Sep 11, 2001, 03:31 #2
- Join Date
- Jan 2001
- Location
- Lawrence, Kansas
- Posts
- 2,066
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm presuming you've got a basic understanding of PHP variables...
This code will load the contents of a file into a variable:PHP Code:$filename = 'file.txt';
$fp = @fopen($filename, 'r');
if (!$fp)
die('Could not open file.');
$contents = fread($fp, filesize($filename));
fclose($fp);
PHP Code:<?php
header("Location: anotherpage.html");
?>
Hope that helps,
Skunk
-
Sep 11, 2001, 14:19 #3
- Join Date
- Jul 2001
- Location
- Tennessee
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is very helpful, thank you!!
-
Sep 12, 2001, 00:20 #4
- Join Date
- Jul 2001
- Location
- Tennessee
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
header function help
I am receiving a parse error on the line with the first header function. What is wrong with it?
<html>
<head>
<title>Redirect to OrderForm</title>
</head>
<?php
$filename = 'file.txt';
$fp = fopen ($filename);
if(!$fp)
die("Could not open file.');
$contents = fread ($fp, filesize ($filename));
if ($contents< 3)
$contents= $contents+1;
else
$contents=1;
endif;
fwrite( $fp, $contents);
fclose( $fp );
if ($contents ==1)
header ("Location: OrderForm_dp.htm");
if ($contents ==2)
header ("Location: OrderForm_ed.htm");
if ($contents >2)
header ("Location: OrderForm_pr.htm");
?>
</body>
</html>
-
Sep 12, 2001, 00:32 #5
- Join Date
- Jun 2001
- Location
- Dublin
- Posts
- 221
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
quoting skunk:The redirection code needs to be called BEFORE any HTML is sent to the browser as it creates a "header" for the page.
At a guess, you should have got a warning to say headers already sent as you have output HTML before you try and send a header which you can't do unless you have output buffering ON.
Remove the:
<html>
<head>
<title>Redirect to OrderForm</title>
</head>
as you don't need to send anything if you redirect.
and the
</body>
</html>
as you will have been redirected by then.Last edited by lveale; Sep 12, 2001 at 00:36.
-
Sep 12, 2001, 01:01 #6
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes follow Iveale's advice because once you fix your parse error the next error would be a runtime error caused by having sent out HTML and then trying to send out a HTTP header - PHP will not send HTTP headers once the script has started outputing HTML. (note to Iveale - no run time error was thrown because the script was never compiled or executed. Because of poorly formed syntax it failed pre-compilation parsing).
Another problem is your control structures. There are two ways to impliment control structures and your syntax does not strictly adhere to either of them.
Style One:
PHP Code:if (condition):
//do true
else:
// do false
endif;
PHP Code:if (condition)
{
// do true
}
else
{
// do false
}
While in C you can get away with not having curly braces at all and this means that only the next immediate statement after the condition is executed - note that this is bad coding style and additionally, this is problematic in PHP because (being interpreted) not everything is a function call even though it might look like it. For example include() and echo() are language constructs (kind of like macros).
So my recommendation is to alter your code to reflect the second coding style and this will not only hopefully solve your syntax errors but mean that everyone on the forums will be able to understand your code betterLast edited by freakysid; Sep 12, 2001 at 01:04.
-
Sep 12, 2001, 05:52 #7
- Join Date
- Jul 2001
- Location
- Tennessee
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Again, Thanks for the help. I have made the changes suggested and it is working, but..
It does not seem to be receiving the $contents value from file.txt. I receive no error on the open. but the file.txt does not change. I assume it is the read or the write which is the problem.
Thank you again.
-
Sep 12, 2001, 06:04 #8
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I am suprised that you did not get a warning when you tried to call fwrite() - but the error reporting depends on which level it has been set to in the php.ini config file.
You need to open the file with "r+" (for read and write mode).
eg:
$fp = fopen($filename, "r+");
Also - don't forget to make sure that the user apache is running as (usually "nobody") has write permissions over the file.
Bookmarks