1

My website would display a picture, that gets sent in every second (refresh webpage) Yet, I realised that I had not installed PHP on my apache (which runs on a raspberry pi), so I did that and changed the directory from /var/www/html/index.php to /var/www/index.php and adjusted the paths etc.

Now if I want to display the site, I get a 500 error. In the log file stands this:

PHP Parse error: syntax error, unexpected 'else' (T_ELSE), expecting end of file in /var/www/html/index.php on line 26

And my index.php(the only file in the project) like this:

<html>
<head>
    <title>Swissskills</title>
    <link rel="stylesheet" href="css/stylesheet.css"/>
    <link rel="icon" href="pictures/post.jpg">
    <meta http-equiv="refresh" content="2.1">
</head>
<body>
    <div id="links">
        <h1 id="title">Webcam</h1>
        <?php
            /*
            $timestamp --> findet letzte Änderung der Datei heraus (nicht in lesbarer Uhrzeit formatiert --> alles in sec)
            $time --> liest aktuelle Zeit
            $diff --> differenz in sec
            */
            $dateinamen = 'pictures/Live.jpg';
            $timestamp = filemtime($dateinamen);
            $time  = time();
            $diff = $time -$timestamp;

            // Wenn Bild jünger als 2sec
            if ($diff < 2)
                echo "<img src='pictures/Live.jpg'";
            sleep(2);
            else
                echo "<img src='pictures/oops.jpg'";
        ?> 
    </div>
    <div id="rechts">
        <h2>Ping</h2>
        <?php
            /*exec (wie in Kommandozeile)
            ping (normal) -n (Anzahl Pakete) -w (Wartezeit für gesendete Pakete)
            $host (Hostname/IP-Adresse)
            $output (Detailinfos wie in Kommandozeile)
            $result (0 = vorhanden; 1 = nicht vorhanden)
            */

            $host1="192.168.1.1";
            exec("ping -n 1 -w 1 " . $host1, $output1, $result1);
            if ($result1 == 0)
                echo "<h3 id='sw1'style='background-color: green';>Router</h3></br>";
            else
                echo "<h3 id='sw1'style='background-color: red';>Router</h3></br>";


            $host2="192.168.1.10";
            exec("ping -n 1 -w 1 " . $host2, $output2, $result2);
            if ($result2 == 0)
                echo "<h3 id='sw2'style='background-color: green';>Switch 1</h3></br>";
            else
                echo "<h3 id='sw2'style='background-color: red';>Switch 1</h3></br>";


            $host3="192.168.1.11";
            exec("ping -n 1 -w 1 " . $host3, $output3, $result3);
            if ($result3 == 0)
                echo "<h3 id='sw3'style='background-color: green';>Switch 2</h3></br>";
            else
                echo "<h3 id='sw3'style='background-color: red';>Switch 2</h3></br>";
        ?>
    </div>
</body>

