3

I'm trying to build my first website. I have a domain name and am building it on my laptop before uploading it to my host. The website is mostly focused around searching a database of family tree information.

I have created a basic HTML file which seems to be working ok. I have a search button linked to a PHP file. Ideally when someone used the button without putting in parameters it should reply with an error message that says "search term is missing" but instead it shows all the code for the PHP file. I've never done anything like this before so am more than a little confused.

Ed Fordham
  • 31
  • 1
  • 2

3 Answers3

2

Welcome to the web development world !;)

the php file must have a .php extensions, and should begin with <?php and end with ?>

you probably forgot one of those :)

Mostafa Berg
  • 3,211
  • 22
  • 36
  • 1
    I tend to drop the `?>` at EOF. Still don't know whether that's okay. – just lerning Nov 02 '13 at 11:25
  • 4
    @justlerning The closing `?>` is optional. – ComFreek Nov 02 '13 at 11:27
  • 1
    @justlerning - When you open a door you should close it. Stops unwanted things turning up. Anyway it is only two key presses – Ed Heal Nov 02 '13 at 11:27
  • Well, as @EdHeal says, better close it, but it's definitely optional, but you must know what you're doing , cause sometimes you'll cause issues. – Mostafa Berg Nov 02 '13 at 11:32
  • 3
    @EdHeal: Totally incorrect. Omitting the closing tag is actually considered [good practice](http://stackoverflow.com/questions/4410704/) in most cases. – Amal Murali Nov 02 '13 at 11:32
  • @AmalMurali - I disagree. My opinion is that it tells to the reader of the code that you have done with the interpreter. – Ed Heal Nov 02 '13 at 11:39
  • @justlerning you're right to leave it open as it prevents code injection like the r47 virus being successful. If it happens, instead of your visitors getting crap installed, your page just fails. – Popnoodles Nov 02 '13 at 11:48
  • @popnoodles - If your machine is getting viruses then you have other problems than just delivering dodgy pages. – Ed Heal Nov 02 '13 at 12:54
  • @EdHeal it's not. I once had shared hosting. – Popnoodles Nov 02 '13 at 14:49
2

Your browser will not understand php by itself, on a given web server there's a number of layers for compiling PHP etc. Try XAMPP, you'll have it up and running in no time and you can see your PHP at work by simply looking up localhost with your browser.

aumbadgah
  • 70
  • 6
1

If you would like to test your PHP code running on your own machine you must set up some things first. You need you own web server and PHP interpreter installed. You can do this easily by installing a package like XAMPP.

This comes with a graphical install package and includes everything you need to host a PHP file.

XAMPP's website

After you have installed this you are going to have a special directory, "htdocs". By default: c:\xampp\htdocs

If you add some files here it is going to be published on your webserver.

For example if you create an index.php it is going to be located on:

http://localhost

more precisely:

http://localhost/index.php

In PHP files you must include special character sequences telling the interpreter that there is PHP code.

<?PHP
// your PHP code goes here
?>

First you should test your server by having a very simple file. eq.:

<?PHP
  echo "Test";
?>

IF this works fine you can go for some further tutorials to dig deeper in PHP. (It should only output the Test string - this is the result of running of the PHP code block)

PHP tutorial

Have fun!

Attila
  • 3,206
  • 2
  • 31
  • 44