2013-02-09 33 views
0

好吧,我會盡量保持這種簡單和甜蜜。我正在使用我的wesbsite,並且當用戶第一次加載站點時,它當前加載了一個啓動頁面。這是我想要的行爲。使用PHP和Javascript重定向並設置Cookie

  1. 如果用戶以前從未去過網站,則重定向到splash頁面。
  2. 如果用戶以前曾經訪問過該網站,並且不想看到這個splash不重定向。
  3. 如果用戶喜歡飛濺並希望在新會話中看到,請顯示啓動頁面。

那些基本上與我一起工作的場景,不能再真正想到,在過去的幾個小時裏一直在試圖破解一些東西,沒有運氣。

的index.php

<?php 
setcookie("visit", "true", mktime (0, 0, 0, 12, 31, 2014), "/"); // delete cookie on 31DEC14 

$cookie_splash = $_COOKIE['splash']; 
$cookie_visit = $_COOKIE['visit']; 
$cookie_visit_now = $_COOKIE['visit_now']; 

do { 

    if ($cookie_splash == '' && $cookie_visit == '' && $cookie_visit_now == '') { 
     /* 
     echo "<script type = text/javascript>"; 
     echo "window.location = 'http://chrisrjones.com/splash.php'"; 
     echo "</script>"; 
     */ 
     header('Location: splash.php'); 
    } 

    if ($cookie_splash == 'false' && $cookie_visit_now == 'true') { 
    break; 
    } 

    if ($cookie_splash == 'true' && $cookie_visit_now == 'false') { 
     /* 
     echo "<script type = text/javascript>"; 
     echo "window.location = 'http://chrisrjones.com/splash.php'"; 
     echo "</script>"; 
     */ 
     header('Location: splash.php'); 
    } 

    if ($cookie_splash == 'true' && $cookie_visit == 'true' && $cookie_visit_now == "false") { 
     /* 
     echo "<script type = text/javascript>"; 
     echo "window.location = 'http://chrisrjones.com/splash.php'"; 
     echo "</script>"; 
     */ 
     header('Location: splash.php'); 
    } 

    if ($cookie_splash == 'true' && $cookie_visit == 'true' && $cookie_visit_now == 'true') { 
     break; 
    } 
} 
while (0); 




?> 

splash.php

<p> 
    <form name="tosplashornottosplash" action="scripts/splash-process.php" method="post" onSubmit="return valForm()"> 
    Splash pages are stupid. 

    <input type="radio" name="splash" id="splash_false" value="false" /> No 
    <input type="radio" name="splash" id="splash_true" value="true" /> Yes 

    <input type="submit" name="splashSubmit" onClick="return valForm(tosplashornottosplash)" value="Enter" /> 
    </form> 
    </p> 

防濺process.php

<?php 

    setcookie("visit", "true", mktime (0, 0, 0, 12, 31, 2014), "/"); // delete cookie on 31DEC14 
    setcookie("visit_now", "true", NULL, '/'); // cookie should expire/delete at end of session. 


    $splashvar = $_POST["splash"]; 

    if ($splashvar == "false") { 
     // create cookie - splash 1 
     setcookie("splash", "true", time()+3600, '/'); // expires in one hour 
    } 
     else { 
      // create cookie - splash 0 
      setcookie("splash", "false", time()+3600, '/'); // expires in one hour 
     } 

    echo "<script type = text/javascript>"; 
    echo "window.location = 'http://chrisrjones.com/index.php'"; 
    echo "</script>"; 

    ?> 

轉到chrisr jones.com看看我在說什麼。

+0

怎麼回事?需要修改哪些內容?請多一點信息。 – 2013-02-09 02:37:57

回答

0

好了,所以我覺得這個問題是不是我的代碼,而是瞭解瀏覽器的行爲。如果我現在在FF 18.0.2中訪問網站(chrisrjones.com),並在啓動頁面上選擇「否」,那麼我可以在點擊輸入後進入網站。

但是,如果我將該選項卡打開,並且設置了FF以在重新啓動時將標籤打開(無論它被稱爲您知道我的意思),並退出FF,然後重新啓動會話cookie永不過期。所以,當我刷新頁面,因此沒有加載啓動頁面。但是,如果我關閉該選項卡,然後退出瀏覽器然後重新加載頁面,則加載啓動頁面。 (這是我想要的)。

另一方面,Chrome在瀏覽器關閉後似乎不會刪除會話cookie,所以這就是爲什麼我在Chrome上遇到這種困難。

我現在已經有了一個更好的理解,爲什麼事情的行爲方式是這樣的,並且意識到這不是我所有的糟糕的編碼習慣。

0

嘗試以下操作:

<?php 
if (($_GET['nosplash'] == '1') || ($_COOKIE['nosplash'] == '1')) { 
    setcookie('nosplash','1',strtotime('+1 year'),'/'); 
    header('Location: http://'.$_SERVER['HTTP_HOST'].'/index2.html'); 
    exit; 
} 

?><html> 
<head> 
<title>Splash screen</title> 
</head> 
<p> 
Splash screen 
</p> 
<p> 
<a href="/index2.html">continue (and show splash screen on next visit)</a><br /> 
<a href="<?=$_SERVER['SCRIPT_NAME']?>?nosplash=1">continue and don't show the splash again</a> 
</p> 
</html> 
+0

我想將這個PHP代碼添加到splash.php頁面? – Chris 2013-02-09 02:59:20

+0

保存爲'test.php',看看它是否做到了你想要的。您可能需要編輯硬編碼的'/ index2.html'才能進入第二頁。 – Tigger 2013-02-09 03:41:16