NameSilo

Simple GuestBook

Located in Source Code started by Eric, Jul 24, 2005.

Replies:
10
Views:
20,193

  1. Eric

    Eric Top Member VIP ★★★★★★★★★★

    Posts:
    6,038
    Likes Received:
    345
    A simple guestbook. Stores information in a text file.
    EDIT: Script Updated. Now v1.0.2 :)

    If you have any problems with this script, i.e. Any errors. Or, if you have any suggestions, PM or reply to this thread.

    -Tweaked the code a lil.
    -Few minor bugs fixed.

    guestbook.php (Displays Entries)
    PHP:
    <html>
    <head>
    <title>Guestbook</title>
    </head>

    <body>
    <?php

    $file 
    "guestbook.txt";
    if(
    file_exists($file))
    {
      
    $fp fopen($file"r");
      
    $book fread($fpfilesize($file));
      
    fclose($fp);
      echo 
    nl2br($book);
    }
    else
    {
      echo 
    "Sorry, seems there was an error, please wait a few minutes and try again.";
    }
    ?>
    </body>
    </html>
    addentry.html (The form)
    HTML:
    <html>
    <head>
    <title>Add an Entry</title>
    </head>
    
    <body>
    
    <form method="post" action="addentry.php">
    Name:
    <br>
    <input name="name" type="text" size="30" maxlength="40">
    <br><br>
    Email:
    <br>
    <input name="email" type="text" size="30" maxlength="40">
    <br><br>
    Home Page:
    <br>
    <input name="site" type="text" size="30" value="http://" maxlength="40">
    <br><br>
    Message:
    <br>
    <textarea name="message" cols="25" rows="5">
    </textarea>
    <br><br>
    <input type="submit" value="Add">
    </form>
    
    </body>
    </html>
    
    addentry.php (what the form submits the information to)
    PHP:
    <html>
    <head>
    <title>Thank You</title>
    </head>

    <body>
    <?php

    $file 
    "guestbook.txt";
    $name $_POST['name'];
    $email $_POST['email'];
    $site $_POST['site'];
    $message $_POST['message'];
    $date date("l dS of F Y h:i:s A");

    $site trim(stripslashes(strip_tags($site));
    $message trim(stripslashes(strip_tags($message));
    $email  trim(stripslashes(strip_tags($email));
    $name trim(stripslashes(strip_tags($name)));


    if(empty(
    $email) || empty($name) || empty($message))
    {
      echo 
    'Sorry all fields are required.';
    }
    else
    {
      
    $validate preg_match('/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/';, $email);

      if(!
    $validate)
      {
        echo(
    $email "is not a valid email!");
      }
      else
      {
        
    $e $email;
      }
      if(
    file_exists($file))
      {
        
    $fp fopen($file"a");
        
    fwrite($fp'
        <font size="4">
        <br><br><br>
        Name: '
    .$name.'<br>
        Email: '
    .$e.'<br>
        Home Page: <a href="'
    .$site.'">'.$site.'</a><br>
        Message: '
    .$message.'<br><br>
        Added: '
    .$date.'</font>
        '
    );
        
    fclose($fp);
        echo 
    '<p align="center">Thank you '.$name.' for singing my guestbook</p>';
      }
      else
      {
        echo 
    'Sorry, seems there was an error, please try again in a few minutes.';
      }
    }
    ?>

    </body>
    </html>
    edit.php (where you can edit/delete entries)
    PHP:
    <html>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Admin Area</title>
    </head>

    <body>
    <form method="post" action="?do=login">
    <b>Password Required</b>
    Admin Username: <input type="text" name="user" size="20">
    Admin Password: <input type="password" name="pass" size="20">
    <input type="submit" value="Submit">
    </form>
    <br>
    <?php

    //I recommend you change the username and password.
    $username "admin";
    $password "password";

    if(
    $do == "login")
    {
      if(
    $_POST['user'] == $username && $_POST['pass'] == $password)
      {
        echo 
    "Login successful.";
        echo 
    "<br>";
        echo 
    "<br>";

        
    $file "guestbook.txt";
        if(
    file_exists($file))
        {
          
    $fp fopen($file"r");
          
    $book fread($fpfilesize($file));
          
    fclose($fp);
        }
    ?>
    <form method="post" action="?do=edit">
    <b>If you wish to edit/delete an entry, edit below:</b>
    <br>
    <textarea name="entries" cols="70" rows="20">
    <?php
    echo $book;
    ?>
    </textarea>
    <br>
    <input type="submit" name="Submit" value="Save">
    <input type="button" name="Cancel" value="Cancel" onclick="javascript: history.back()">
    </form>
    <br>
    <br>
    <?php
      
    }
      else
      {
        echo 
    "<font color=\"red\">Invalid Username or Password.</font><a href='javascript: history.back()'>Try again.</a>";
      }
    }
    if(
    $do == "edit")
    {
      
    $entries $_POST['entries'];
      
    $entries trim(stripslashes($entries));
      
    $file "guestbook.txt";

      if(
    file_exists($file))
      {
        
    $fp fopen($file"w");
        
    fputs($fp$entries);
        
    fclose($fp);
    ?>
    <br>
    <br>
    Content Edited Successfully.
    <?php
      
    }
    }
    ?>

    </body>
    </html>
    guestbook.txt (Where the entries are stored) - Just upload a blank .txt file. Also.. It has to be writable. CHMOD 0777


    Enjoy :)
     
    Last edited: Nov 22, 2005
    The views expressed on this page by users and staff are their own, not those of NamePros.
  2. whiteknight

    whiteknight Established Member ★★★★★★★★★★

    Posts:
    220
    Likes Received:
    9
    I have a small problem with mine when you go to view the entries the html code popes up also how would you delete them if there are any negative ones?

    http://www.kvchosting.com/code/addentry.html - my version of your code ;)
     
  3. whiteknight

    whiteknight Established Member ★★★★★★★★★★

    Posts:
    220
    Likes Received:
    9
    nm got it working thanks for the script :)
     
  4. Eric

    Eric Top Member VIP ★★★★★★★★★★

    Posts:
    6,038
    Likes Received:
    345
    I'll edit the post in a sec. Here in a min, i'll write a page so ou can edit the guestbook etc..


    -Eric
     
  5. whiteknight

    whiteknight Established Member ★★★★★★★★★★

    Posts:
    220
    Likes Received:
    9
    Is there a way how u can make it so if u wanted to delete negative entries you can do it without having to delete the .txt and re-uploading it and chmoding it?
     
  6. Eric

    Eric Top Member VIP ★★★★★★★★★★

    Posts:
    6,038
    Likes Received:
    345
    Script Updated in First Post. Will add the edit/delete page in a lil bit.

    Edit/Delete page added.
     
  7. AJ

    AJ Established Member ★★★★★★★★★★

    Posts:
    397
    Likes Received:
    4
    Ive got this up and running on my site (http://scoville-jenkins.com/guest_book/guestbook.php). Seems to work really well!:) I did add a link to the add entry page on the main page and then I added a link back to the main page on the thank you page, since those pages didnt have links back already. And I also aint got the edit page up since I wasnt quite sure how to work it lol.:p Anyways... as I said, works great! Thanks SV!:D
     
  8. whiteknight

    whiteknight Established Member ★★★★★★★★★★

    Posts:
    220
    Likes Received:
    9
  9. Eric

    Eric Top Member VIP ★★★★★★★★★★

    Posts:
    6,038
    Likes Received:
    345

    Hello AJ,

    Thanks, I'm glad you like it. :) The edit page, just upload to your server, then go to http://www.yourdomain.com/path/to/edit.php Enter user and pass, then you can delete/edit entries. :)

    Psssssst, I'm working on a SQL version. ;)

    -Eric
     
  10. Coolprogram

    Coolprogram Established Member ★★★★★★★★★★

    Posts:
    663
    Likes Received:
    7
    Dam nice sript SV heck i may even use it.Know what i will use it.
     
  11. strawbs

    strawbs New Member

    Posts:
    1
    Likes Received:
    0
    hi guys really sorry if this is an old post great script i have uploaded all scripts
    to my server CHMODDED .txt file to 0777 i'v added my username and pass in the edit.php
    so i put my username and pass into the admin part and its not letting me in?

    could anyone help
     

Want to reply or ask your own question?

It only takes a minute to sign up – and it's free!
NameWorth
  1. NamePros uses cookies and similar technologies. By using this site, you are agreeing to our privacy policy, terms, and use of cookies.
    Dismiss Notice
Loading...