使用唯一密码在Laravel中密码保护页面[关闭]

使用唯一密码在Laravel中密码保护页面[关闭]

问题描述:

I need to set up password protected pages within laravel (using 5.4), each page entry in my database has a password field.

I want to be able to only access the page if the password is correctly entered...

I think this makes sense and I can't seem to find anything else on this other than using the built in auth functionality of laravel...

Thanks in advance.

我需要在laravel中设置受密码保护的页面(使用5.4),我数据库中的每个页面条目都有一个密码 字段。 p>

我希望只有正确输入密码才能访问该页面... p>

我认为这是有道理的,我 除了使用laravel的内置auth功能之外,似乎无法找到任何其他内容... p>

提前致谢。 p> div>

This is a pretty simple thing to do..

Create a form with a password field and a hidden id field where you store the id of the page you want to access.

When user submits the password, check if it matches the one in your database..

if (Hash::check('enetered-password', $hashedPassword)) {
    // The passwords match...
}

If so, store the page id in an array in the session. Next time the user wants to visit the page, you check if the id exists in this array, if so redirect to the page, otherwise redirect to the password form page.

It would be even wiser to add a middleware to the protected pages, to check if the user has permission or not and also redirect.

Once the session expires (i.e. use logs out) all data will be destroyed, so if they login again, they need to enter the page passwords again.