PDA

View Full Version : The "One Page" Website.


Josh-
30 Nov 2003, 12:42am
The "One Page" Website
-- written by Josh


You may notice, that sometimes you see sites using one or two pages, rather then using TONS of pages. This isn't usually what it seems. Sometimes they may be using a simple script, that makes it seem so. If you don't understand what I'm talking about, look at short-medias forums. They use similar forms of it everywhere. I see one right now, http://short-media.com/forum/newthread.php?s=&action=newthread&forumid=27. Instead of having a page named "s", ect, they have a page named newthread.php that has another type of "section" of it, "s". I use this to manage my websites, pretty much. You can also make custom 404 pages, which can sometimes be useful to you. Instead of having to go to each individual page, such as:

Index.php...
Contact.php..
About.php..
Ect..

(this is just an example.)

You can now only appear to have on file, which may appear as:
Home.php?section=index or Home.php?section=contact.php..

Now to start with the coding of the PHP:

Your index.php file should include the following:

<?php
if ( @!$section ) {
$section = "home";
}
if ( @$result = include ( "$section" . ".php" ) ) {
} else {
include ("404.php");
}
?>

Upon loading of your website, it will say index.php, but will be loading home.php. From linking from page to page, you will only need to use index.php?section=(pagename), no PHP included. You can also include HTML files, ect this way.

However, the most useful part, and mostly main part of this small script, is to have more organization, and the usefulness of custom 404 pages.

Everytime something is entered, other then a file that you have, it will bring up 404.php. You can easily modify it to become 404.html, ect.

For example, lets say you don't have a chat.php file in your folder.

When someone tries to go to index.php?section=chat, rather then loading a regular server or windows 404 error, it will load your 404.php page, ect.

I guess you may not want this code, except for looks, but don't underestimate custom 404 pages. They can be very useful, such as you can include something that will email you when the 404 page is opened. This can be very useful in administrating a website with a large amount of pages.

Hopefully there is someone out here that can make use of this, and I wish them best of luck in doing it.

Your welcome,
Josh.

TheLostSwede
30 Nov 2003, 01:05am
Great stuff Josh,

I am following your guides with great interest. Good man.

Josh-
30 Nov 2003, 01:06am
Example of a Custom 404 Page


<h1>404 ERROR - FILE/FOLDER NOT FOUND!</h1>
<?php
if ($do != "sendmail") {
?>
<form name="sendmail" action="404.php" method="post">
<font face="Verdana,Arial" size="2">
<input type="hidden" name="do" value="sendmail">
E-Mail: <input type="text" name="email" size="60"><br />
404 error found at: <input type="text" name="subject" size="60"><br />
Comments:<br /><textarea name="comments" rows="10" cols="30"></textarea><br />
<input type="submit" value="submit">
</font>
</form>
<?php
} elseif ($do == "sendmail") {
mail("admin@yoursite.com",$subject,$email,$comments) or die("Error: the message could not be sent");
} // endif
?>

Will allow the user to send you email about where the 404 was found, ect.

Josh-
30 Nov 2003, 01:07am
Mackanz had this to say
Great stuff Josh,

I am following your guides with great interest. Good man.

Thanks. It makes me proud to see that I'm being appreciated :)

Hopefully more tutorials (guides) to come:D
-- Josh

Josh-
30 Nov 2003, 01:09am
&lt;form name="sendmail" action="404.php" method="post"&gt;

404.php could be changed to echo the url of the page, I suppose. But 404.php is more simple, and just as efficient.

Park_7677
30 Nov 2003, 01:12am
The script won't work if GLOBALS are off (see below). They are in PHP 4+ by default as they're a security risk. So, just ask for the data before the IF..THEN:
$section = $_REQUEST["section"];

