如何验证潜在的数据库名称?

发布于 2024-10-02 03:36:01 字数 115 浏览 0 评论 0原文

我想提示用户输入数据库名称并警告他如果该名称无效。

有没有比实际尝试创建数据库并检查结果更简单的方法?

没什么大不了的,我只是好奇。 PHP 和 MySql,但我需要它兼容 ODBC。

I want to prompt the user to enter a database name and warn him if it is invalid.

Is there an easier way than actually trying to create the database and checking the result?

No biggy, I'm just curious. PHP and MySql, but I need it to be ODBC compliant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2024-10-09 03:36:01

您应该让 PHP 脚本在提交之前验证名称,并让它捕获在尝试创建名称时从 MySQL 返回的任何错误。

例如,PHP 可以根据正则表达式验证名称。也许您只想允许字母字符。

事实上,我强烈建议接受字母字符,并在向数据库命令发送任何内容之前验证这一点...没有任何意义让任何人有机会提交可能最终导致 SQL 注入的值,在具有创建数据库能力的用户的上下文。

但我也想问;您真的需要允许用户定义自己的数据库名称吗?启用它可能看起来“很好”,但是与为用户生成半随机或递增名称相比,在这种特殊情况下无需验证他们的输入,有什么真正的好处吗?

You should have your PHP script validate the name before submitting it, and have it catch any error that comes back from MySQL when trying to create it.

The PHP could validate the name against a regular expression, for example. Maybe you want to only permit alphabetical characters.

In fact, I strongly recommend only accepting alphabetical characters, and validating this before ever sending anything to a database command... no sense giving anyone any chance to submit values that could end up causing SQL injection, in the context of a user with the ability to create databases.

But I would also ask; Do you really need to allow users to define their own database names? It might seem 'nice' to enable it, but is there any real benefit over generating a semi-random or incrementing name to users, eliminating any need to validate their input in this particular case?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文