There is an identical question to this issue from October, but it's unanswered. I'm not sure what the protocol is for that.
I'm working on a site purely for fun and to learn some PHP. This includes a form to post "news". Most of the time it goes through fine, but sometimes it arbitrarily doesn't and I'm given the 403 error
You don't have permission to access /news.php on this server.
Searching this scenario results in a lot of people talking about something called mod_sec, which I have no idea how to deal with if the web host I'm with uses it.
Below is all the code involved, but since it usually functions perfectly it may not help. The valid username and password don't include any of the characters altered by the function.
Edit: The error occurs when hitting the form submit button. Upon accidental investigation it happens before even reaching the database connection attempt and so does have absolutely nothing to do with the PHP code.
All of the included code is located on the same page (wasn't originally, but I moved it to eliminate that angle)
This is taking place online with an actual web host, so local file conflicts and antivirus etc. aren't to blame.
The site error log merely complains about a lack of 403 or 404 page, the access log just says this:
[24/Jan/2016:05:04:56 -0500] "POST /news.php HTTP/1.1" 404 - [URL] "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"
PHP:
function sanitiseText($text)
{
$output = nl2br($text);
$output = str_replace("'", "’", $output);
$output = str_replace("<", "<", $output);
$output = str_replace(">", ">", $output);
$output = str_replace('"', '"', $output);
return $output;
}
$a_un = sanitiseText($_POST['un']);
$a_pw = sanitiseText($_POST['pw']);
$mysql_admin = new mysqli(DB_SERVER, $a_un, $a_pw, B_NAME) or
die("Could not access the database");
if($_POST['postNews'])
{
$newsTitle = sanitiseText($_POST['newsTitle']);
$newsPost = sanitiseText($_POST['newsPost']);
$query = "SELECT * FROM news ORDER BY postID DESC";
$result = $mysql_admin->query($query);
$info = $result->fetch_assoc();
$pID = $info['postID'] + 1;
$query = "INSERT INTO news(postID, title, post, posted) VALUES ('$pID', '$newsTitle', '$newsPost', NOW())";
$mysql_admin->query($query);
header("Location:/");
}
HTML:
<form enctype='multipart/form-data' method='post' action='' name='newsform'>
Username: <input type="text" name="un" size="12" value=""/><br>
Password: <input type="password" name="pw" size="12" value=""/><br><br>
Title: <input type="text" name="newsTitle" size="40" value=""/><br><br>
Post:<br><textarea name="newsPost" cols="45" rows="5"></textarea><br><br>
<input type='submit' value='Make News Post' name='postNews'/>