这是有关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”,以便将其锁定。
我正在尝试编写如下代码:
答案 0 :(得分:1)
您可以将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并基于此阻止或允许下载,但是那样您将无法使用该字段计算下载次数...
答案 1 :(得分:1)
我已为您编写代码。我了解您是否还有其他条件。
$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;
}
答案 2 :(得分:1)
如果您查看PHP logical operators documentation,将有一张表显示PHP中的各种逻辑运算符,其中一个是 && 运算符。该运算符的结果是:
如果$ a和$ b都为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 ,但是如果数据库中具有相似的字段,则可以编写类似于上面的条件。
答案 3 :(得分:1)
由于只有少数几个条件,因此我会立即测试所有条件:
$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();