在php中只运行一次代码

时间:2012-03-13 10:25:16

标签: php

我只需运行一次程序;之后它将运行另一个程序。这怎么可能?

if()
{
include 'first.php';
}
else
{
include 'second.php';
}

我不想把条件放在if条件下。

请有人帮忙吗?

4 个答案:

答案 0 :(得分:2)

在$ _SESSION中存储一个标记你的first.php已经运行一次的标志,如下所示:

if(!isset($_SESSION['first_run'])){
    $_SESSION['first_run'] = 1;
    include 'first.php';
}

include 'second.php';

答案 1 :(得分:0)

使用锁定文件。

$lockfile = '/some/writable/path/executed.lock';
if (file_exists($lockfile)) {
    include('second.php');
} else {
    file_put_contents($lockfile, '');
    include('first.php');
}

答案 2 :(得分:0)

让 PHP 通过 require_once 和/或 include_once 为您处理

内部first.php

your 
.
.
.
code 
.
.
.
here

define('FIRST_RUN', false);

在您的脚本中:

define('FIRST_RUN', true);
include_once 'first.php';

if( !FIRST_RUN )
    include 'second.php'

答案 3 :(得分:-2)

你做了什么?

if(!file_exists('/tmp/foo')) { 
    touch('/tmp/foo');
    include 'first.php'; 
} else { 
    include 'second.php'; 
}
相关问题