第一个HTML / PHP表单给我500服务器错误

时间:2011-05-12 20:25:24

标签: php html

我的html文件:

<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'> &nbsp; (Be sure to review the article before pressing this button -- <b>there's no going back</b>)</form><br>

</body>
</html>

和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');

    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>

这是我第一次接触PHP,所以我很困惑。 当我转到html页面并“提交”我的表单时,我被重定向到:

  

HTTP错误500(内部服务器   错误):意外情况是   在服务器遇到时遇到   试图满足要求。

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

检查服务器的错误日志。应该有更多关于导致500错误的细节。您发布的错误消息是“友好”的公共错误消息,根据设计说的很少。

尝试运行一个非常基本的<?php echo 'hello world' ?>。如果这种情况爆发,那么你的PHP安装就会出现问题,这会导致在调用PHP时Web服务器爆炸。

答案 1 :(得分:1)

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');

    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>