2019-02-24 Pujan Niroula 3 Minute(s) Read

Create Guestbook in PHP

img-thumbnails.png

Guestbook is not mandatory for site but if you want to have a guestbook where visitor of your site leaves message publicly then this post is for you. Guestbook is a better way to show your site's fame. If a guest appreciate your product on site through contact us form then just you will know but If a visitor publicly posted on guestbook then everyone will know your service or product's fame.

Guestbook Example

Most of guestbook script available on internet is based on PHP and Mysql. That is little lengthy process where you have to setup database and tables. But today I will show you how to create guestbook without MYSQL.  

<?php
$file_name = 'guestbook.txt';
if(!file_exists($file_name)) {
        file_put_contents($file_name, '');
    }
function check_input($input){
    if(empty(trim($input))) {
        die('Fill all fields properly');
    } else {
        return htmlspecialchars($input);
    }
}
function add_entry($username, $message, $file_name) {
    $get_content = file_get_contents($file_name);
    $time = date('Y-m-d h:i A',time());
    $content = '<div class="container border rounded p-3 bg-light"><b>'.$username.'</b><span class="text-muted"> ('.$time.')</span>'.'<p class="text-dark">'.$message."</p></div>\n".$get_content;
    file_put_contents($file_name, $content);
}
if(isset($_POST['username']) && isset($_POST['message'])){
    $username=check_input($_POST['username']);
    $message=check_input($_POST['message']);
    add_entry($username, $message, $file_name);
}
$final_content = file_get_contents($file_name);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Simple Guestbook System by Pujann</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body class="bg-info">
<div class="container text-center">
    <h1>Simple Guestbook System without Mysql in PHP</h1>
    <span class="bg-light p-2">
        <?php echo substr_count($final_content, 'container border rounded p-3');?> Message(s)
    </span>
    <button class="btn btn-success" data-toggle="modal" data-target="#addMessage">Add New Message</button>
    <div class="row mt-2">
        <div class="col-md-6 offset-md-3 col-sm-12">
            <?php
            echo $final_content;
            ?>
        </div>
    </div>
</div>
<div class="modal fade" id="addMessage" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h3>Leave a Message</h3>
            </div>
            <div class="modal-body">
                <div class="container border rounded bg-light p-2">
                    <form action="" method="post" class="form">
                        <input type="text" class="form-control mb-1" name="username" placeholder="Full Name(eg. John Doe)" required="">
                        <textarea name="message" id="" cols="10" rows="5" class="form-control" placeholder="Leave your message...." required=""></textarea>
                        <input type="submit" class="btn btn-danger mt-1" value="Submit">
                    </form>                
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Note: It will create a new file named guestbook.txt on the folder it is uploaded to and store all the guestbook messages. If you deleted guestbook.txt all data will be lost.

Thanks for reading...

Tags:php, how to,
Comments (33) Add New Comment
Aja2024-02-23 1:21 AM

fantastic publish, very informative. I ponder why the other experts of this sector don't notice this. You should continue your writing. I am sure, you have a great readers' base already!

Reply
Candida2024-02-16 12:21 AM

Hello, I do believe your site could possibly be having web browser compatibility problems. When I look at your website in Safari, it looks fine however, when opening in IE, it's got some overlapping issues. I merely wanted to provide you with a quick heads up! Besides that, wonderful site!

Reply
Hildegarde2024-02-13 9:49 AM

Hello, I enjoy reading all of your article. I wanted to write a little comment to support you.

Reply
Orval2024-02-13 2:16 AM

Hello my loved one! I want to say that this post is amazing, nice written and include approximately all important infos. I would like to see more posts like this .

Reply
Sabrina2024-02-11 2:53 PM

Hey there! Would you mind if I share your blog with my twitter group? There's a lot of folks that I think would really enjoy your content. Please let me know. Cheers

Reply
Rogelio2024-02-11 7:41 AM

Currently it seems like Wordpress is the best blogging platform out there right now. (from what I've read) Is that what you're using on your blog?

Reply
Alma2024-02-10 10:38 AM

When some one searches for his vital thing, so he/she wishes to be available that in detail, therefore that thing is maintained over here.

Reply
Luther2024-02-09 6:57 AM

For latest news you have to go to see the web and on web I found this website as a finest site for most recent updates.

Reply
Tammara2024-02-09 12:37 AM

Greetings! I've been following your website for a long time now and finally got the bravery to go ahead and give you a shout out from Humble Texas! Just wanted to mention keep up the great work!

Reply
avon Representative Uk2021-06-07 6:56 PM

Hi there, for all time i used to check website posts here early in the dawn, because i like to find out more and more.

Reply
Mehmet2022-03-26 12:51 PM

w

buy avon online uk2021-05-26 2:42 PM

Great blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my website loaded up as fast as yours lol

Reply
9kg condenser tumble dryer2021-05-21 4:25 AM

Hi there are using Wordpress for your blog platform? I'm new to the blog world but I'm trying to get started and set up my own. Do you need any coding knowledge to make your own blog? Any help would be greatly appreciated!

Reply
Kickass Torrents2020-02-08 11:25 AM

Nice post. I learn something new and challenging on sites I stumbleupon everyday. It will always be helpful to read through articles from other authors and practice something from other sites.

Reply

What's up, after reading this awesome post i am too glad to share my knowledge here with mates.

Reply
Jav HD2019-09-30 3:39 AM

My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on a number of websites for about a year and am anxious about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be greatly appreciated!

Reply