Sunday, September 19, 2010

Webdesign, chapter twelve: CMS part 4

This post is titled making things (slightly) more comfortable.
In the last post I showed you how you can get one page out of the database that I've created for this "CMS". I'm using air-quotes here because it's so far away from any freely available CMS that it shouldn't even be called that.... but we're getting there (slowly).
To get a specific page out of our database we need to specify what we actually want to get. In SQL this is done with a WHERE clause in our query. We're going to change our SELECT query to the following, to get the page specified by the URL field in our addcontent page:
$query = "SELECT * FROM xyious_content WHERE URL = '" . $PHP_SELF . "' ORDER BY ID DESC";
The ORDER BY in the end there just tells the DB to sort descending (DESC) by the ID field, so that we could just add a new page with the same URL into our database and that query would get the newer one (first, but we only get 1 row from our result set which would then be the newest entry).
That where clause is the only change we actually need to make to our display content page to make it universal, which means you can just copy and rename the file to get any page out of the database you want without changing a single character inside the file. Now that's way more comfortable than writing a whole page for every web page that you want to show people. However, it's not quite as comfortable as it should be, since you still need to put data into the database, copy and rename the display content php file, and make sure that the URL you entered when putting data into the database matches exactly the filename for the display content file. Luckily php can just do all that for you.
So we change our add content page to just write the file for us, so that the filename matches what we entered, the file is created at the same time as the content arrives at the database, so that it can be served immediately, so that there now is only a single step you need to go through when making a whole new page on your site, entering the content into the form we made. Now in the add content page we need to first check if the file we're trying to write already exists, since we don't really want to just overwrite potentially important files (or have someone else do that, considering so far the file is just out there, unprotected, and everyone could just enter all kinds of stuff into our database, so that everyone could just overwrite every single file on our website if we would just overwrite instead of checking. First we create a variable named filename in which we store the filename, and we get the value from the URL field of our form. We actually skip the first character of the URL field since it must be a slash (since $PHP_SELF has a slash in the beginning). Then we check if the file already exists, and if it does we're just not going to send any data to the db since we need to chose a different filename first:

$filename = substr($_POST['URL'], 1);

if (file_exists($filename)) {

echo "<h2>File already exists</h2>";

} else {
in the else clause we just put all of the code we already had, including the mysql_query() part. Below that we now put some code to actually create the file and copy the contents of a template file, then show us how many bytes were written so we can confirm that it actually worked:
$template = file_get_contents("template.php");

$file = fopen($filename,"w") or exit("Unable to open file!" . $filename);

echo fwrite($file, $template) . " bytes written";

fclose($file);
 The php code is very straightforward, file_get_contents gets the whole contents of a file and puts them into a single string variable, fopen opens a file, the second parameter there is "w" so that we can actually write into said file, the or exit part will prevent further execution of the php script in case we can't open the file, fwrite writes the contents of a string into a file, it returns the number of bytes written which we then echo to the website, and fclose quite obviously closes the file.
Amazingly enough that's already all we need for one step adding of a whole website to our server.

No comments:

Post a Comment

Facebook