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 (337) Add New Comment
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