QuoteCode Development process

Index

In the Beginning

As with the ShoutBox code this is a project which I started some time ago [30th October 2002 to be exact] which due to current demand I have chosen to expand apon, explain and release

Currently this is still a work in progress, but you are quite welcome to have the code for your own purposes. If you do; it would be really nice if you could link to me (or this page) from somewhere near your QuoteCode, and e-mail me telling me you have used it, that way I can know if the code is useful or not, and send you any fixes I may come up with.

If you do make any changes to my code I would be really interested to see what you have done, so drop me a line. Thanks :)

To look at the code directly see https://natbat.co.uk/randomquote.txt and to see the list of all current quotes, go tohttps://natbat.co.uk/viewAllQuotes.php.

The Current code

The random quotes generator uses the following form to take the name of the person adding the quote and the quote itself andsend the details to the php code in the same page

The code which generates the above form is as follows:

    

The code that takes the information in the form is in the same file above that of the form. It is only activated if the form has been submitted, the check for submission is to see if the compulsory variable $quote has been set. If it has not been set, then the normal content of the page with the form is displayed.

If, however the form has been submitted the follwing code is executed, resulting in the quote being displayed back to the user who entered it, and consequently e-mailed to me.

if (isset($_POST['quote'])) {
    // This strips the whitespace from either end of each attribute, removes all html and slashes.
    // (Slashes that were put in by php automaticly)
    $quote = trim(strip_tags(stripslashes($_POST['quote'])));
    $name = trim(strip_tags(stripslashes($_POST['name'])));
    
    // If the quote is longer than 300 characters it is striped and a '...' added to the end.
    // (A client side check is also performed in the form using the 'maxlength="500"' attribute in the form.
    if (strlen($quote) >= 500) {
        $quote = substr($quote, 0, 500);
        $quote = $quote."...";
    }
    
    // Same thing for the name but with length of 100 characters.
    if (strlen($name) >= 100) {
        $name = substr($name, 0, 100);
        $name = $name."...";
    }
    
    // So long as the quote is not null, it is stored in the quotes.txt file
    // NOTE: Permissions need to be set on this file as 777 so that it can be added to automaticaly
    if ($quote != "") {
        $fp = fopen("quotes.txt", "a");
        // If the name is not null, write directly to the quotes.txt, if not, set $name = 'Anon'
        if ($name == "") {
            $name = "Anon";
        }
        fwrite($fp, "$quote [$name]\n");
        // close the 'quotes.txt' file
        fclose($fp);
        // Mail the quote to me (or if this code is on your site, to you)
        // This is not my real e-mail address :)
        mail("", "[AUTO] New quote added", "A new quote has been added to 'quotes.txt'\nThe quote was added by: $name\nThe quote added was: $quote");
        print "

New quote added: $quote

Added by: $name

"; } print '

Back

View all quotes

'; } else { ?> // close the php statement so it is possible to enter normal html, // just dont forget to end the if statement at the end

Ok, so the structure of the file now looks like this (The code is in just the one file):

Check to see if the form has been submited

if it has
    prepare the $name and the $quote variables for submission
    if the quote is not null
        open the file 'quotes.txt'
        if the name is null
            set the name to 'Anon'
        print the quote and the name to the file in the predefined format, '$quote [$name]' then a new line
        close the file
        e-mail the quote to the code owner
        tell the user that the quote was entered successfully
    print navigation back, and to view all the quotes
    
If the form has not been submitted
    Display the html form and anything else you want to appear on the page



end the html

Finaly, to display a quote at random, on any page of your site, just use:

And now, a quote: ".$quotes [array_rand ($quotes)]."

"; ?>

So I hope this helps you in your QuoteCode development, for more help you can see my actual php file at: https://natbat.co.uk/randomquote.txt. Also you can e-mail me with any questions or improvements on the code.

Improved Requirements

Not so much requirements at the moment, more as future implementations I would like to include at some point.

  • I intend to allow people to download the quotes.txt file
  • Might be a nice feature if people could subscribe to receive updates every time a new quote is added
  • A database implementation may be on the cards some time in the future.
  • Customisable elements of the code, such as the email address the quote to be sent to.
  • Can't think of anything else much to put here right now, so, er... I'll add stuff later :)

Currently, as I have said before, this is still a work in progress, but the code is released under the creative commons and you are quite welcome to have the it for your own purposes. If you do; it would be really nice if you could link to me (or this page) from somewhere near your QuoteCode, and e-mail me telling me you have used it, that way I can know if the code is useful or not, and send you any fixes I may come up with.

If you implement any improvements to the code, or have any further ideas for development I would love to hear about them. E-mail me then I can then accredit you for your contribution.

And now, a quote: The law is the last result of human wisdom acting upon human experience for the benefit of the public. [Samuel Johnson]

View all quotes

Back to Personal Projects