简体   繁体   English

允许用户仅在获得批准后才能访问网站的一部分

[英]Let users access to part of website only if they're approved

I'm working with laravel 5.4 and I have part of my website that give download links but I need to give this download links only to users who paid for that links. 我正在使用laravel 5.4,并且网站的一部分提供下载链接,但我只需要将此下载链接提供给为该链接付费的用户。

Example: I have courses which users can buy to learn. 示例:我有一些可供用户购买以学习的课程。 Each course has different link of downloads. 每个课程都有不同的下载链接。 here i want to show download link of exact course that user has bought to him/her and not all the courses for like 30 days. 在这里,我想显示用户已购买给他/她的确切课程的下载链接,而不是30天之内的所有课程。

How can I do that? 我怎样才能做到这一点?

PS: I only need guide about database structures to how to relate specific user to specific course for limited time only. PS:我只需要有关数据库结构的指南,以了解如何仅在有限的时间内将特定用户与特定课程相关联。

Thanks. 谢谢。

I'm not really that experienced with Laravel, but it's very easy to do in PHP. 我对Laravel并不是很了解,但是在PHP中很容易做到。 One thing you can do is store information in a session like this: 您可以做的一件事是将信息存储在这样的会话中:

$_SESSION['premium'] = true;

So then you can check to see if the session is true like this: 因此,您可以像这样检查会话是否正确:

if ($_SESSION['premium'] = true) {
    # Paid content
} else {
    echo "Sorry! This is for premium members only!";
}

You also have the options of querying the database to see if the user have a column called 'paid' set to 1(true) or 0(false). 您还可以选择查询数据库,以查看用户是否将名为“付费”的列设置为1(true)或0(false)。 Which I'm assuming you understand how to do. 我假设您知道该怎么做。

In response to your recent comment: 针对您最近的评论:

Create a table called purchases with these columns: 使用以下列创建一个名为purchases表:

  • id ID
  • user_id 用户身份
  • course_id COURSE_ID

Then on the page, query that table: 然后在页面上查询该表:

$query = mysqli_query($connection_variable, "SELECT * FROM purchases WHERE user_id='$_SESSION['username'] AND course_id='$course_id'");
if (mysqli_num_rows($query) > 0) {
    #Show course
} else {
    echo "ERROR MESSAGE HERE";
}

Try that and tell me how it goes! 尝试一下,告诉我如何!

Assuming your users login first. 假设您的用户先登录。

Create a model for Purchases with a user_id , course_id and timestamps and set up a one to many relationship. 使用user_idcourse_id和timestamps为Purchases创建模型,并建立一对多关系。

In your logic when someone purchases a course you add a record into your Purchases table above. 根据您的逻辑,当有人购买课程时,您会在上面的“购买”表中添加一条记录。 Supposing you have a listing of these Purchases, you would grab these purchases by 假设您有这些购买的清单,则可以通过以下方式获取这些购买

User::find(1)->purchases

and this would return the purchases they are allowed to download. 这将返回允许下载的购买内容。 You would need additional validation using scopes maybe to effect your 30 day availability rule eg download is only available if the date today is within 30 days from the created_at column. 您可能需要使用范围进行其他验证,这可能会影响您30天的可用性规则,例如,仅当今天的日期在created_at列的30天之内时才可以下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM