奇怪的禁止403错误 - 需要解决方案

时间:2015-10-22 12:55:38

标签: php apache .htaccess http-status-code-404 http-status-code-403

我有一个add-post.php页面的博客,其中包含一个带有操作的简单表单:

<form id="form" action="add-post-php.php" method="POST" enctype="multipart/form-data">

然后我将文件add-post-php.php放在与add-post.php相同的文件夹中。所以我在表单中输入我的博客的详细信息,然后按提交并获取:

  

禁止

     

您无权访问此服务器上的/add-post-php.php。

     

此外,尝试时遇到404 Not Found错误   使用ErrorDocument来处理请求。

  • 我在本地主机上测试了这个并且它正常工作
  • 该文件的权限设置为0644,但我也在0755尝试过,没有任何改进。
  • 我的.htaccess文件没有任何问题,目录中没有其他.htaccess文件。
  • 我尝试使用表单action
  • 中的完整网址路径

我的add-post-php.php脚本完全是:

<?php
include("php/settings.php"); // Contains DB Connections
?>
<?php
$id= time();
$month = date("m");
$year = date("Y");
$path = "images/posts/$year/$month/";

$section = $_POST["section"];
$category = $_POST["category"];
$credit = $_POST["credit"];
$title = ucwords($_POST["title"]);
$text = $_POST["text"];
$exclusive = $_POST["exclusive"];

$added = date("Y-m-d H:i:s");

$photo = $_FILES["photo"]["name"];
$ext = substr(strrchr($photo, '.'), 1);
?>
<?php
$insert_sql = "INSERT INTO posts (id, section, category, credit, title, article, exclusive, added) VALUES('$id', '$section', '$category', '$credit', '$title', '$text', '$exclusive', '$added')";
$insert_res = mysqli_query($con, $insert_sql);
if(mysqli_affected_rows($con)>0){
move_uploaded_file($_FILES["photo"]["tmp_name"],"$path" . $id . "." . $ext);
}
else{
echo "0";
exit();
};
?>
<?php
header("Location: post.php?id=$id");
exit();
?>

这是我的.htaccess:

Options -MultiViews

DirectoryIndex posts.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    

RewriteRule ^posts/([0-9]+)/?$              posts.php?currentpage=$1 [NC,L,QSA]

RewriteRule ^section/([\w-]+)/?$            section.php?section=$1 [NC,L,QSA]
RewriteRule ^section/([\w-]+)/([0-9]+)/?$   section.php?section=$1&currentpage=$2 [NC,L,QSA]

RewriteRule ^posts/([\w-]+)/?$              posts.php?category=$1 [NC,L,QSA]
RewriteRule ^posts/([\w-]+)/([0-9]+)/?$     posts.php?category=$1&currentpage=$2 [NC,L,QSA]

RewriteRule ^post/([0-9]+)/([\w-]+)/?$      post.php?id=$1&title=$2 [NC,L,QSA]

RewriteRule ^sites/([0-9]+)/?$              sites.php?currentpage=$1 [NC,L,QSA]

RewriteRule ^posts posts.php
RewriteRule ^section section.php
RewriteRule ^sites sites.php

RewriteRule ^about about.php
RewriteRule ^advertise advertise.php
RewriteRule ^subscribe subscribe.php

和文件夹结构:

enter image description here

我检查了php error log,那里没有任何可疑之处。当文件明显存在且权限是否正确时,有没有人知道为什么我会收到Forbidden Error?

1 个答案:

答案 0 :(得分:0)

我发现问题出在include文件中的add-post-php.php语句中:

<?php
include("php/settings.php"); // Contains DB Connections
?>

我把它拿出来改为:

<?php
//Database Connect
$con = mysqli_connect("localhost","username","password","database");
?>

一旦我编辑了这行代码,脚本就会起作用。