PHP显示为文本而不是应用程序

So I am new to coding PHP and am working on creating a login system. I have followed this tutorial and have done everything that he has said, but when I open any of the files it just shows the code. http://www.youtube.com/watch?v=c_hNNAdyfQk

I really need help figuring this out and would love any feedback that I can get.

Here is the code that I used for index.php

    <?php
require_once 'core/init.php';

if(Session:exists('home')) {
    echo '<p>'. Session::flash('home') . '<p>';
}

$user = new User();
if($user->isLoggedIn()) {
?>
    <p> Hello <a href="#"><?php echo escape(user->data()->username);</a>!</p>

    <ul>
        <li><a href="logout.php"> Log out</a></li>
    </ul>

<?php
} else {
    echo '<p> You need to <a href="login.php">log in</a> or <a href="register.php">register</a></p>';
}

Based on your comments, it seems that PHP is running correctly and the error is in the main file or the included file.

Try the following:

  1. Make sure your included file core/init.php has <?php opening and ?> closing tags.

  2. When using include or require, the included file must not contain white spaces or line breaks before the opening tag. Make sure that init.php does not contain any white spaces or line breaks before the opening <?php tag.

  3. You forgot to close the php tag on line 9 of the sampled code:

    change: <p> Hello <a href="#"><?php echo escape(user->data()->username);</a>!</p> to:<p> Hello <a href="#"><?php echo escape(user->data()->username);?></a>!</p>

Hope this helps!

To me it seems you just double clicked the php file and opened it in browser so the address bar looks something like: file://xyz/path/to_your/file.php If that's the case, put http://localhost/path/to_your/file.php