如何更改每个页面上的会话变量0和1刷新

时间:2017-04-27 09:33:56

标签: php wordpress session ads

我有一个代码,我想用wordpress网站运行,这在简单和常规的网站上运行良好,没有缓存但是后续的PHP代码无法在缓存网站,wp-rocket插件或自动优化的插件安装,不幸的是我无法删除这些插件,请帮助我,我也不能使用javascript ...

<?php 
if(!isset($_SESSION)) 
    { 
session_start();
    }
   ?>


<?php
if(isset($_SESSION['test']))
{
 if($_SESSION['test'] == 0)
 {
  $_SESSION['test'] = 1;
    ?>
 <!----place your Second ads here--->




    <!----Second ads end here-->
 <?php
 }
    else
 {
  $_SESSION['test'] = 0;
  ?>
 <!----place your First ads here--->




    <!----First ads end here-->
  <?php
 }
}
else
{
 ?>
<!----place your first ads here  this is default ads-->

 <!----First ads end here-->
<?php
$_SESSION['test'] = 0;
}
?> 

正如您所看到的,我想在每次刷新页面时交换广告。

3 个答案:

答案 0 :(得分:0)

试试吧

    <?php
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    ?>

答案 1 :(得分:0)

使用这个

<?php 
    if(empty($_SESSION)) 
    { 
        session_start();
    }
?>
instead of
<?php 
    if(!isset($_SESSION)) 
    { 
        session_start();
    }
?>`

答案 2 :(得分:0)

<?php
session_start();
//set a variable to prevent 2 if statements from executing
$x='0';
//for the first time the page is loaded
    if(!isset($_SESSION['test'])) {
        $_SESSION['test'] = '0'
        }

    if(($_SESSION['test'] == '1' && $x=='0'){
        //place second ads here
        $_SESSION['test'] = '0';
        $x='1';
        }
    if(($_SESSION['test'] == '0') && $x=='0'){
        //place first ads here
        $_SESSION['test'] = '1';
        $x='1';
        }
?>

并在标题中添加

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">