-1

so I have this php code for my website I'm working on, it's a messaging site, it was working earlier today, but then I tried adding a new password feature, which wasn't working so I restored what I thought was a working version. On the earlier version if I hit the the enter button on the login page and it'd take me straight to the website, now it's giving me this weird issue where when I enter the site for the "first time" it gives me a 500 error, and it's telling me the url is: http://3.99.183.22/index.php, I then have to hit the back button and then it works, and the chat screen loads, with the url: http://3.99.183.22/#, I can then edit the url to be: http://3.99.183.22/index.php and it works still. It's also not working on mobile because of this issue now. Any ideas?

<?php

 

session_start();

 

if(isset($_GET['logout'])){    

     

    //Simple exit message

    $logout_message = "<div class='msgln'><span class='left-info'>User <b class='user-name-left'>". $_SESSION['name'] ."</b> has left the chat session.</span><br></div>";

    file_put_contents("log.html", $logout_message, FILE_APPEND | LOCK_EX);

     

    session_destroy();

    header("Location: index.php"); //Redirect the user

}

 

if(isset($_POST['enter'])){

    if($_POST['name'] != ""){

    $username=$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
//    echo $username;
   // $password = $_SESSION['password'] = stripslashes(htmlspecialchars($_POST['password']));
   // echo $password;
    $dbname = 'Hermes_Global';

    $dbuser = 'dbmasteruser';

    $dbpass = 'youdonthavetoseemypassword';

    $dbhost = 'someawsconnectionthatsworking';

    

    $link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");

    mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");

    $sql = "INSERT INTO user_test (username) VALUES(?)";
    $stmt = $link->prepare($sql);

    $stmt->bind_param("s", $username);

    $stmt->execute();
//    if($stmt->execute()) {
//  echo "New record entered successfully";
//  }
//  else {
//  echo $stmt->error;
//  }

    mysqli_close($link);

    }

    else{

        echo '<span class="error">Please type in a name</span>';

    }

}

 

function loginForm(){

    echo

    '<div id="loginform">

    <p>Please enter your a username</p>

    <form action="index.php" method="post">

      <label for="name">Username</label>

      <input type="text" name="name" id="name" />

      <input type="submit" name="enter" id="enter" value="Enter" />

    </form>

   

  </div>';

}

 

?>

 

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8" />

 

        <title>Hermes Chat Application</title>

        <meta name="description" content="Hermes Chat Application" />

        <link rel="stylesheet" href="style.css" />

    </head>

    <body>

    <?php

    if(!isset($_SESSION['name'])){

        loginForm();

    }

    else {

    ?>

        <div id="wrapper">

            <div id="menu">

                <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>

                <p class="logout"><a id="exit" href="#">Exit Chat</a></p>

            </div>

 

            <div id="chatbox">

            <?php

            if(file_exists("log.html") && filesize("log.html") > 0){

                $contents = file_get_contents("log.html");

                echo $contents;

            }

            ?>

            </div>

 

            <form name="message" action="">

                <input name="usermsg" type="text" id="usermsg" />

                <input name="submitmsg" type="submit" id="submitmsg" value="Send" />

            </form>

        </div>

        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

        <script type="text/javascript">

            // jQuery Document

            $(document).ready(function () {

                $("#submitmsg").click(function () {

                    var clientmsg = $("#usermsg").val();

         //   console.log(clientmsg);
                    $.post("post.php", { text: clientmsg });

                    $("#usermsg").val("");
           
                    return false;

                });

 

                function loadLog() {

                    var oldscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height before the request

 

                    $.ajax({

                        url: "log.html",

                        cache: false,

                        success: function (html) {

                            $("#chatbox").html(html); //Insert chat log into the #chatbox div

 

                            //Auto-scroll           

                            var newscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height after the request

                            if(newscrollHeight > oldscrollHeight){

                                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div

                            }   

                        }

                    });

                }

 

                setInterval (loadLog, 2500);

 

                $("#exit").click(function () {

                    var exit = confirm("Are you sure you want to end the session?");

                    if (exit == true) {

                    window.location = "index.php?logout=true";

                    }

                });

            });

        </script>

    </body>

</html>

<?php

}

?>

Note that I also have a file called post.php, which looks like this:

<?php
session_start();
if(isset($_SESSION['name'])){
    $text = $_POST['text'];
    $dbname = 'Hermes_Global';

    $dbuser = 'dbmasteruser';

    $dbpass = 'mypassword'; //i'm not publicly sharing the passwords

    $dbhost = 'theawsdatabase';

    

    $link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
    echo $text;
    $text_message = "<div class='msgln'><span class='chat-time'>".date("g:i A")."</span> <b class='user-name'>".$_SESSION['name']."</b> ".stripslashes(htmlspecialchars($text))."<br></div>";
    file_put_contents("log.html", $text_message, FILE_APPEND | LOCK_EX);
}
?>

  • 1
    Get the error details from log file and update in your question please. Just 500 error is not enough info. – vee May 10 '22 at 02:38
  • @vee how would I check the log file? – SerikTheCzar May 10 '22 at 02:51
  • Use `phpinfo()` and see where is `error_log`. – vee May 10 '22 at 03:02
  • https://stackoverflow.com/questions/2687730/how-can-i-make-php-display-the-error-instead-of-giving-me-500-internal-server-er – Don't Panic May 10 '22 at 07:33
  • @vee I got this error: Fatal error: Uncaught Error: Call to a member function bind_param() on bool in /var/www/html/index.php:54 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 54 – SerikTheCzar May 10 '22 at 15:48
  • @SerikTheCzar Read more about this [here](https://stackoverflow.com/questions/4488035/call-to-a-member-function-bind-param-on-a-non-object), or [here](https://stackoverflow.com/questions/22662488/how-to-deal-with-mysqli-problems-mysqli-fetch-array-argument-1-must-be-of-t) – vee May 10 '22 at 16:48

0 Answers0