Showing posts with label First PHP script. Show all posts
Showing posts with label First PHP script. Show all posts

Saturday, 19 May 2012

First local LAMP site

It's all coming together...

In my post about installing PHP, I created a small script to see if it was a birthday or not. I'm now going to turn this into an actual local site using all the constituent parts of LAMP and some CSS. Using MySQL may be a little overkill, but I think it would be good to practice first on a nice simple site.

I already have the site set up in my hosts folder located at /etc/hosts.
My virtual hosts file is also already set up but seems a bit bloated. At the moment I will leave it as it is. I think a future post about virtual hosts may be a good idea.

Create a database in mysql
CREATE DATABASE isitmikesbirthday;
USE isitmikesbirthday;
I now need to set up a user for my script to select from the database as and set up any database tables I want.
GRANT SELECT ON isitmikesbirthday.* TO 'iimb'@'localhost' IDENTIFIED BY 'password';
Then create a table with for my information
CREATE TABLE persons(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), first_name VARCHAR(10), last_name VARCHAR(10), date_of_birth DATE);
I then need to put some data into the table
INSERT INTO persons (first_name,last_name,date_of_birth) VALUES ('Michael','Gwynne','xxxx-09-27');
That should be the MySQL part over. Now to the PHP script. It should be pretty self explanatory. This script should also be foolproof and have all the error checking needed in it.

Hmm, it seems I can't paste PHP code into Blogger. I'll have to find a work around and come back to this. Unless anyone knows how to.... please feel free to comment below.
In the mean time, you can download my code in order to have a look at it.
isitmikesbirthday?
Simple site


There are several things I realise are slightly bad practice in my code.
  1. Some of these things like the class and CSS should be spilt into separate files.
  2. The member variables of Date should not be public and should be instead called by helper functions to avoid accidental assignment.
With my first two goals in mind:
  1. Google Friendliness (Speed, W3C validation etc..)
  2. Neat, Readable, Self-documenting code
Please have a look at the code and see if you can find any ways to improve performance, readability and security. Comment below, add reasoning if neccessary.

Friday, 18 May 2012

PHP

Installing PHP5

This useful article again tells you pretty much all you need to know.
2 (or 4) simple things to do and PHP is set up on your machine.

That wouldn't make much of a blog post. So I'm going to make a simple PHP site and set up my folders and settings before I move on to installing MySQL.

Settings and Folder structure

I decided that seeing as I would be creating projects, I would have a "Projects" folder in my home folder. Just use the command line or the GUI to do this.
If you were following the article mentioned above, you need to deactivate the site "mytest" by entering the following on the command line.
sudo a2dissite mytest
Now copy one of the virtual host files I named the copy "isitmikesbirthday".
I opened up this file and changed the DocumentRoot to /home/michael/Projects/isitmikesbirthday and changed the Directory to /home/michael/Projects/isitmikesbirthday/
After that I put an entry in the hosts file. In /etc/hosts I added the following line
127.0.0.1 isitmikesbirthday.local
Then I needed to enable the site with
sudo a2ensite isitmikesbirthday
And restart apache
sudo service apache2 reload

The first PHP script

All that's left is to create and index.php file in the folder Projects/isitmikesbirthday/
And it's in that file that the magic happens. I put...

<?php
if(27 == gmdate("j") && 9 == gmdate("n"))
     echo "Yes";
else
     echo "No";
?>