GLOABLS in PHP 4 are off:
From: PHP Documentation (http://www.php.net/manual/en/language.variables.predefined.php)

In PHP 4.2.0 and later, the default value for the PHP directive register_globals (http://www.php.net/manual/en/configuration.directives.php#ini.register-globals) is off. This is a major change in PHP. Having register_globals off affects the set of predefined variables available in the global scope. For example, to get DOCUMENT_ROOT you'll use $_SERVER['DOCUMENT_ROOT'] instead of $DOCUMENT_ROOT, or $_GET['id'] from the URL http://www.example.com/test.php?id=3 instead of $id, or $_ENV['HOME'] instead of $HOME.

For related information on this change, read the configuration entry for register_globals (http://www.php.net/manual/en/configuration.directives.php#ini.register-globals), the security chapter on Using Register Globals (http://www.php.net/manual/en/security.registerglobals.php), as well as the PHP 4.1.0 (http://www.php.net/release_4_1_0.php) and 4.2.0 (http://www.php.net/release_4_2_0.php) Release Announcements.

Good work Josh :thumbup

Josh-
30 Nov 2003, 01:18am
Thanks for clearing that up for me :)

I wouldn't want anyone to get frustrated, then pissed off at me.

Thanks for the correction,
--Josh

RWB
30 Nov 2003, 02:25am
I can't wait to learn this stuff in an upcoming class :D But that is like 7 months off :( Won't hurt to learn now though, thanks for this stff and keep it coming!

Josh-
30 Nov 2003, 02:56am
Thanks guys :)

Do any of you have any requests? Maybe I can write it up. :/

Kwitko
30 Nov 2003, 03:52am
I wrote a basic CMS for a website I did using the one page approach. Adding, deleting, and editing records was all done with one page. For me it makes it easier to keep track of your code. I use a case switch for the different sections. The switch is determined by a hidden variable in the form named action whose value is either "add", "delete", or "edit".


switch ($action) {
case "add":
add();
showForm();
showTable();
break;
case "delete":
delete();
showForm();
showTable();
break;
case "edit":
edit();
break;
default:
showForm();
showTable();
}


$action is just $_REQUEST['action']. So using the case switch and some basic functions I can reuse a good chunk of my code. Good stuff, Josh. Keep it coming.

Josh-
30 Nov 2003, 06:55am
I'm in the process of writing a full CMS data-base driven program. Going to be very extended, extreme amount of features.

RWB
18 Feb 2005, 05:27am
I am trying to get this to work, I haven't done any coding in a while so it's a bit sketchy.

Here is what I have so far... copied most of what is in this thread for the sake of just getting it to work.... I have another page, but I don't know where to add it, or how to set it's "section". I'm all around a bit lost, but I understand the jist of it I think.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
****** name="author" content="Christopher Sklenarik" />
****** name="keywords" content="3d, modeling, designer, vector, raster, art, tutorials, editorials" />
****** name="description" content="The Portfolio of Christopher Sklenarik" />
****** name="robots" content="all" />
<title>Fusion Digital Media - The Art of Christopher Sklenarik</title>
<!--
//For future CSS Style when graphics and final design is ready.
<style type="text/css" title="currentStyle" media="screen">
@import "/001/001.css";
</style>

//For future use of an Icon when graphics and final design is ready.
<link rel="Shortcut Icon" type="none/no-icon" href="http://www.fusion-dm.net/no-icon.ico" />
-->
<?php
if ( @!$section ) {
$section = "home";
}
if ( @$result = include ( "$section" . ".php" ) ) {
} else {
include ("404.php");
}
?>
</head>

<body>
<h1>404 ERROR - FILE/FOLDER NOT FOUND!</h1>
<?php
if ($do != "sendmail") {
?>
<form name="sendmail" action="404.php" method="post">
<font face="Verdana,Arial" size="2">
<input type="hidden" name="do" value="sendmail">
E-Mail: <input type="text" name="email" size="60">
404 error found at: <input type="text" name="subject" size="60">
Comments:<textarea name="comments" rows="10" cols="30"></textarea>
<input type="submit" value="submit">
</font>
</form>
<?php
} elseif ($do == "sendmail") {
mail("me@mysite.net",$subject,$email,$comments) or die("Error: the message could not be sent");
} // endif
?>
</body>
</html>

a2jfreak
18 Feb 2005, 05:39pm
You'll need to:

$section = $_REQUEST['section'];

so that $section will have a value.

In the actual HTML you'll need:

<input type="hidden" name="section" value="put_the_value_here" />


You can change the value of 'section' by (assuming the form name is mainForm):

document.mainForm.section.value = 'new Value';

RWB
18 Feb 2005, 09:59pm
now I am just confused even more

a2jfreak
18 Feb 2005, 10:41pm
I don't have time to walk you through it today . . . if someone else doesn't clarify or you don't have it figured out by Monday then (if I remember) I'll try to clarify for you.

RWB
18 Feb 2005, 11:31pm
Thanks a2jfreak, but Shorty helped out out, I've got it working and all that now :)