使用 PHP 从外部服务器验证 MySQL 数据库中的信息
我不太熟悉 PHP 的 mysql_connect 的工作原理,但可以编写非常基本的脚本。我加入了一个论坛管理员论坛,一家托管公司向我们提供了合作伙伴关系。
这种合作关系涉及我们的活跃会员通过该服务托管软件包获得折扣。托管所有者建议他为这个特定的包设置一个“隐藏 URL”,但我认为如果非我们论坛的成员发现了该 URL,他们可能会滥用该 URL。
因此,我建议我们安装一个脚本,该脚本接受用户在托管网站上输入的凭据(用户名和密码),并将该信息与我们的数据库(显然在单独的服务器上)交叉引用以查找匹配项检查用户是否已激活并且至少有 100 个帖子,并且只有当所有 if 语句都返回 true 时,才会对用户进行身份验证并授予托管包的访问权限。
我知道如何验证数据,但我不知道如何访问服务器外部的数据库,我做了一些谷歌搜索,我发现的信息除了让我困惑之外什么也没做。
I'm not extremely familiar with how PHP's mysql_connect works, but can program very basic scripts. I joined up at a forum for forum administrators and we have been offered a partnership by a hosting company.
This partnership involves our active members receiving discounts for hosting packages from that service. The hosting owner suggested that he would have a "hidden URL" for this particular package, but I felt that this could be abused by non-members of our forum if they found out about the URL.
So I suggested that we instead install a script that takes credentials input by the user (username and password) on the hosting website, and cross-references that information with our database (obviously on a separate server) to look for a match, as well as check if the user is activated and has at least 100 posts, and only if all the if statements return true, the user is then authenticated and granted access permission for the hosting package.
I know how to validate the data, but I do not know how to access a database external to the server, and I did a few Google searches and the information I found did nothing but confuse me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 可以以与连接本地主机服务器相同的方式连接到外部服务器:
外部服务器必须允许来自发出请求的 Web 服务器通过其防火墙的 MySQL 连接,并且它必须有一个用户
webserveruser@webserver-主机名
可以访问需要查询的数据库。在外部服务器上,假设您只需要对表进行读取 (
SELECT
) 访问:MySQL can connect to an external server in the same manner as it connects to the localhost server:
The external server must allow MySQL connections through its firewall from the web server making the requests, and it must have a user
webserveruser@webserver-hostname
with access to the database that needs to be queried.On the external server, assuming you need only read (
SELECT
) access to a table:对于托管公司来说,似乎最简单的解决方案是单独通过电子邮件向每个人发送一个长而独特的 URL,例如
单击此链接将带您进入他们的注册/购买页面,您将在其中获得特别折扣,然后在购买后一旦创建,该链接就会失效或无效,因此其他人无法使用它。有人随机猜出 URL 的可能性极小。
这比尝试要求系统管理员直接访问他们的数据库要容易得多。唯一的其他选择是数据库/论坛系统管理员编写一个 PHP API 脚本,托管服务可以安全地使用该脚本来检查数据库的凭据,而数据库系统管理员无需透露有关数据库的重要安全详细信息。
It seems like the easiest solution would be for the hosting company to individually email each one of you a long, unique URL like
Clicking this link would take you to their signup/purchase page where you would receive a special discount and then after the purchase is made, the link becomes dead or invalid so no one else could use it. It is extremely unlikely that someone could guess the url randomly.
That would be a lot easier than trying to ask sysadmins for direct access to their databases. The only other alternative would be for the database/forum sysadmin to write a PHP API script that the hosting service can safely use to check the database for credentials without the database sysadmin needing to reveal important security details about the database.
您应该能够在主机之间远程访问任何 mysql 数据库。我个人知道,我要求他们提供远程连接,他们向我询问他们允许访问的具体 IP 地址。
我还整理了一些代码,以防您对如何实现所需的用户表单/验证感到困惑 - 正如您所说,您的 PHP 知识有限。
假设您有一个与此类似的登录表单
下面是 checklogin.php 的一些 PHP 代码,我将它们与 // 注释放在一起以指导您:
上面的代码未经测试,几乎不包含任何安全机制,也没有验证。
但它会满足您建议的保护表单的标准。
You should be able to access any mysql database remotely from host to host. I know personally with mine I asked them to make remote connecting available, and they asked me for the specific IP address that they would allow to access it.
I've knocked together a bit of code also in case you're confused about how you would go about achieving the desired form/validation of user that you're after - as you stated that you have limited PHP knowledge.
Presuming that you have a login form similar to this one
Here's some PHP code for checklogin.php that I've thrown together with //comments to guide you:
The code above is untested, and contains hardly any safety mechanisms and no validation.
But it would fulfil the criteria that you have suggested to protect your form.