php如果在课堂外调用功能

发布于 2025-01-20 09:00:07 字数 520 浏览 2 评论 0原文

我正在为网络平台制作SDK,有一个客户端类需要cookie来授权,并且有一个从cookie获取身份验证令牌的函数。所以我的问题是:如何检查函数是否在类外被调用。我需要这个,因为我想用密码保护这个函数,并且如果类调用它,它就可以在没有密码的情况下工作。 这是我的代码:

public function gettoken(?string password = ""): string{
    //check if it's called inside of class
    if (fromClass() == true){
       //code that gets token
    }
    //if it's called outside of class
    if ($password == $this->password){
       //code that gets token
    }
    return "Incorrect password";
}

I'm making SDK for a web platform, there is a client class which requires cookie to authorize and there is a function that gets auth token from cookie. So my question is: how to check if function was called outside of class. I need this because i want to protect this function with password and make it so if class called it, it would work without a password.
Here is my code:

public function gettoken(?string password = ""): string{
    //check if it's called inside of class
    if (fromClass() == true){
       //code that gets token
    }
    //if it's called outside of class
    if ($password == $this->password){
       //code that gets token
    }
    return "Incorrect password";
}

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

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

发布评论

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

评论(1

莫言歌 2025-01-27 09:00:07

这听起来很像一个坏主意。

为什么不创建两个函数:一个需要密码的公共函数,一个不需要密码的私有函数。当然,公共函数可以在密码验证后调用私有函数。

像这样的事情:

public function getTokenUsingPassword($password)
{
    if ($password == $this->password) {
        return $this->getToken();
    }
    return false;
}

private function getToken()
{
    return //code that gets token
}

This sound very much like a bad idea.

Why not make two functions: One public that requires a password and one private that doesn't. The public function can, of course, call the private function after the password has been verified.

Something like this:

public function getTokenUsingPassword($password)
{
    if ($password == $this->password) {
        return $this->getToken();
    }
    return false;
}

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