I just install PHP on a Windows 11 box. I set the environment system path to C:\php, which is where I installed PHP. When I type “php --version” from the command prompt, it shows the version. But when I run a script in a browser, it is still just show the script instead of executing it. I’ve run it from a Linux box and it works fine.
Php is primarily used as a web server scripting language. Yes, you can run it from the command line, but that has nothing to do with with how browsers work. Browsers make http(s) requests to web servers and the server returns a response to the browser. If the request is to a URL that corresponds to a php script, the web server will invoke the php language engine to process the request.
So, primarily, php must be integrated with a web server and you must invoke it via a URL that corresponds to that web server.
I am running it from a browser. It is being called from index.html and it works fine when I run it from a Linux server. I have also Apache24 installed. I used the command prompt merely to test that php runs.
By default, only files ending with a .php extension cause the php language engine to be invoked on the web server. It requires a specific change to the server configuration settings to cause a request to a file with a .html extension to invoke php.
The file linked in the index.html IS a .php file. I added this code to the Apache24 httpd.conf file
PHP8 module
PHPIniDir “C:/php”
LoadModule php_module “C:/php/php8apache2_4.dll”
AddType application/x-httpd-php .php
But now the browser says “This site can’t be reached” when I click on the link.
Define ‘linked’?
A URL on a localhost development system would look like - http://localhost/yourfile.php. A file system path on windows would look like - file:///C:/Users/your_name/some_path/some_file.html
If you are opening the index.html file directly, through the file system, any href link in it will also be opened directly though the file system. You need to request the index.html file using a URL like - http://localhost/index.html or better yet, since you are in the php world now, make all your files .php, regardless of if they have php code in them or not.
I’m opening the index.html file in the browser by typing “localhost” in the address bar. On another box I’m opening it with mysite.com in the address bar. This run the index.html file. As I said, this works fine when I use apach2 and PHP8 with a Linux box as the server. I just can’t get it to work with Windows as the server.
This is a line in the html file.
<a href="example.php">List Examples</a><br>
That’s what I mean by linked.
So all your PHP will be in the example.php
file as that is just an anchor element <a>
.
Yes, and it works fine using Linux Mint as the server, so it isn’t a problem with the file. I tried linking to other php files and NONE of them work. I’ve also tried with a “Submit” button and passing varible. Obviously there is something wrong with my Apache or PHP setups. I just don’t know what.
The reason your PHP script is not executing when you run it from a browser on your Windows 11 box is because you need to configure your web server to process PHP files. This typically involves setting up a web server such as Apache or IIS and configuring it to handle PHP requests.