If statement being completely ignored?

cyberpirates.infinityfreeapp.com

No error message (there should be one)

I am trying to include a catch for when users create posts on my forum with too many characters, and the logic I’ve written in PHP works locally on my desktop. When I upload the code to the hosting server however, it completely bypasses the if statement I wrote and makes the post anyway.

//check post contains content
    if (!strlen(trim($_POST["content"])) > 0) {
        header("Location: ../MessageBoard/index.php?error=nopostcontent");
        exit();
    } else if (strlen(trim($_POST["content"]) > 1000)) {
        header("Location: ../MessageBoard/index.php?error=toomanychars");
        exit();
    } else {
        //insert the post

it might be worth noting that “if (!strlen(trim($_POST[“content”])) > 0)” properly returns, but "else if (strlen(trim($_POST[“content”]) > 1000)) " does not

I’ve changed the code slightly and it works now

//check post contains content
    $postcontent = $_POST["content"];
    if (!strlen(trim($postcontent)) > 0) {
        header("Location: ../MessageBoard/index.php?error=nopostcontent");
        exit();
    } else if (strlen($postcontent) > 1000) {
        header("Location: ../MessageBoard/index.php?error=toomanychars");
        exit();
    } else {
        //insert post

I think it was something to do with trim() being used to remove whitespace from the characters which is leftover as I just copied it from the “no content” check and modified it

1 Like

Why would you have it like this? That’s just completly pointless. Why wouldn’t you just change the > operator to be <, which would give the same output and would make far more sense?

If you’re talking about this, then it should be:

// check post contains content
$postcontent = $_POST["content"];
// not more than zero should be less than one.
if (strlen(trim($postcontent)) < 1) {
    // do something
}
2 Likes

Why would you have it like this? That’s just completly pointless. Why wouldn’t you just change the > operator to be < , which would give the same output and would make far more sense?

If you’re talking about this, then it should be:

Thanks guys these are the solutions I asked for good job

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.