user830593
user830593

Reputation: 221

Run PHP Function once

Is there a way to run a PHP FUNCTION "fun();" only once when a logged in user accesses a page multiple times?

here is the function.

<?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();
    }
}

?>

Upvotes: 2

Views: 6257

Answers (3)

Martin Bean
Martin Bean

Reputation: 39389

Can you not run the function, then store a $_SESSION variable? For example:

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

Upvotes: 1

wanovak
wanovak

Reputation: 6127

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

Upvotes: 11

Steve Nguyen
Steve Nguyen

Reputation: 5974

Using authentication/sessions and storing the number of times a user has accessed that page in say a database for example or a flat file. You'll need to persist their access count to that page regardless.

Upvotes: 0

Related Questions