1

I have been looking for a way to run one piece of code only once in php. I want to create and set a variable at the beginning of the webpage and whenever I call that file, the code will ignore that line.

I found this in which there are basically two suggestions: setting a session variable and creating a lock file which I don't want to since there might be many users at the same time and I need to have a lock file for each one of them.

I tried the first option but it doesn't work.

Here are my two files:

config.php

<?php 
     if (at the beginning){
         $variable = something
         echo 'hello';
     }
     else
          do something
?>

second.php

<?php
     require_once 'config.php'    
     //do some other stuff
?>

Whenever second.php is called, it prints 'hello' in the webpage, so if (at the beginning) returns true even though it is not at the beginning. Note that, I check whether at the beginning by if(!isset($_SESSION['done'])){ $_SESSION['done'] = 'done'; }

I wonder what is my mistake and how can I solve the problem. Any help is appreciated.

Community
  • 1
  • 1
smttsp
  • 4,011
  • 3
  • 33
  • 62
  • do you want it to run once per request, once per session, once per year? – Roland Starke Mar 11 '16 at 16:55
  • @RolandStarke: Whenever the webpage is requested, i.e. for each user opening the webpage. – smttsp Mar 11 '16 at 16:58
  • So the missing negation was just a typo? Please comment if you make changes that affect program's behaviour. – syck Mar 11 '16 at 16:59
  • @syck: Yes, I directly copy and pasted from `http://stackoverflow.com/a/9682456/2021883`, s/he made a mistake, I warned him/her as well. But it was already negated in my code. – smttsp Mar 11 '16 at 17:02
  • 1
    @smttsp if you only include the file with `require_once` than it should not be executed multiple times per request – Roland Starke Mar 11 '16 at 17:07
  • @RolandStarke: With this code, for each user I will create new session right? – smttsp Mar 11 '16 at 17:14

1 Answers1

2

The program flow for config.php should be:

<?php 
     session_start();

     if (!isset($_SESSION['done'])) {
         $_SESSION['done'] = 1;
         echo 'hello';
     }
     else {
         // do something
     }
?>
syck
  • 2,984
  • 1
  • 13
  • 23
  • Will the `session_start()` initiate the session only once or whenever the file is called? I will call `config.php` from multiple other files. – smttsp Mar 11 '16 at 17:10
  • @RolandStarke: yes, I always `require_once`. Thanks a lot, it works now – smttsp Mar 11 '16 at 17:15
  • `session_start()` should be called once per request. But since you use `require_once()`, config.php should not be executed more than a single time. – syck Mar 11 '16 at 17:17
  • I don't know why but it executes multiple times. `if (!isset(....) echo 'first'); else{echo 'again???';}`. The first time it prints `first`, then it always prints `again???`. Also, I'm sure I'm always calling with `require_once`. – smttsp Mar 11 '16 at 17:21
  • Within a single script run? You can use [debug-backtrace()](http://php.net/manual/en/function.debug-backtrace.php) from inside config.php to find out which function originate the require statements from. – syck Mar 11 '16 at 17:38