0

I am experiencing trouble while installing Apache2 on my Debian 8 VPS (jessie). I installed the website correctly on Hostinger, it works perfectly, but knowing Hostinger was a free plan, I moved to a sufficient VPS so I could get my hands dirty and do the job my myself. I now can handle everything, but the truth is, it's been a long time since I didn't use a debian server, and installing Apache seems harder than I thought.

So, I secured my VPS as I wanted, that's not the problem, the site works correctly, but partially.

I mean, on somes pages, the PHP code executes very well, it works with my database without any problem. I have a utils.php file that contains a getBDD() function like this one :

return new PDO("mysql:host=localhost;dbname=name;charset=utf8", "user", "password");

And it works on my main pages, I can request everything. You can see it here : http://mdjfeelzor.ovh/petiteancienne.php (the website is french).

But I also have an ajax chat that needs a bit of PHP to work properly. Aaaand I don't understand why, but when I initialize my $conn with getBDD(), I am experiencing an issue further in the code.

Look at that code :

include '../utils.php';

            session_start();

            if(isset($_POST['enter'])){
                if($_POST['name'] != ""){
                    $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));

                    try {
                        $conn = getBDD();

                        $answer = $conn->prepare('SELECT * FROM openchats WHERE ip=? AND name=?');
                        $answer->execute(array(stripslashes(htmlspecialchars($_SERVER['REMOTE_ADDR'])),
                        stripslashes(htmlspecialchars($_SESSION['name']))));

                        if (!($data = $answer->fetch())) {
                            $req = $conn->prepare('INSERT INTO openchats (ip, name) VALUES (:ip, :name)');
                            $req->execute(array(
                                'ip' => stripslashes(htmlspecialchars($_SERVER['REMOTE_ADDR'])),
                                'name' => stripslashes(htmlspecialchars($_SESSION['name']))));
                        }
                    }

                    catch(PDOException $e) {
                        echo "Connection failed: " . $e->getMessage();
                    }
                }
            }

It works correctly, right ? (it's supposed to work correctly on your machine) But the thing is, the code starts showing on my web page on the first call of prepare() as you can see here : http://mdjfeelzor.ovh/chat

So, I was wondering, what do you think is the problem ? Is it my way of programming that isn't working ? By the way, I'm using PHP5. I also tried the code <?php phpinfo(); ?> it works perfectly.

Thanks for your help !

EDIT : By the way, the code is shown but it is not supposed to enter in the condition, as $_POST['enter'] doesn't exist on the first load, the code really works on my computer and on Hostinger so I think that the problem comes from Apache configuration ^^' and the AJAX code is not running at that point.

LeTiroir
  • 13
  • 2
  • Enable error logging and check the error logs (apache and PHP, depending on setup this can go in each of those error logs). Can it be that `include '../utils.php';` triggers a warning that the file could not be found? If you `require` instead of `include` this would be a hard error, which helps to fail early so to show such problems more prominently (albeit with an AJAX request things are generally more hidden and harder to debug - check your browsers developer tools, especially the networking tab). – hakre Jul 01 '17 at 14:13
  • And if PHP is not executed, it's perhaps you requested a location from the webserver, the webser does not know what to do with it, so it will return plain text. – hakre Jul 01 '17 at 14:15
  • The thing I don't understand is that it works perfectly on my computer and on Hostinger, but not on my VPS... and I tried removing the include and instead of calling `getBDD()` just copying and pasting the content of the function, it does not change anything.. – LeTiroir Jul 01 '17 at 14:39
  • By the way, here are the contents of error.log : https://pastebin.com/k9z5jS0w – LeTiroir Jul 01 '17 at 14:41
  • The file it looks like it is perhaps in a different directory in which the apache webserver on that VPS is not configured to execute it. – hakre Jul 01 '17 at 14:43
  • Okay, I understand what's the problem. The code started with `` instead of ` – LeTiroir Jul 01 '17 at 15:12
  • That is a PHP configuration setting. Check your code and use ` – hakre Jul 01 '17 at 15:14
  • Possible duplicate: https://stackoverflow.com/q/5121495/367456 (edit: wrong link) – hakre Jul 01 '17 at 15:15

0 Answers0