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

Create Guestbook in PHP


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.  

$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'])){
    add_entry($username, $message, $file_name);
$final_content = file_get_contents($file_name);
<!DOCTYPE html>
    <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>
<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)
    <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">
            echo $final_content;
<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 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">

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 (17) Add New Comment
Joe Gab2024-06-18 9:40 PM

Hello, Our private lending group pantners is seeking interests from genuine and reliable individuals or companies in need of startup capital or business expansion funds. We invite you to take advantage of this opportunity for the benefit of your business. Contact me by email if you are interested, on: joeygab14@gmail.com Thanks Joe Gab Investment Analyst London, Uk

diplomaxose2024-06-13 10:24 PM

?????????? ??? ???????? ???????????? (?????) ?? ?????????? ?????? ????? ?????? ? ????? ???????. ?????? ??????: ?????? ??????? ??? ?????? ?? ??????. ????? ?????????, ?? ?? ??????? ????????, ????????? ?? ?????? ??????????? ????? ????????? ? ????? ???. ? ????? ?????? ????? 100 ??????????? ??????????? ??????? ?????????? ?????? ? ????? ???????. ? ??????????? ?? ?????? ??????? ??????? ??? ??? ??????????? ????????? ? ?????? ?????????? ????????. - interestbook.ru

neurontnMa2024-06-12 2:39 AM

Thanks for magnificent info I was looking for this information for my mission.

OLanekix2024-06-11 2:31 PM

Simply wish to say your article is as amazing. The clarity in your post is simply excellent and i can assume you're an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the gratifying work. ediblehomegardensresort.com/index.php?do=/public/blog/view/id_119635/title_-2024/?  est-m.ru/katalog/ukg/?  dveribiz.ru/shop/vkhodnye-dveri/ekonom-klass/?  worldgonews.ru/page/37?  bn.tobase.ru/viewtopic.php?f=30&t=2717? 

LewisRar2024-06-08 10:44 PM

Howdy just wanted to give you a brief heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different browsers and both show the same outcome. forums.worldsamba.org/showthread.php?tid=992935?  drosetourmanila.com/vn/retail_promo.html?  mafia-game.ru/forum/member.php?u=28444?  forexrassia.ru/page/4?  cuscino.vn/danh-muc/san-pham-khac/ruot-goi? 

Leonardo2024-05-01 4:35 AM

Hello! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no back up. Do you have any methods to protect against hackers?

Janina2024-04-30 5:57 PM

Very nice article, exactly what I wanted to find.

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

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?

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.

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.

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!

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!

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.

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!