这是关于PHP的基本问题。
我的问题是:我想设置不同类型的用户。例如,对于普通用户,他们只能下载6个文件,对于VIP用户,他们只能下载15个文件。
这是我的代码:
$dl_user = gator::getUser($dl_username);
if ($dl_user['downloads'] > 99){
echo "<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>";
die;
}
$dl_user = gator::getUser($dl_username);
if ($dl_user['downloads'] > 6){
echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
die;
}
默认情况下,“没有帐户”的访客用户将无法下载任何内容,我给他们变量“100”,因此它将锁定它。
我正在尝试制作这样的代码:
您可以将VIP列设置为不同的值以检查用户是否为VIP,例如0 =来宾,1 =成员,2 = VIP ...
$dl_user = gator::getUser($dl_username);
if ($dl_user['vip'] == 0){
// BLOCK ANY download.
} else if($dl_user['vip'] == 1) {
// User is a member (allow 6 downloads)
if($dl_user['downloads'] > 6) {
//BLOCK the download
} else {
//ALLOW download and COUNT
$dl_user['downloads'] = $dl_user['downloads'] + 1;
}
} else if($dl_user['vip'] == 2) {
// User is a VIP (allow 15 downloads)
if($dl_user['downloads'] > 15) {
//BLOCK the download
} else {
//ALLOW download and COUNT
$dl_user['downloads'] = $dl_user['downloads'] + 1;
}
} else {
// User has unknown status (not logged or some error)
}
如果您需要使用字段下载来保持数据结构以检测用户类型,我建议只验证其是否为6,15或100,并根据该情况阻止或允许下载,但这样您将无法计算下载量使用该领域......
我已经为你编写了代码。我想你了解if else条件。
$dl_user = gator::getUser($dl_username);
if($gustuser){
echo "<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>";
die;
}elseif($normaluser && ($dl_user['downloads'] >6 )){
echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
die;
}elseif($vipuser && ($dl_user['downloads'] >15 )){
echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
die;
}
如果你看看PHP logical operators documentation,有一个表格显示PHP中的各种逻辑运算符,其中一个是&&运算符。该运算符的结果是:
如果$ a和$ b都为TRUE,则为TRUE。
考虑到这一点,如果您需要在if语句中检查多个条件,可以使用&&来完成此操作。针对您的问题的可行解决方案可能是:
$dl_user = gator::getUser($dl_username);
if ($dl_user['downloads'] > 99){
echo "<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>";
die;
}
if ($dl_user['downloads'] > 6){
echo "<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>";
die;
}
if ($dl_user['downloads'] > 6 && $dl_user['is_vip'] && $dl_user['vip_col'] === 1 {
// Code that restricts user to 15 files
}
显然在这个例子中组成了关键字is_vip和vip_col,但是如果数据库中有类似的字段,你可以写一个类似于上面的条件。
我会立刻测试我的所有条件,因为只有少数:
$echo =
// is the user have more than 15 downloads OR have more than 5 but not vip?
$dl_user['downloads'] > 14 || ($dl_user['downloads'] > 5 && $dl_user['vip'] < 1) ?
//we kick him out
"<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>" :
// does he have 100 downloads (guest)
$dl_user['downloads'] > 99 ?
// get subscribed
"<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>" :
//is none of the above? He can downloads
false;
// we echo the result if there is one, or we leave;
echo $echo ? $echo : die();
如果VIP可以不止一个,那么你知道他可以下载多少,我会像之前那样计算它
//assuming each vip gives you the right to get 10 more download, I multiply vip by 10
$permited_download = $dl_user[vip] > 0 ? intval($dl_user[vip])*10 : '5';
//then I do the same as the previous exemple but with my calculated variable
$echo =
// is the user have more than permited downloads (if he's a guest, it's 5) ?
$dl_user['downloads'] > ($permited_download - 1) ?
//we kick him out
"<script type='text/javascript'>location.href='https://www.example.com/buynow/'</script>" :
// does he have 100 downloads (guest)
$dl_user['downloads'] > 99 ?
// get subscribed
"<script type='text/javascript'>location.href='https://www.example.com/?signup=1&'</script>" :
//is none of the above? He can downloads
false;
// we echo the result if there is one, or we leave;
echo $echo ? $echo : die();