The "One Page" Website.

Josh-Josh- Royal Oak, MI
edited February 2005 in Internet & Media
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]
<?php
if ( @!$section ) {
$section = "home";
}
if ( @$result = include ( "$section" . ".php" ) ) {
} else {
include ("404.php");
}
?>
[/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.

Comments

  • TheLostSwedeTheLostSwede Trondheim, Norway Icrontian
    edited November 2003
    Great stuff Josh,

    I am following your guides with great interest. Good man.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Example of a Custom 404 Page

    [php]
    <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
    ?>
    [/php]
    Will allow the user to send you email about where the 404 was found, ect.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    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-Josh- Royal Oak, MI
    edited November 2003
    <form name="sendmail" action="404.php" method="post">

    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_7677Park_7677 Missouri Member
    edited November 2003
    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:
    [PHP]$section = $_REQUEST["section"];[/PHP]

    GLOABLS in PHP 4 are off:
    From: PHP Documentation

    In PHP 4.2.0 and later, the default value for the PHP directive 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 instead of $DOCUMENT_ROOT, or $_GET from the URL http://www.example.com/test.php?id=3 instead of $id, or $_ENV instead of $HOME.

    For related information on this change, read the configuration entry for register_globals, the security chapter on Using Register Globals, as well as the PHP 4.1.0 and 4.2.0 Release Announcements.

    Good work Josh :thumbup
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    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
  • RWBRWB Icrontian
    edited November 2003
    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-Josh- Royal Oak, MI
    edited November 2003
    Thanks guys :)

    Do any of you have any requests? Maybe I can write it up. :/
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited November 2003
    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".

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

    $action is just $_REQUEST. 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-Josh- Royal Oak, MI
    edited November 2003
    I'm in the process of writing a full CMS data-base driven program. Going to be very extended, extreme amount of features.
  • RWBRWB Icrontian
    edited February 2005
    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.

    [PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Christopher Sklenarik" />
    <meta name="keywords" content="3d, modeling, designer, vector, raster, art, tutorials, editorials" />
    <meta name="description" content="The Portfolio of Christopher Sklenarik" />
    <meta 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&quot; />
    -->
    <?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>
    [/PHP]
  • a2jfreaka2jfreak Houston, TX Member
    edited February 2005
    You'll need to:
    [php]
    $section = $_REQUEST;
    [/php]
    so that $section will have a value.

    In the actual HTML you'll need:
    [php]
    <input type="hidden" name="section" value="put_the_value_here" />
    [/php]

    You can change the value of 'section' by (assuming the form name is mainForm):
    [php]
    document.mainForm.section.value = 'new Value';
    [/php]
  • RWBRWB Icrontian
    edited February 2005
    now I am just confused even more
  • a2jfreaka2jfreak Houston, TX Member
    edited February 2005
    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.
  • RWBRWB Icrontian
    edited February 2005
    Thanks a2jfreak, but Shorty helped out out, I've got it working and all that now :)
Sign In or Register to comment.