如何使用 php 存储和显示任何访问者的输入
我想创建一个非常简单的网站,但不幸的是我的 php 技能很弱。基本上,当用户出现时,我想要一个带有文本和闪烁光标的页面(我可能可以自己弄清楚光标部分,但请随意建议)。当用户键入时,我希望它在他们键入时显示文本,并且当他们按 Enter 键(或单击某些内容/其他内容)时,刚刚键入的文本将被发送到数据库,然后页面将使用该新文本进行更新,供其他人查看。然后光标将在下一行闪烁。所以基本上它就像一个非常简单的维基,任何人都可以添加任何内容,但没有人可以删除之前输入的内容。没有登录什么的。有人可以建议解决这个问题的最佳方法吗?我假设它需要对数据库进行 php 调用来显示初始页面,然后另一个 php 请求发送数据,然后另一个 php 请求显示新页面。我只是不知道细节。非常感谢!
额外问题 1:如何动态更新页面,以便如果 A 在 B 打字时发送文本,B 会立即在 B 的页面上看到 A 发送的文本?
附加问题 2:如果该数据库变得非常大(例如,数百万字),可能会出现哪些类型的问题,以及我如何预先解决这些问题?如果有必要,我可以在任何给定页面上仅显示一小部分(纯文本)数据库,然后进行分页。
I want to create a very simple site, but unfortunately my php skills are weak. Basically, when a user shows up, I want to have a page with text and a blinking cursor (I can probably figure the cursor part out myself, but feel free to suggest). When a user types, I want it to show the text as they type, and when they hit enter (or click something/whatever), the text just typed will be sent to a database and then the page will update with that new text, for anybody else to see. The cursor will then be blinking on the next line down. So basically it's like a really simple wiki, where anyone can add anything, but nobody can ever remove what has been typed before. No logging in or anything. Can someone suggest the best way to go about this? I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page. I just don't know the details. Thanks so much!
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只有一页,则不需要数据库。您需要做的就是在服务器上保存一个文本文件(使用
fopen()
和相关函数)仅被附加到。如果您有多个页面,则使用简单的id (INTEGER)、filetext (LARGEBLOB)
。 (注意largeblob
的限制为2 ^32 字节)。对于用户的浏览器部分,您需要使用 Javascript 和 AJAX 来通知服务器任何更新。只需联系一个 PHP 脚本,该脚本 (1) 接受输入并 (2) 将其附加到文件中。
还可以使用 AJAX 调用来获取新内容(例如,如果您分配行号,则浏览器只会告诉脚本它读取的最后一行,并且脚本会返回该点之后的所有新行)。
差不多了。但当浏览器访问该文件时,仅发送该文件的最后 50 行左右。您不想让浏览器崩溃。
以字节而不是单词来思考,您可能会遇到性能问题。您可以限制文件大小或将存储分成一定大小的多个文件,这样您就不必扫描很少会获取的传递内容。
If you only have one page, you don't need a database. All you need to do is save a text file on the server (use
fopen()
and related functions) that only gets appended to. If you have multiple pages, then a simpleid (INTEGER), filetext (LARGEBLOB)
. (Notelargeblob
has a limit of 2^32 bytes).For the user's browser part, you'll need to use Javascript and AJAX to inform the server of any updates. Just get in touch with a PHP script that (1) accepts the input and (2) appends it to a file.
Also use the AJAX call to fetch new content (e.g. if you assign line numbers, then the browser just tells the script the last line it read, and the script returns all new lines past that point).
Pretty much. But only send the last 50 lines or so of the file when the browser visits it. You don't want to crash the browser.
Think in terms of bytes, not words, and you'll likely run into performance issues. You could cap file sizes or split up the storage into multiple files at a certain size so you don't have to scan pass content that will rarely be fetched.