2017-04-27 53 views
0

我有一個代碼,我想運行一個WordPress的網站,這是工作正常的簡單和普通的網站沒有緩存但後面的PHP代碼不工作緩存的網頁,WP-火箭插件或自動優化插件安裝的,可惜我不能刪除那些插件,請幫我我也不能使用JavaScript ...如何更改每個頁面上的會話變量0和1刷新

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

正如你可以看到,我想要在每次刷新頁面時交換廣告。

+0

'的session_start();'應始終在最前面,但會話存儲在服務器端,所以它與我認爲的緩存無關。 –

+0

您必須在緩存系統中制定一個例外。看到緩存引擎完全緩存提供的HTML,廣告將被保存到這些文件中。你使用的是什麼緩存插件? – Thoby

+0

感謝您的回覆..我在我的WordPress站點使用wp-rocket或自動優化插件。 –

回答

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"); 
    ?> 
0

使用這一個

<?php 
    if(empty($_SESSION)) 
    { 
     session_start(); 
    } 
?> 
instead of 
<?php 
    if(!isset($_SESSION)) 
    { 
     session_start(); 
    } 
?>` 
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">