限制用户每周执行一次以上的代码(非自动)

时间:2015-11-13 09:56:21

标签: php

我希望我的用户每周只能访问一次脚本。 请注意,我不希望代码每周执行一次'自动'。

我希望用户能够每周只执行一次代码(同步)。在范围内,我限制用户访问的脚本是扫描。谢谢... 我试过了,但它没有用。

if (isset( $_POST['action'])) {
    switch($_POST['action']){

    $q = mysql_query("SELECT dateStop from `run` where userid = $userid");
    $row = mysql_fetch_assoc($q) or die(mysql_error());
    $timeDiff = time() - $row['dateStop'];
    $timeDay = $timeDiff / (24 * 60 * 60);
    if ($timeDiff <= 604800) {
        echo "<script> alert('You can only scan once in a week. Try again in the next $timeDay day(s)'); </script>";

        case "is_running": {
            $res = $db->query("SELECT 1 FROM `" . SQL_PREFIX . "run` WHERE userid = $userid and ISNULL(dateStop)");
            die(json_encode(array("running" => (mysql_num_rows($res) > 0))));
        } else {.....}  

2 个答案:

答案 0 :(得分:0)

我写了一个快速的脚本,它会做你想要编辑的东西,所以它适合你。阅读他们解释正在做什么的评论。任何问题评论并将检查

/**
**YOU can use this function to get hold of the date a week from your current date.
**It does generally process most date formats eg. 10-12-2015, jan 28, 2015, timestamp...
**/

function processDate($val, $days)
{
    $value = $val;
    $dayToAdd = $days;

    switch($value)
    {                                   
    case is_numeric($value):
        // time()
        $value = strtotime('+'.$dayToAdd.' days', $value);
        $value = date('d - m - Y', $value);
        break;
    default: 
        // days + Unix
        $value =  strtotime(str_replace(',', '', $value));
        $value = strtotime('+'.$dayToAdd.' days',$value);
        $value = date('d - m - Y', $value);
        break;
    }

    return $value;
}



/**
**Presuming this checks if a button is clicked?
**/

if(isset($_POST['action']))
{
    /**
    **Im presuming this query gets hold of the last date when the script was run.
    **I DID NOT TEST THIS SCRIPT WITH A DB i testing it by setting $row['dateStop'] = time();
    **You will have to test with db.
    **/

    //delete $row['dateStop'] = time() AND UNCOMMENT YOUR QUERY TO TEST IT.
    $row['dateStop'] = time();


    /**
    **UNCOMMENT YOUR QUERY AND TEST SCRIPT USING IT.
    **/

    //$q = mysql_query("SELECT dateStop from `run` where userid = $userid LIMIT 1");    
    //$row = mysql_fetch_assoc($q) or die(mysql_error());




    /**LastDate = last date script was run, NewDate = a week after last date, timeDiff = how many days diffrence
    **You can edit the function to get the format to be whatever you need. So if you want it to be like unix
    **timestamp you will have to change date('d - m - Y', $value); INTO a correct format use http://php.net/manual/en/function.date.php
    **/

    $LastDate = processDate($row['dateStop'], 0);
    $NewDate = processDate($row['dateStop'], 7);
    $timeDiff =  $NewDate-$LastDate;

    /**
    **Correct way to use a switch case to check if either the date is within a week or if it has been a week.
    **/

    switch ($timeDiff) 
    {
    case $timeDiff == 7:
        //code to be executed if it has been a week;
        echo "it has been 7 days".$timeDiff;
        break;
    case $timeDiff > 7:
        //code to be executed if it has been over a week;
        echo "it has been over 7 days".$timeDiff;
        break;
    case $timeDiff<7:
        //code to be executed if it has been less than a week;
        echo "it has been less than 7 days".$timeDiff;
        break;
    default:
        //A default script to run incase none of the cases match;
    }
}

答案 1 :(得分:0)

只需使用 SQL

用户ID 提供给以下SQL,如果允许用户进行扫描,则会让您知道

SELECT IF(DATEDIFF(CURRENT_TIMESTAMP, dateStop) >= 7, 'yes', 'no') AS allowedToScan
FROM `run`
WHERE userid = 2

SQL小提琴:http://sqlfiddle.com/#!9/b32db6/10