运行PHP函数一次

时间:2011-07-15 15:27:58

标签: php

有没有办法运行PHP FUNCTION“fun();”登录用户多次访问页面时只有一次?

这是功能。

<?php

$connect = //connect to database

function fun()
{

    //loop declare begin
    $quer = "SELECT * FROM sffedorvsdan WHERE username = '$_SESSION[username]'";

    if($quer_run = mysql_query($quer))
    {
        while($row = mysql_fetch_assoc($quer_run))
        {
            $pick1 = $row['pick1'];
            $pick2 = $row['pick2'];
            $pick3 = $row['pick3'];
            $pick4 = $row['pick4'];
            $pick5 = $row['pick5'];
            $pick6 = $row['pick6']; 

            //loop end EXCEPT CLOSE 

            $user = $_SESSION['username'];

            if($pick1 == '11')
            {

                $score = mysql_query("
                    UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10), 
                    wins = (wins + 1), games = (games + 1)
                    WHERE username = '$user'
                ");

                return $score;
            } 
            else if($pick1 == '21')
            {
                $score2 = mysql_query("
                    UPDATE stats SET score = (score - 5), mmascore = (mmascore - 5), 
                    games = (games + 1)
                    WHERE username = '$user'");

                return $score2;
            }
        }
    }else{
        echo mysql_error();
    }
}

?>

3 个答案:

答案 0 :(得分:9)

if(!isset($_SESSION['visitedOnce'])){
    fun();
    $_SESSION['visitedOnce'] = true;
}

答案 1 :(得分:1)

你能不运行该功能,然后存储$_SESSION变量?例如:

if (!isset($_SESSION['function_ran']) || $_SESSION['function_ran'] != '1') {
    fun(); // run function
    $_SESSION['function_ran'] = '1';
}

答案 2 :(得分:0)

使用身份验证/会话并存储用户访问该页面的次数,例如数据库或平面文件。无论如何,您都需要将访问次数保持在该页面上。