0

I have a .php file which renders a webpage and works as intended. It contains the following chunk:

<?php
    }
    else
    {
        openConnection();

        $userid = $_SESSION["userid"];
        $loggedinphrase = getLoggedInPhrase($connection, $userid);

        print "<p>\nYou are currently <span class=\"important\">" . $loggedinphrase . "</span>.\n</p>";

        mysql_close($connection);
    }
?>

I am copying the entire contents of this PHP file over to a different HTML file. In particular, I'm sticking it right after <h2>Overview</h2> in this templatefile.

But I get this on the webpage:

\nYou are currently " . $loggedinphrase . ".\n

"; mysql_close($connection); } ?>

Why could this be happening?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jaynp
  • 3,275
  • 4
  • 30
  • 43
  • the page is not paresingthe php for some reason –  May 24 '13 at 01:41
  • Does php have execution privilage? – cerkiewny May 24 '13 at 01:45
  • Okay so I just added a `` and it didn't actually load. I know my server can handle php because .php files work fine. Why isn't PHP working within an HTML file for me? – jaynp May 24 '13 at 01:50
  • use .php extension or configure the server to pass .html as php –  May 24 '13 at 01:53
  • PHP and HTML are two different technologies. You can't stick raw PHP code into an HTML file and expect it to work. You can fudge it by telling your server to treat .html requests as .php requests, but you'll really be using PHP under the hood. – Major Productions May 24 '13 at 02:01
  • "View Source" and I think you'll see the problem. – tadman May 24 '13 at 02:07
  • @KevinM1, you will be using php 'under the hood', with that (common) technique file extensions are meaningless. –  May 24 '13 at 02:07
  • Sorry, wrong duplicate target. Candidates (but both posted later): *[PHP gets commented out in HTML](https://stackoverflow.com/questions/21279901)* (2014, 7 answers) and *[How can I run a PHP script inside a HTML file?](https://stackoverflow.com/questions/22853669)* (2014, 13 answers) – Peter Mortensen Nov 30 '19 at 22:55

4 Answers4

8

You can't use PHP in an HTML file unless you have your server configured so, which you obviously don't since you're getting this output. Make sure your file is a PHP file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • I think you may be onto something that my server isn't configured right. But I am trying to get PHP working in an HTML file NOT a PHP file. – jaynp May 24 '13 at 01:53
  • Oh, then perhaps [this](https://www.rssinclude.com/blog/36_how_to_run_php_scripts_in_html_or_htm_files) might be of assistance – php_nub_qq May 24 '13 at 01:59
  • Is your server configured to run `.html` files through PHP? Probably not. Should you do this? Probably not. Why not rename the page to have a `.php` extension? – tadman May 24 '13 at 02:07
  • @tadman If I'm trying to use HTML5 and such, is it not necessary for my files to have a .html extension? I can use all of the HTML5 goodies from within a PHP file? – jaynp May 24 '13 at 02:11
  • 2
    The file extension is irrelevant if your server is configured correctly. The browser ignores extensions and instead pays attention to the `Content-Type` header supplied by the server. If this is `text/html`, then it will be interpreted as HTML. The `DOCTYPE` header in the HTML itself determines which version of HTML the browser uses. – tadman May 24 '13 at 03:31
1

You are probably just trying to view the file in your browser, but if you just open a .php file in your browser, it treats it as all HTML.

PHP is server-side code, so you need a server. I use XAMPP, so that I can just put my project into the "htdocs" subdirectory in the xampp directory and then go to my browser, type "localhost" into the URL bar and navigate to the correct project. Then it should work.

If you have a server with an address, you have to move the project onto the server and then go to that address with your browser.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AndrewB
  • 764
  • 1
  • 6
  • 25
0

If the extension is a .php then it will be able to read the PHP content. You have the file extension as .html, and thus, the browser parses it as HTML instead of the server parsing it as PHP for the browser. A simple way to fix this is to get PHP installed on your server and use a .php extension for the file you have issues with. Another workaround without switching the extension would be to put the PHP code in a PHP file somewhere that can parse it, and use an iframe element, but this way would be very tricksy, when you can easily avoid having to do it.

user1640021
  • 100
  • 1
  • 6
0

You must use a .php extension file. A php file definitely can contain html content along with all style & script but the vise versa is not possible because html is the client side code and php is a server side code and all the servers can run html content...

Moreover in your code I can see an extra closing brace '}' at the beggining. Try removing those and changing the file extension.

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57