Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with PHP Guestbook script

    I've managed to write a script for a simple php guestbook without database (it stores the comments into a simple file)
    As I am beginner I used a video guide: http://www.youtube.com/watch?v=JtlRnUNfIj4

    It seems that there are no compilation errors in the script.

    PHP Code:
    <html>
        <head>
        <title>My Guestbook</title>
        
        <?php
            $file_name 
    "messages.txt";
            
    $post_sep "<>";
            
    $info_sep "&";
            
            class 
    writePosts {
                function 
    inputPost($author$message){
                    global 
    $file_name$post_sep$info_sep;
                    
    $date time();
                    
    $self $_SERVER['PHP_SELF'];
                    
    $author urlencode($author);
                    
    $message urlencode($message);
                    
    $fh fopen($file_name"r+");
                    
                    
    fread($fhfilesize($file_name));
                    
    fwrite($fh$author.$info_sep.$date.$info_sep.$message.$post_sep);
                    
    fclose($fh);
                    
                    echo 
    "<script>window.location = '$self'; </script>";
                
                }
                function 
    getPost() {
                    if (isset (
    $_POST['author']) && isset($_POST['message'])){
                        
    $this->inputPost($_POST['author'], $_POST['message']);
                    }
                }
            }
            
            class 
    readPosts{
                function 
    readAll(){
                    global 
    $file_name$post_sep$info_sep;
                    
    $fh fopen($file_name"r");
                    
    $all fread($fhfilesize($file_name));
                    
    fclose($fh);
                    
    $each explode($post_sep$all);
                    
                    
    $i 0;
                    
    $counteach count($each);
                    while(
    $i$counteach 1){
                        
    $post_info explode($info_sep$each[$i]);
                        
    $this->outPost($post_info[0], $post_info[1], $post_info[2]);
                        
    $i++;
                    }
                    
                }
                function 
    outPost($author$date$message){
                    
    $date date("m/d/y"$date);
                    
    $author urldecode($author);
                    
    $message urldecode($message);
                    
                    echo 
    "<div>";
                    echo 
    "( $date ) &nbsp; <strong$author says:</strong><br />";
                    echo 
    "$message </div>";
                }
            }
            
    $write = new writePosts;
            
    $read = new readPosts;
            
    $write->getPost();
        
    ?>
            
        </head>
        
        <body>
        <?php $read->readall(); ?>
        <h1><div align="center">My Guestbook</div></</h1>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <input name="author" /_><br /_>
                <textarea name="message"></textarea><br /_>
                <input type="submit"value="Submit" /_>
            </form>
            
        </body>
     </html>
    So the issue is that when I try to launch the page with firefox or any other browser I get the following error.
    Firefox can't find the file at /C:/wamp/www/$self.

    I would really appreciate it if anybody could help me solve my issue.
    Thank you in advance.
    Last edited by silencekf; 02-07-2010 at 12:23 PM.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Try removing
    PHP Code:
    <?php echo $_SERVER['PHP_SELF']; ?>
    as it's not required
    and also changing
    PHP Code:
    echo "<script>window.location = '$self'; </script>"
    to
    PHP Code:
    echo "<script type=\"text/javascript\">window.location = '".$_SERVER['REQUEST_URI']."'; </script>"
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. That helped and now it's working fine.

    But I came across with another issue.
    If someone writes a bigger comment, well I mean a really long one then it all shows in one row.
    My question is how can I divide so it would not display it in one long row and would not have to scroll the browser sides to read it?
    Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •