如何轻松制作PHP Shoutbox应用程序

Today I will tell you about creating easy Shoutbox application using our existed login system. This will useful and simple solution. We will able to shout something with our logged members. We will use database to store messages.

今天,我将告诉您有关使用我们现有的登录系统创建简单的Shoutbox应用程序的信息。 这将是有用且简单的解决方案。 我们将能够与我们已登录的成员一起大喊大叫。 我们将使用数据库来存储消息。

Here is sample:

这是示例:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

So download the example files and lets start coding!

因此,下载示例文件并开始编码!

步骤1. HTML (Step 1. HTML)

As usual, we start with the HTML. This is login form code.

和往常一样,我们从HTML开始。 这是登录表单代码。

login_form.html (login_form.html)

<form class="login_form" method="post" action="index.php">
    <div>Username: <input type="text" name="username" /></div>
    <div>Password: <input type="password" name="password" /></div>
    <div><input type="submit" value="Login" name="Login" /></div>
</form>
<div>You can use username "User1" of "User2" and password "qwerty" to login in system</div>

<form class="login_form" method="post" action="index.php">
    <div>Username: <input type="text" name="username" /></div>
    <div>Password: <input type="password" name="password" /></div>
    <div><input type="submit" value="Login" name="Login" /></div>
</form>
<div>You can use username "User1" of "User2" and password "qwerty" to login in system</div>

Here are new 2 template files to shoutbox:

这是shoutbox新增的2个模板文件:

shoutbox_begin.html (shoutbox_begin.html)

<hr />
<link type="text/css" rel="stylesheet" href="shoutbox.css" />
<div class="shoutbox_main">
    <h3>Shoutbox</h3>

<hr />
<link type="text/css" rel="stylesheet" href="shoutbox.css" />
<div class="shoutbox_main">
    <h3>Shoutbox</h3>

shoutbox_end.html (shoutbox_end.html)

    <form class="s_form" method="post" action="index.php">
        <div><input type="text" name="s_message" /><input type="submit" value="Say" name="s_say" /></div>
    </form>
    <div>You can shout anything in this chat</div>
</div>

    <form class="s_form" method="post" action="index.php">
        <div><input type="text" name="s_message" /><input type="submit" value="Say" name="s_say" /></div>
    </form>
    <div>You can shout anything in this chat</div>
</div>

步骤2. CSS (Step 2. CSS)

Here are used CSS styles.

这是使用CSS样式。

styles.css (styles.css)

.login_form {
    border: 1px solid #AAA;
    padding:10px;
}

.login_form {
    border: 1px solid #AAA;
    padding:10px;
}

shoutbox.css (shoutbox.css)

.shoutbox_main {
    border:1px solid #AAA;
    width:350px;
    padding:10px;
}
.message {
    border-bottom:1px solid #AAA;
    padding:2px;
}
.message span {
    font-size:10px;
    color:#888;
    margin-left:10px;
}
.s_form {
    margin:0px;
    padding:10px 0;
}

.shoutbox_main {
    border:1px solid #AAA;
    width:350px;
    padding:10px;
}
.message {
    border-bottom:1px solid #AAA;
    padding:2px;
}
.message span {
    font-size:10px;
    color:#888;
    margin-left:10px;
}
.s_form {
    margin:0px;
    padding:10px 0;
}

步骤3. SQL (Step 3. SQL)

We will need to execute next SQL in our database.

我们将需要在数据库中执行下一个SQL。


