r/PHPhelp • • Jan 21 '23

Does PHP only work inside C:\xampp\htdocs with a localhost?

If I try to put a php file inside any other location other than whats listed in the title, it just shows the code and doesnt execute it. Is there a way around this?

8 Upvotes

21 comments sorted by

9

u/greg8872 Jan 21 '23 edited Jan 21 '23

Php files techincally can work anywhere on your system if you manually call them (example assuming default install)

c:\xampp\php\php.exe  c:\path\to\your\phpfile.php

However, to work in a browser, yes, it can only work inside certain directories, defined in your apache.conffile. You can define other locations in there, but by default, it just sets up the one site location.

EDIT to expand a little more:

When you open a location in your browser, it takes what it finds and renders it. If you call to http://localhost/ that is making a call to Apache running on your computer, which then will via the config file, look for a given file (or assumed defaults if a specific file isn't given), and then processes it and returns the output to the browser. In the case of a plain .html file (and other basic asset files) it will just pass them through. However for specially defined file types (like php ones), it will first pass the file to be processed by PHP, and then the output of PHP gets passed to the browser. This is why you can't interact with a PHP file during execution, it is always fully processing then you get output (well technically, it can send part of the output while processing, but generally it happens so fast, you don't notice)

However if in your browser you directly call the file, ie browse to file:\\\xampp\htdocs\sample.php your browser is directly loading the file from the file system and rendering the "output" ends up rendering the raw php code, since nothing processed it.

1

u/Reaver75x Jan 21 '23

Thank you that was explained really well.

2

u/chmod777 Jan 21 '23

What, exactly, are you trying to do?

You can open a terminal at the directory, and use 'php -S localhost:4000' and it will run the build in webserver. Good for quick test, but its not very performant.

Otherwise you need to set up and use a webserver that will handle requests and forward to the php engine.

1

u/Reaver75x Jan 21 '23

I’m trying to make a local server on hard drive C , and have made a local website on hard drive F that I would like to connect to the server on C.

I was able to do that but now I am really just looking for a way after I click the submit button from the php form to return me back to my main index.html on the F drive.

As of right now I don’t know the correct path to use.

My code is:

If (mysqli_query($conn, $sql) { ?>

<script type=“text/JavaScript”> window.location = “F:/RU/folder/index.html” </script>

<?php }

1

u/CyberKaliyugiNepali Jan 21 '23

You can change the location to store PHP files by modifying values for DocumentRoot and Directory in Apache Server's httpd.conf file.

1

u/Reaver75x Jan 21 '23

I have tried doing that. I’m assuming you mean the htdoc location inside the httpd.conf file. Unless I have to restart xampp it didn’t work.

1

u/greg8872 Jan 22 '23

yes you do. Anytime you change the config file, you need to restart for it to reload it.

1

u/Reaver75x Jan 22 '23

Ahh okay thanks for letting me know. I should have tried that before I impulsively refactored the files already. But it’s good to know.

1

u/him_x Jan 21 '23

If you use Windows as OS I strongly recommend you to use IIS instead of apache. If you're interested in using it let me know in a comment and I'll leave you guide to use it.

1

u/Reaver75x Jan 22 '23

I’ve messed around with it in the past but I really didn’t know how to use it at the time lol. Why do you feel it’s better? And yes I’m interested

1

u/him_x Jan 25 '23

Look here's a guide on how to use IIS instead of apache. You see, in windows is easier to configure virtual hosts with IIS, that way you can run php apps on your computer in any folder.

https://stackify.com/how-to-host-php-on-windows-with-iis/

1

u/Reaver75x Jan 26 '23

thank you man, I will try that out

2

u/wh33t Jan 21 '23

Its not possible to "execute" a php file outside of the web directory, directly in the browser. You can have a php script include another php scripts code and that script can be anywhere you have read access to.

The reason you are seeing the php code is (Im guessing) because you are not loading the script through the webserver (http://localhost) and instead are just loading a txt file inti the browser?

2

u/kAlvaro Jan 21 '23

If you use Xampp (or any other third-party PHP+Apache bundle) that stuff comes preconfigured for you, but you can always change it or adapt it to your needs. The key Apache settings are:

  • <VirtualHost> block to define a web site

  • DocumentRoot directive to define the web site public directory

And then you can place SetHandler application/x-httpd-php (assuming you run PHP as Apache module, which I believe Xampp does) inside a different number of blocks to fine-tune what files to process as PHP. For example, I want all files with *.php extension anywhere, so I put a <FilesMatch> block as top level rule:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

I could also put it inside <Directory> or <Location> blocks to restrict PHP files to certain locations.

2

u/ImmensePrune Jan 21 '23

You need you keep your PHP script files stored within htdocs for execution. If they are located outside htdocs you won’t be about to view via the browser but can still run on the CLI.

Also, I recommend using Docker over XAMPP!

3

u/greg8872 Jan 21 '23

Just curious, do you have a good tutorial site for Docker? I gave it a try years ago, but never fully got into it and would like to try again. For reference I run windows machines.

Normally for dev work, I have a dev VPS setup to match the setting on the live servers for my company and the ones my clients have. But I would like to learn docker, but for some reason before, couldn't get into the docs at the time.

2

u/ImmensePrune Jan 21 '23

I don’t have any specific tutorials for a Docker config, but I did do my configuration by myself and have google doc notes on how to configure if you want that.

I also used an article I found on DEV, let me pull that up again and I’ll share it with you!

2

u/greg8872 Jan 21 '23

Thank you.

1

u/ImmensePrune Jan 21 '23

No problem! We also use a VPS for prod and test servers at work but I liked to set up my local host for everything.

During the configuration, it helped me understand how everything works a lot better than reading an article or watching a video.

Give me a little and I’ll side chat you with the documents!

1

u/am0x Jan 21 '23

That’s where your server is pointing to hosted files as sites. This is specific to xampp.