search
HomeBackend DevelopmentPHP TutorialMethod to implement message board based on thinkPHP framework

Method to implement message board based on thinkPHP framework

May 05, 2018 pm 03:25 PM
phpthinkphpmessage board

This article mainly introduces the method of implementing a message board based on the thinkPHP framework. It briefly analyzes the process of implementing the message board in the thinkPHP framework and the related core code of the controller and model. Friends in need can refer to it

The example in this article describes the method of implementing a message board based on the thinkPHP framework. I would like to share it with you for your reference. The details are as follows:

After struggling for a day, the concept version of THINKPHP Xiao Deng’s message board was finally released.

In fact, THINKPHP is really developing very quickly. As an Internet user, There is nothing wrong with those who "move bricks" and engage in this kind of pure code farmer's work.

The code implements the following functions

1. Message function.

2. Verification function.

3. Paging display function.

Just wrote a few lines of code (PS: The page design code does not count, even the controller and model code)

Now I will publish the code of the controller, about THINKPHP I won’t elaborate on the coding rules, just read the thinkphp manual.

class IndexAction extends Action
{
  public function index() {
    $Form = M("word");
    // 按照id排序显示前6条记录
    import("@.ORG.Page");    //导入分页类
      $count = $Form->count();  //计算总数
      $p = new Page ( $count, 1 );
      $list=$Form->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
      $page = $p->show ();
      $this->assign ( "page", $page );
      $this->assign ( "list", $list );
    $this->display(); //模板调用,这个是关键。
  }
  //数据插入
  public function insert() {
    $word = D("word");
     if($vo = $word->create())
       {
         if(false !== $word->add())
        {
           $this->success("数据添加成功");
         }
         else
         {
          $this->error('数据写入错误!');
         }
       }
    else
      {
       $this->error($word->getError());
      }
  }
  //验证重复
  public function checkTitle()
  {
    if (!empty($_POST['username'])) {
      $Form = M("word");
      //getByTitle是model的获取数据根据某字段获取记录的魔术方法
      //比如getById etc getByXXX XXX大写
      if ($Form->getByUsername($_POST['username'])) {
        $this->error(&#39;<font color=red>标题已经存在</font>&#39;);
      } else {
        $this->success(&#39;标题可以使用!&#39;);
      }
    } else {
      $this->error(&#39;标题必须&#39;);
    }
  }
}

The following is the code to verify the model

class wordModel extends Model{
  protected $_validate = array(
   array(&#39;username&#39;, &#39;require&#39;, &#39;称呼必须!&#39;, 1),//1为必须验证
   array(&#39;email&#39;, &#39;email&#39;, &#39;邮箱格式错误!&#39;, 2),//2为不为空时验证
   array(&#39;qq&#39;,&#39;number&#39;,&#39;QQ号错误&#39;,2),
   array(&#39;content&#39;, &#39;require&#39;, &#39;内容必须&#39;,1),
   array(&#39;username&#39;,&#39;&#39;,&#39;称呼已经存在&#39;,0,&#39;unique&#39;,1)
  );
  protected $_auto = array(
   array(&#39;datetime&#39;, &#39;get_date&#39;,1, &#39;callback&#39;),
   array(&#39;ip&#39;,&#39;getip&#39;,1,&#39;callback&#39;)
  );
  protected function get_date()
  {
   return date("Y-m-d H:i:s");
  }
  protected function getip()
  {
   return $_SERVER[&#39;REMOTE_ADDR&#39;];
  }
}

thinkphp has one thing to pay attention to , in CURD operations, table names are required.

Related recommendations:

Build OAuth20 service based on ThinkPHP framework

The above is the detailed content of Method to implement message board based on thinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.