CREATE TABLE `s_messages` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`user` VARCHAR( 255 ) NOT NULL ,
`message` VARCHAR( 255 ) NOT NULL ,
`when` INT( 11 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `s_messages` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`user` VARCHAR( 255 ) NOT NULL ,
`message` VARCHAR( 255 ) NOT NULL ,
`when` INT( 11 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

步骤4. PHP (Step 4. PHP)

Now that we followed all the code on the front-end, it is now time for the last part of this tutorial – the PHP back-end.

既然我们已经遵循了前端上的所有代码,那么现在是本教程的最后部分– PHP后端。

As we can see – this is just pure class with functions for login system.

我们可以看到–这只是具有登录系统功能的纯类。

I used next functions:

我使用了下一个功能:

getLoginBox – function return login form. In case if member logged – it will return Hello member record and possibility for logout

getLoginBox –函数返回登录表单。 如果成员已登录–它将返回Hello成员记录和注销的可能性

simple_login – perform login to system (storing necessary information in cookies)

simple_login –执行登录到系统(将必要的信息存储在cookie中)

simple_logout – perform logout (clearing used cookies)

simple_logout –执行注销(清除使用的cookie)

check_login – return true in case if username and password exists in system

check_login –如果系统中存在用户名和密码,则返回true

getShoutbox – function return shoutbox form.

getShoutbox –函数返回shoutbox表单。

index.php (index.php)

// initialization of login system and generation code
$oSimpleLoginSystem = new SimpleLoginSystem();
// draw login box
echo $oSimpleLoginSystem->getLoginBox();
// draw shoutbox application
echo $oSimpleLoginSystem->getShoutbox();
// class SimpleLoginSystem
class SimpleLoginSystem {
    // variables
    var $aExistedMembers; // Existed members array
    // constructor
    function SimpleLoginSystem() {
        $this->aExistedMembers = array(
            'User1' => 'd8578edf8458ce06fbc5bb76a58c5ca4',  //Sample: MD5('qwerty')
            'User2' => 'd8578edf8458ce06fbc5bb76a58c5ca4'
        );
    }
    function getLoginBox() {
        ob_start();
        require_once('login_form.html');
        $sLoginForm = ob_get_clean();
        $sLogoutForm = '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>';
        if ((int)$_REQUEST['logout'] == 1) {
            if (isset($_COOKIE['member_name']) && isset($_COOKIE['member_pass']))
                $this->simple_logout();
        }
        if ($_REQUEST['username'] && $_REQUEST['password']) {
            if ($this->check_login($_REQUEST['username'], MD5($_REQUEST['password']))) {
                $this->simple_login($_REQUEST['username'], $_REQUEST['password']);
                return 'Hello ' . $_REQUEST['username'] . '! ' . $sLogoutForm;
            } else {
                return 'Username or Password is incorrect' . $sLoginForm;
            }
        } else {
            if ($_COOKIE['member_name'] && $_COOKIE['member_pass']) {
                if ($this->check_login($_COOKIE['member_name'], $_COOKIE['member_pass'])) {
                    return 'Hello ' . $_COOKIE['member_name'] . '! ' . $sLogoutForm;
                }
            }
            return $sLoginForm;
        }
    }
    function simple_login($sName, $sPass) {
        $this->simple_logout();
        $sMd5Password = MD5($sPass);
        $iCookieTime = time() + 24*60*60*30;
        setcookie("member_name", $sName, $iCookieTime, '/');
        $_COOKIE['member_name'] = $sName;
        setcookie("member_pass", $sMd5Password, $iCookieTime, '/');
        $_COOKIE['member_pass'] = $sMd5Password;
    }
    function simple_logout() {
        setcookie('member_name', '', time() - 96 * 3600, '/');
        setcookie('member_pass', '', time() - 96 * 3600, '/');
        unset($_COOKIE['member_name']);
        unset($_COOKIE['member_pass']);
    }
    function check_login($sName, $sPass) {
        return ($this->aExistedMembers[$sName] == $sPass);
    }
    // shoutbox functions addon
    function getShoutbox() {
        //the host, name, and password for your mysql
        $vLink = mysql_connect("localhost","username","password");
        //select the database
        mysql_select_db("database_name");
        // adding to DB table posted message
        if ($_COOKIE['member_name']) {
            if(isset($_POST['s_say']) && $_POST['s_message']) {
                $sUsername = $_COOKIE['member_name'];
                $sMessage = mysql_real_escape_string($_POST['s_message']);
                mysql_query("INSERT INTO `s_messages` SET `user`='{$sUsername}', `message`='{$sMessage}', `when`=UNIX_TIMESTAMP()");
            }
        }
        //returning the last 5 messages
        $vRes = mysql_query("SELECT * FROM `s_messages` ORDER BY `id` DESC LIMIT 5");
        $sMessages = '';
        // collecting list of messages
        while($aMessages = mysql_fetch_array($vRes)) {
            $sWhen = date("H:i:s", $aMessages['when']);
            $sMessages .= '<div class="message">' . $aMessages['user'] . ': ' . $aMessages['message'] . '<span>(' . $sWhen . ')</span></div>';
        }
        mysql_close($vLink);
        ob_start();
        require_once('shoutbox_begin.html');
        echo $sMessages;
        require_once('shoutbox_end.html');
        $sShoutboxForm = ob_get_clean();
        return $sShoutboxForm;
    }
}

// initialization of login system and generation code
$oSimpleLoginSystem = new SimpleLoginSystem();
// draw login box
echo $oSimpleLoginSystem->getLoginBox();
// draw shoutbox application
echo $oSimpleLoginSystem->getShoutbox();
// class SimpleLoginSystem
class SimpleLoginSystem {
    // variables
    var $aExistedMembers; // Existed members array
    // constructor
    function SimpleLoginSystem() {
        $this->aExistedMembers = array(
            'User1' => 'd8578edf8458ce06fbc5bb76a58c5ca4',  //Sample: MD5('qwerty')
            'User2' => 'd8578edf8458ce06fbc5bb76a58c5ca4'
        );
    }
    function getLoginBox() {
        ob_start();
        require_once('login_form.html');
        $sLoginForm = ob_get_clean();
        $sLogoutForm = '<a href="'.$_SERVER['PHP_SELF'].'?logout=1">logout</a>';
        if ((int)$_REQUEST['logout'] == 1) {
            if (isset($_COOKIE['member_name']) && isset($_COOKIE['member_pass']))
                $this->simple_logout();
        }
        if ($_REQUEST['username'] && $_REQUEST['password']) {
            if ($this->check_login($_REQUEST['username'], MD5($_REQUEST['password']))) {
                $this->simple_login($_REQUEST['username'], $_REQUEST['password']);
                return 'Hello ' . $_REQUEST['username'] . '! ' . $sLogoutForm;
            } else {
                return 'Username or Password is incorrect' . $sLoginForm;
            }
        } else {
            if ($_COOKIE['member_name'] && $_COOKIE['member_pass']) {
                if ($this->check_login($_COOKIE['member_name'], $_COOKIE['member_pass'])) {
                    return 'Hello ' . $_COOKIE['member_name'] . '! ' . $sLogoutForm;
                }
            }
            return $sLoginForm;
        }
    }
    function simple_login($sName, $sPass) {
        $this->simple_logout();
        $sMd5Password = MD5($sPass);
        $iCookieTime = time() + 24*60*60*30;
        setcookie("member_name", $sName, $iCookieTime, '/');
        $_COOKIE['member_name'] = $sName;
        setcookie("member_pass", $sMd5Password, $iCookieTime, '/');
        $_COOKIE['member_pass'] = $sMd5Password;
    }
    function simple_logout() {
        setcookie('member_name', '', time() - 96 * 3600, '/');
        setcookie('member_pass', '', time() - 96 * 3600, '/');
        unset($_COOKIE['member_name']);
        unset($_COOKIE['member_pass']);
    }
    function check_login($sName, $sPass) {
        return ($this->aExistedMembers[$sName] == $sPass);
    }
    // shoutbox functions addon
    function getShoutbox() {
        //the host, name, and password for your mysql
        $vLink = mysql_connect("localhost","username","password");
        //select the database
        mysql_select_db("database_name");
        // adding to DB table posted message
        if ($_COOKIE['member_name']) {
            if(isset($_POST['s_say']) && $_POST['s_message']) {
                $sUsername = $_COOKIE['member_name'];
                $sMessage = mysql_real_escape_string($_POST['s_message']);
                mysql_query("INSERT INTO `s_messages` SET `user`='{$sUsername}', `message`='{$sMessage}', `when`=UNIX_TIMESTAMP()");
            }
        }
        //returning the last 5 messages
        $vRes = mysql_query("SELECT * FROM `s_messages` ORDER BY `id` DESC LIMIT 5");
        $sMessages = '';
        // collecting list of messages
        while($aMessages = mysql_fetch_array($vRes)) {
            $sWhen = date("H:i:s", $aMessages['when']);
            $sMessages .= '<div class="message">' . $aMessages['user'] . ': ' . $aMessages['message'] . '<span>(' . $sWhen . ')</span></div>';
        }
        mysql_close($vLink);
        ob_start();
        require_once('shoutbox_begin.html');
        echo $sMessages;
        require_once('shoutbox_end.html');
        $sShoutboxForm = ob_get_clean();
        return $sShoutboxForm;
    }
}

结论 (Conclusion)

I described one of easy and useful shoutbox application based on PHP and MySQL. You can use this material to create own scripts into your startups. Good luck !

我描述了一个基于PHP和MySQL的简单实用的shoutbox应用程序。 您可以使用此材料在创业公司中创建自己的脚本。 祝好运 !

翻译自: https://www.script-tutorials.com/how-to-easily-make-shoutbox-application/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值