0

I have been learning some basic PHP via codeacademy.com recently, and am currently attempting to run it on my PC. I am using jEdit to write the code, and xampp/Apache as a server. The actual code looks like:

<html>
    <body>
        <?php
            echo "<p> I know how to run a PHP Program in XAMPP! <\p>";
        ?>
    </body>
</html>

Running this code by typing

http://localhost/[my directory]

into the URL bar of my browser returns Error 403 - access to the directory is forbidden on this server.

I've done some searching around, and I understand that I need an index.html or index.htm file, but I don't know what these are - my knowledge of html is extremely limited.

If someone could offer any help on how I'd bypass the forbidden access message, or if there's another way to run PHP code, I'd be extremely thankful.

Dan Pollard
  • 51
  • 1
  • 7
  • 1
    Unless XAMPP is configured to run `.html` file endings as PHP, you really need that in `index.php`. – Jared Farrish Jun 18 '17 at 00:32
  • Make a search for "htdocs" directory on your computer. There have to open a index.php file. In this file you have to copy your code. After that try to type that address (without directory). But depends how your server was configured – Robert Negreanu Jun 18 '17 at 00:34
  • I would also investigate running a [Php7 + Apache Docker container](https://stackoverflow.com/questions/41423349/docker-how-to-set-up-apache-php-in-docker-compose-yml) with a separate docker container for MySQL. – Jared Farrish Jun 18 '17 at 00:37
  • @JaredFarrish Ok, how would I create an index.php and what would I do with it? Sorry, as I said I'm very new at this – Dan Pollard Jun 18 '17 at 00:37
  • Right click in the `htdocs` folder, New > Text Document, rename it `index.php` and copy/paste/save. – Jared Farrish Jun 18 '17 at 00:39
  • As a bridge, it's also easier to run a [Vagrant box pre-built with your requirements](https://github.com/spiritix/vagrant-php7). – Jared Farrish Jun 18 '17 at 00:41
  • This is a decent Vagrant as well with a good writeup about getting it running: https://scotch.io/bar-talk/announcing-scotch-box-30-and-scotch-box-pro – Jared Farrish Jun 18 '17 at 00:45

1 Answers1

1

Name the file with your code in it: "index.php". Make sure this file is in your "xampp/htdocs/" directory.

Additionally you might want to put this file within its own folder in that directory for the sake of keeping all of your files specific to one project together.

You can then access index.php with "http://localhost:port#/folder_name/"

KyleS
  • 401
  • 3
  • 16
  • Ok, I'll try. If I've understood correctly though, does this mean I can only have one PHP file at any time, because it has to be called index.php? – Dan Pollard Jun 18 '17 at 00:43
  • 1
    Nope, you may have as many php files as you need. The purpose of 'index.php' is so the server knows what page to serve on default. For example, when you visit a website, the first page that loads typically starts with "index" or "default" followed by whatever file extension. This is used so the server knows what page to show when the user first enters the website. – KyleS Jun 18 '17 at 00:45
  • ... although it is common to have something called a [Front Controller](https://stackoverflow.com/questions/6890200/what-is-a-front-controller-and-how-is-it-implemented-in-php), where everything passes through it. Keep that in your back pocket. For now, just remember `.php` in a web directory will let that file be run as Php. @kyle – Jared Farrish Jun 18 '17 at 00:49