First HTML/PHP form is giving me a 500 server error
My html file:
<html>
<head><title>RSS Form</title></head>
<body>
<form method='post' action='write.php'>
<img src='logo.png' align='left' />
<font size='6'>RSS Feed</font><br>
The feed that just keeps on giving...<br>
<p>Title:<br>
<input type='text' name='title' size='84' /><br></p>
<p>Article Body:<br>
<textarea rows='20' cols='100' wrap='physical' name='desc'></textarea><br></p>
<input type='submit' value='Post RSS' name='submit'> (Be sure to review the article before pressing this button -- <b>there's no going back</b>)</form><br>
</body>
</html>
and Write.php
<html><body>
<?php
$file_name = 'rss.xml';
if !(file_exists($file_name)) {
initialize_xml($file_name);
}
$rss = fopen($file_name, 'w+') or die('can\'t open file');
remove_tags($rss);
write_content($rss);
close_tags($rss);
finish();
function initialize_xml($name) {
$rss = fopen($name, 'w') or die('can\'t open file');
f开发者_运维知识库write($rss, "<?xml version=\"1.0\" ?>\n");
fwrite($rss, "<rss version=\"2.0\">\n");
fwrite($rss, "<channel>\n");
fwrite($rss, "<title>---</title>\n");
fwrite($rss, "<description>This feed will keep users up to date on IT issues that may arise</description>\n");
fwrite($rss, "<link>---</link>\n");
fwrite($rss, "<managingEditor>---</managingEditor>\n");
fwrite($rss, "<webMaster>---</webMaster>\n\n");
close_tags($rss);
fclose($rss);
}
function write_content($rss) {
fwrite($rss, '<item>\n');
fwrite($rss, '<title>');
fwrite($rss, $_POST['title']);
fwrite($rss, '</title>\n');
fwrite($rss, '<description>');
fwrite($rss, $_POST['desc']);
fwrite($rss, '</description>\n');
fwrite($rss, '<date>');
$today = getdate();
$timestamp_format = $today[weekday] + ' ' + $today[month] + ' ' + $today[mday] + ' ' + $today[hours] + ' ' + $today[minutes] + ' ' + $today[seconds];
fwrite($rss, $timestamp_format);
fwrite($rss, '</date>');
}
function close_tags($rss) {
fwrite($rss, '</channel>\n');
fwrite($rss, '</rss>\n');
fwrite($rss, '</xml>\n');
}
function remove_tags($rss) {
// go to end of file
// remove last 3 lines
}
function finish() {
echo 'The article ';
echo $_POST['title'];
echo ' has been added to the feed.\n';
echo '<a href="index.html">Go Back</a>';
}
?>
</body></html>
This is my first contact with PHP so I'm quite confused. When I go to the html page and "submit" my form, I get redirected to:
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.
Thanks for your help
Check the server's error log. There should be more details about what's causing the 500 error. The error message you posted is the "friendly" public error message which by design says very little.
Try running a very basic <?php echo 'hello world' ?>
as well. If that blows up, then there's some wrong with your PHP installation which is causing the webserver to blow up when PHP is invoked.
Some updates to your PHP code:
<html><body>
<?php
$file_name = 'rss.xml';
if (!file_exists($file_name)) {
initialize_xml($file_name);
}
$rss = fopen($file_name, 'w+') or die('can\'t open file');
remove_tags($rss);
write_content($rss);
close_tags($rss);
finish();
function initialize_xml($name) {
$rss = fopen($name, 'w') or die('can\'t open file');
fwrite($rss, "<?xml version=\"1.0\" ?>\n");
fwrite($rss, "<rss version=\"2.0\">\n");
fwrite($rss, "<channel>\n");
fwrite($rss, "<title>---</title>\n");
fwrite($rss, "<description>This feed will keep users up to date on IT issues that may arise</description>\n");
fwrite($rss, "<link>---</link>\n");
fwrite($rss, "<managingEditor>---</managingEditor>\n");
fwrite($rss, "<webMaster>---</webMaster>\n\n");
close_tags($rss);
fclose($rss);
}
function write_content($rss) {
fwrite($rss, "<item>\n");
fwrite($rss, '<title>');
fwrite($rss, $_POST['title']);
fwrite($rss, "</title>\n");
fwrite($rss, '<description>');
fwrite($rss, $_POST['desc']);
fwrite($rss, '</description>\n');
fwrite($rss, '<date>');
$today = getdate();
$timestamp_format = $today['weekday'] . ' ' . $today['month'] . ' ' . $today['mday'] . ' ' . $today['hours'] . ' ' . $today['minutes'] . ' ' . $today['seconds'];
fwrite($rss, $timestamp_format);
fwrite($rss, '</date>');
}
function close_tags($rss) {
fwrite($rss, "</channel>\n");
fwrite($rss, "</rss>\n");
fwrite($rss, "</xml>\n");
}
function remove_tags($rss) {
// go to end of file
// remove last 3 lines
}
function finish() {
echo 'The article ';
echo $_POST['title'];
echo " has been added to the feed.\n";
echo '<a href="index.html">Go Back</a>';
}
?>
</body></html>
精彩评论