Any help is appreciated.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
Niklas
  • 13
  • 7
  • You have a basic syntax error. I don't understand how you're even capable of finding the error in the log, but not able to correct the code? Obviously the `else` on line 26 is just flapping in the breeze, it needs an `if`. See: http://php.net/manual/en/control-structures.elseif.php – KIKO Software Aug 07 '18 at 08:52
  • there is an if right above it? Or cant I use "else"? – Niklas Aug 07 '18 at 08:56
  • 1
    You can find a solution [here](https://stackoverflow.com/a/41251720/8913537) – Philipp Maurer Aug 07 '18 at 08:58

2 Answers2

0

You have a syntax error in the file around

if ($diff < 2)
    echo "<img src='pictures/Live.jpg'";
sleep(2);
else
    echo "<img src='pictures/oops.jpg'";

Try using braces around the code inside the if statement

if ($diff < 2) {
    echo "<img src='pictures/Live.jpg'";
    sleep(2);
} else {
    echo "<img src='pictures/oops.jpg'";
}
Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
memo
  • 1,868
  • 11
  • 19
  • I've just adjusted the code as it is here, it still says error on line 26 `if ($diff < 2){ echo " – Niklas Aug 07 '18 at 09:16
  • No, it doesn't anymore: https://tio.run/##lY@xDoJADIZn7ykaQgIMSsIKSuLs7kygQMnd0XDoYngzNx8MQQYBWezQof/X/2@55L6PYi5ZgJ0lLZJOFGo4gsOUtrcGjX@hOx4qLpxQwKzslhSaNlE80DlJVOPAnbl4GwswwB9uLWaU54M2QfuveSgWnO/DFbWGM8kMqtdTF9hAIg0EBtMFSTm4k2sEgQePhTgWpmUNVkSqANOkx42HrfBnyUhEdoPV9WI39g5QGvwnqa7ZbCV1Ij71/Rs – memo Aug 07 '18 at 09:19
  • It does... I don't know why but it does. – Niklas Aug 07 '18 at 09:24
  • Could it be a problem that I'm editing the file on my Windows PC with notepad and then transferring it via USB to the pi? – Niklas Aug 07 '18 at 09:25
  • Possibly. Maybe try checking the file path is correct, or restart your php server – memo Aug 07 '18 at 09:27
  • Well, well, well... I still had the html folder with an old version of an index.php in it and it was referring to this one – Niklas Aug 07 '18 at 10:01
0

Your if-statement is missing open/end curly brackets

Either use curly brackets, or semi-colons (http://php.net/manual/en/control-structures.alternative-syntax.php)

<html>
<head>
    <title>Swissskills</title>
    <link rel="stylesheet" href="css/stylesheet.css"/>
    <link rel="icon" href="pictures/post.jpg">
    <meta http-equiv="refresh" content="2.1">
</head>
<body>
<div id="links">
    <h1 id="title">Webcam</h1>
    <?php
            /*
            $timestamp --> findet letzte Änderung der Datei heraus (nicht in lesbarer Uhrzeit formatiert --> alles in sec)
            $time --> liest aktuelle Zeit
            $diff --> differenz in sec
            */
            $dateinamen = 'pictures/Live.jpg';
            $timestamp = filemtime($dateinamen);
            $time  = time();
            $diff = $time -$timestamp;

            // Wenn Bild jünger als 2sec
            if ($diff < 2) {
                echo "<img src='pictures/Live.jpg'";
                sleep(2);
            }
            else {
                echo "<img src='pictures/oops.jpg'";
            }
        ?>
</div>
<div id="rechts">
    <h2>Ping</h2>
    <?php
    /*exec (wie in Kommandozeile)
    ping (normal) -n (Anzahl Pakete) -w (Wartezeit für gesendete Pakete)
    $host (Hostname/IP-Adresse)
    $output (Detailinfos wie in Kommandozeile)
    $result (0 = vorhanden; 1 = nicht vorhanden)
    */

    $host1="192.168.1.1";
    exec("ping -n 1 -w 1 " . $host1, $output1, $result1);
    if ($result1 == 0)
        echo "<h3 id='sw1'style='background-color: green';>Router</h3></br>";
    else
        echo "<h3 id='sw1'style='background-color: red';>Router</h3></br>";


    $host2="192.168.1.10";
    exec("ping -n 1 -w 1 " . $host2, $output2, $result2);
    if ($result2 == 0)
        echo "<h3 id='sw2'style='background-color: green';>Switch 1</h3></br>";
    else
        echo "<h3 id='sw2'style='background-color: red';>Switch 1</h3></br>";


    $host3="192.168.1.11";
    exec("ping -n 1 -w 1 " . $host3, $output3, $result3);
    if ($result3 == 0)
        echo "<h3 id='sw3'style='background-color: green';>Switch 2</h3></br>";
    else
        echo "<h3 id='sw3'style='background-color: red';>Switch 2</h3></br>";
    ?>
</div>
</body>
Ali Alwash
  • 533
  • 1
  • 6
  • 23