# Part 5.5Dynamic sites in PHP

Most websites adapt to the user. For instance, with a website where users can logon, for instance Amazon.com, at the top right maybe located "Hello user" where user is the name of the person who has logged on to the website. One way of doing this is via PHP.

Most online shops use a server side language such as PHP as a means of making their pages more . A keypoint about dynamic pages is that they can vary in ways that make life as a website builder much easier.

Sites should try to make sure that it looks like a normal website to the user, particularly the URL. It is not good for users remembering big long URLs and ones with file extensions. For instance, all the pages on this site are written in PHP but there is no need for a .php at the end of the URL. Users are less aware of the PHP file extension. To do this, placing index.php in the directories changes the experience for users. This way a link to the page /about/index.php looks like /about and both go to the same page. This is a more user-friendly approach to some thing that makes more sense to the end user.

This piece of information from About.com explains this well:

If you don't put in an index.html file in a directory, most Web servers will display a file listing of all the files in that directory. While in some situations, you might want that, most of the time this is ugly at best and a security hole at worst. Writing a default Web page and naming it index.html helps solve those problems.

This is true in almost all web servers. For security reasons, a web server should not make an "/indexof" page to appear in front of a user so that they can see what files are in that directory of the site.

## Getting the choices from a cookie

Building a dynamic site that can be customised for instance is easy. In the following example, the PHP is going to customise the background colour of the site.

PHP
<?php
//Change background
$background =$_COOKIE("backcolor");
//For this example the site is going to offer two stylesheets
if ($background == "red") { echo '<link rel="stylesheet" href="red.css">'; } ?>  The code is simply taking the value from a cookie and determining what background colour (or stylesheet) the user chooses. This would be a more appropriate way of doing this than to change <body bgcolor> as it is recommended by W3C that CSS is used rather than altering styles on the page directly. ## Getting file contents Dynamic websites change. The menu can change, the footer can change and so on. This can be easily achieved by including an easily updated text file on the server. The function used here is file_get_contents which will take the file name as a parameter (in the form of a string). In the following example, a text file named text.txt and a PHP file named index.php which will have the PHP script shown after the contents of the file will be used. Text <!--This comment will be put into the HTML--> <a href="http://www.jamiebalfour.scot/">My website</a><br> <a href="http://www.youtube.com/user/jamiebalfour04">My YouTube</a><br>  And the PHP: PHP <?php //Simple PHP to echo the HTML from the file named text.txt echo "<p>This is a test</p>";$content = file_get_contents("text.txt");
echo \$content;

?>


Output for this code sample will look somewhat like this below:

This is a test

My website