-2

I recently installed an Apache2 web server, and PHP5 on my Raspberry Pi, and hooked it to my home router. It all works fine in terms of displaying HTML, even when the index document is a .php file. However, as soon as I try to enter any PHP code, and run it through my browser, it's completely removed from the document.

<html>
    <head>
        <?php
            $baseUser = fopen("GCusernames.txt", "a");
            $newUser = $_POST['user'];
            fwrite($baseUser, $newUser);
            fclose($baseUser);  
        ?>
    </head>
    <body>
        <h1>Writing to userbase</h1>
    </body>
</html>

PHP document inspected in my browser

I've tried changing it back and forth to a HTML file and scouring the internet for clues. But everything I found was seriously unclear and vague.

I'd appreciate any help at all. Thanks.

UPDATE: So after I realized how idiotic I was (thank you everyone who answered,) I now understand that the PHP code shouldn't show up in the browser anyway. Sorry about that.

However, it still doesn't explain why the code doesn't execute at all. It just goes straight to the PHP page, rather than executing it.

Henders
  • 1,195
  • 1
  • 21
  • 27
Sam J
  • 1
  • 1
  • php is server side code, this shouldnt and most likely wont be sent to the browser as its executed once the initial request is made – atoms Jun 23 '16 at 11:45
  • 2
    Thats how PHP works. If you'd see the PHP code in your browser then there would be something wrong with your webserver – tkausl Jun 23 '16 at 11:46
  • The php will be executed on the server it won't just be displayed. What is it you are trying to do? – Anigel Jun 23 '16 at 11:46
  • To send content to a client, please, check on the [echo](http://php.net/manual/en/function.echo.php) documentation – Bonatti Jun 23 '16 at 11:49

2 Answers2

0

PHP is interpreted by the server, it is not send to the user. Thats what PHP is for.

For Example: You want to make a password login. You write the passwordfunction in php because the user cannot see it. More Information on PHP

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • So really, I'm just an idiot. Sorry to waste your time. If you can be bothered, can you take a look at the update? /\/\/\ – Sam J Jun 25 '16 at 03:50
  • How did you tested it? Create a php file with the content: . If you see it is running without echo etc everything is working fine – Jonas Wilms Jun 25 '16 at 08:51
0

PHP is run by the server that your '.php' file lives on, in this case, your Raspberry Pi. When you request a page from your PI your PHP code will be executed before any HTML is returned to your browser.

You will never see your PHP code in your browser's inspector because your browser never even knows it existed.

If you added something like:

<?php echo "This sentence was written by PHP"; ?>

to your page, you would see the text This sentence was written by PHP in your HTML but you wouldn't see the word echo or the <?php ? tags.

There is a great piece in the PHP docs which explain the three main abilities of PHP. They also have a good guide on how to get started with PHP which may be worth checking out if you are not that familiar with it.


Further Reading:

Henders
  • 1,195
  • 1
  • 21
  • 27
  • Okay, so I'm more of a noob than I thought. Sorry to waste your time. If you're up for it, could you take a look at the update? /\/\/\ Thanks. – Sam J Jun 25 '16 at 03:51
  • So you are just trying to open a file, write a string to the end of it and close the file again? How do you know it isn't working? What is in the GCusernames.txt file to prove otherwise? What is happening in the error log and have you got [error logging turned on?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Henders Jun 25 '16 at 10:05