2

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("<", "&lt;", $output);
    $output = str_replace(">", "&gt;", $output);
    $output = str_replace('"', '&quot;', $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'/>
  • Does any other PHP error get generated, or an apache server error, aside from a 404 request? I assume the HTML and PHP code in your question both reside on the page /news.php? – Martin Jan 23 '16 at 23:46
  • 1
    your `header()` command at the bottom of the PHP is bad. You should specify an unambiguous file, such as `header('Location: /index,php');` or even better, a valid URL. – Martin Jan 23 '16 at 23:47
  • I assume you test it on localhost, so I think it can be open file issue, antivirus issue, etc. – Jan Rydrych Jan 23 '16 at 23:48
  • At what point does the 403 error occur? When you load the page fresh, when you load the page from being elsewhere on the site or after you submit the form? Also can you link the October question if that has more specific details? – Martin Jan 23 '16 at 23:51
  • And I would also check the webserver access log file... – Jan Rydrych Jan 23 '16 at 23:53
  • The error you've shown us has nothing to do with the code you've quoted. You say you!'re asking the same question again but have not provided a link to the original question, and I don't see it on your profile. You are not making it easy to answer. – symcbean Jan 24 '16 at 01:39
  • @Martin - The error occurs when hitting the form submit button. This is taking place online with an actual web host. 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" –  Jan 24 '16 at 10:08
  • The October question wasn't posted by me and doesn't really have any more information, but [here it is](https://stackoverflow.com/questions/32876751/forbidden-you-dont-have-permission-to-access-script-php-on-this-server-but?rq=1) –  Jan 24 '16 at 10:11
  • And does your database collect the saved data correctly, to distinguish that the error occurs *after* the database has successfully run? – Martin Jan 24 '16 at 10:41
  • @Martin I messed up the password entry and discovered that the error actually occurs before the database connection is even made. It's like something goes wrong between hitting the form button and the if($_POST) code being triggered. –  Jan 24 '16 at 10:47
  • haha, ignore my fresh answer then I'll edit it! – Martin Jan 24 '16 at 10:48
  • can you show what the 403 error says? You have only put up the 404 error so far. Thanks – Martin Jan 24 '16 at 10:50
  • Have you read this post and does it help at all? http://stackoverflow.com/questions/10873295/error-message-forbidden-you-dont-have-permission-to-access-on-this-server – Martin Jan 24 '16 at 10:52
  • @Martin it might be easier to understand if you use the form http://scissle.com/news.php, I removed the need to use the log-in. The last post in the table reliably produces the error if you copy it into the box. –  Jan 24 '16 at 10:58

1 Answers1

0

Trial and error:

  • Set the proper (full) page URL into the form action field. Part-done

  • Show any details from your .htaccess file or similar permissions settings. None.

  • Do you use any SymLinks on your filespace? No.

  • I found no character encoding in your page, news.php so suggest you add character encoding to the headers and to the form with accept-charset="utf-8" inside your <form> tag.

  • Just incase this is a parse error with PHP add a PHP error logging add

    ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

to the top of the news.php page.


Recoding your HTML. I think a possible cause is that the server is getting confused by the poor quality of the HTML page, so here is a fix up of the page, (partly that and the fact I find it hard not to fix these sort of things!). Please copy/paste this as the HTML for news.php and then see how often the 403 error occurs.

It is worth noting that you do not have a 404 error, the 404 is caused by the 403 looking for a corresponding error page to display to the browser, and as you have none, there is a 404 file not found. The 404 is not important.

Code (upgraded to HTML5):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>News Page - Scissle</title>
<link href="sheet.css" rel="stylesheet" media="screen" type="text/css" /></head>
<body>
<div id="border">
</div>
<div id="content">
<center> <!-- Do not use <center> tags, use CSS-->
    <form enctype='multipart/form-data' method='post' action='http://scissle.com/news.php' name='newsform' accept-charset="utf-8">

    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' />
</form>
<br>
<hr>
<table class="book"><tr class="heading"><td>Some points:

1) Encase CSS classes in quotes.
2) Remember to close html / body/head tags
3) Try to avoid blank spaces/ empty lines at the start or end of the HTML file.
4) remember to close your Form tags too.
</td></tr></table></center>
</div>
</body> 
</html>

From Comments:

The error occurs when hitting the form submit button. This is taking place online with an actual web host.

Therefore I would place a strong suspicion that the error is caused by the improper / inexact usage of PHP header() function, as referenced in my comments.

Replace your header block with the following:

    ...
    $mysql_admin->query($query);
    header("Location: http://scissle.com/index.php");
    exit;
    }

This will give the server an exact address to follow rather than a simple / as you currently have.

Also added an exit; after the header as recommended.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • I don't think I have a .htaccess file, I've not set one up myself and can't find evidence of a default one. All the pages have permissions of 0644 I'm not entirely sure what you mean by SymLinks, so I'm guessing the answer to that is no? –  Jan 24 '16 at 11:05
  • @Scissle do you manage your permissions within PHP entirely or with httpauth or similar server level stuff? I have updated my bullets a little bit. – Martin Jan 24 '16 at 11:06
  • I basically have no idea what I'm doing, so no server level stuff. I've added the code bits you advised, but I can't see it having affected anything.. Thank you for all your help so far, though! –  Jan 24 '16 at 11:11
  • @Scissle I suspect the issue may be something server level such as a less than perfect hosting plan, but reading your website and seeing the comments here it's well worth reading up on how to setup PHP authentication for logging people in and out as admins on your website. Do not store the password in the cookies/sessions for example. Sorry I can't see more clearly what's going on here. – Martin Jan 24 '16 at 11:14
  • @Scissle Also use a HTML5 template as your current page layouts are invalid and they will possibly be the cause of the issue. Your structure should be `` but you have CSS style sheets in between the two when they should be in the `HEAD` section. etc. https://html5boilerplate.com/ would be a good start :-) – Martin Jan 24 '16 at 11:17
  • Well above my pay grade, then! Never mind, thank you for trying. Oh I know, it's on the list! This is just a hobby project, so no real stakes at the moment. But I'm very much still learning so thank you for all your advice. –  Jan 24 '16 at 11:19
  • @Scissle also make use of https://validator.w3.org/ to validate your output. I'm probably bamboozling you now but I think fixing the page code will definietely help iron out the 404/403 problems – Martin Jan 24 '16 at 11:19
  • @Scissle don't be disheartened, it can be done! I'm pretty sure ironing out the wrinkles will fix these bumps along the way (does that makes sense? lol) – Martin Jan 24 '16 at 11:21
  • @Scissle now I'm getting constant 403 errors. finally! But no further clues as to the cause :-( – Martin Jan 24 '16 at 11:30
  • @Scissle ok final comment, I've edited my question and added a fixed HTML file for `news.php` for you, see if this makes the issue disappear? – Martin Jan 24 '16 at 11:42
  • I've copied your amendments exactly (thank you, it seems there's a lot I don't know about HTML!) but still having the same results as ever, most tests work perfectly but every now and then it just doesn't :/ –  Jan 24 '16 at 12:36