Link to name.php web page downloads the file instead of displaying the page

This simple code is from PHP & MySQL: Novice to Ninja, 6th Edition

<a href="name.php?name=Kevin">Hi, I&rsquo;m Kevin!</a>

That’s the only line of code at phpoop.life/name.html/.
The name.php file contains the following:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="dcterms.created" content="Sat, 10 Mar 2018 02:12:01 GMT">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <title></title>
    
    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>

  <php?
  $name = $_GET['name'];
  echo 'Welcome to our website, ' . $name . '!';
  ?>
  
  </body>
</html>

I tried to create a virtual server, but my Intel CORE 15 doesn’t support it.

Why does it download the php file rather than displaying the page???

Your help is much appreciated. I really want to get on with this book.

<?php

Should be that. If it downloads the PHP file, that’s most likely because you didn’t install it correctly.

well that’s embarrassing! Seems i titled my website apprpriately :slight_smile:.

Made the change, “<?php” and cleared caches in my browsers, but they still want to grab the local name.php file and send it to the download directory rather than open the name.php web page.

What to do?

It sounds like you are trying to open it as a file instead of a web page.

In other words, when you navigate to the file by having the path to it in your browsers address bar, eg. http://localhost/somefile.php the server should give the browser the output of the file not it’s text content.

I can think of two things to check. The more obvious, that the file has the “.php” file extension so PHP will know to parse it. The more involved, that your set-up is correct. i.e. your localhost is running and it knows where to find the PHP engine.

How did you set up your localhost server, XAMPP, or other “packaged environment”, Apache, PHP separately with the path added to your system variables, the path to PHP in Apache’s httpd.conf file inside a Virtual Machine? etc.

1 Like

To expand on the above - running a PHP script requires that you have some kind of server software running in a place you can access it. So if you’re just finding the PHP script inside Windows File Explorer and double-clicking it, the default isn’t usually to run it via your server - on my box it’s to open it to edit. You need to open the browser and open it in the address bar.

2 Likes

Thanks for your suggestions above. All three touched on what I think the problem is.

It came to me as I was brushing my teeth before I went to bed last night, which tells me I should think before I speak (post). i haven’t set up my host server php.

I was wanting to create the website from scratch rather than use a CMS. But as I intend to use what I learn from this book on Wordpress site with a php plugin, I’ll go ahead and install Wordpress and see how that works.

I need to learn more about host servers and their relationship to websites.

Just consider where the code is running. HTML and JavaScript tends to run in the browser, so the browser doesn’t usually need anything else to make it work, whereas PHP runs on the server, so you need to have one somewhere, whether it’s local or not.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.