2010-08-19 43 views
2

我想在用戶同意某事之前保護網站不被訪問。其基本思想是,在每個頁面的頂部,它將檢查cookie是否存在,如果不存在,則退出幷包括包含消息的PHP頁面和兩個按鈕,其中一個將創建cookie,另一個按鈕將其簡單地從站點移出,例如, google.comPHP創建cookie並僅在cookie存在時才顯示網站

編輯:

這是我結束了:

警告包括會是這個樣子:

<?php 

function pageURL() { 
    $pageURL = 'http'; 
    if ($_SERVER["HTTPS"] == "on") { 
     $pageURL .= "s"; 
    } 
    $pageURL .= "://"; 
    if ($_SERVER["SERVER_PORT"] != "80") { 
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
    } 
    else { 
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 
    } 
return $pageURL; 
} 

$pageRedirect = pageURL(); 

    if (
     isset($_POST['agree_button']) && ($_POST['agree_button'] == 'I agree') 
    ) { 
     setcookie('agreed', 'true'); 
     header("Location:$pageRedirect",303); 
    } 
?> 


<form action="<?php echo pageURL(); ?>" method="post"> 
    <p>INSERT MESSAGE HERE (User must agree)</p> 
    <input type="submit" value="I agree" name="agree_button" /> 
    <input type="button" value="I disagree" /> 
</form> 

和在頁面像頂部這個:

<?php 

    if(!isset($_COOKIE['agreed']) || ($_COOKIE['agreed'] != 'true')) 
    { 
     include('warning.php'); exit; 
    } 

?> 

回答

3

我會做客戶端...

<script src="js/jquery.cookie.js" type="text/javascript"></script> 
<form> 
    <p>INSERT MESSAGE HERE (User must agree)</p> 
    <input type="submit" value="I agree" onclick="$.cookie('agreed', 'true'); location.href='/'" /> 
    <input type="button" value="I disagree" /> 
</form> 

和檢查將是...

if (
    !isset($_COOKIE['agreed'])|| 
    ($_COOKIE['agreed'] != 'true') 
) { 
    include('warning.php'); 
    exit; 
} 
如果你想設置服務器端cookie的

,你需要...

<?php 
    if (
     isset($_POST['agree_button'])&& 
     ($_POST['agree_button'] == 'I agree') 
    ) { 
     setcookie('agreed', 'true'); 
     header('Location: /'); // redirect to main page 
    } 
?> 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
    <p>INSERT MESSAGE HERE (User must agree)</p> 
    <input type="submit" value="I agree" name="agree_button" /> 
    <input type="button" value="I disagree" /> 
</form> 

看到setcookie() man page

+0

喜歡PHP,但是使用PHP來創建Cookie部分呢?不想爲此使用JavaScript。謝謝。 – Cameron 2010-08-19 21:18:56

+0

當我點擊按鈕cookie被設置,但我仍然看到窗體和消息,除非我刷新頁面,這是奇怪的,因爲這是包含在頁面中,並張貼回來,所以肯定它應該隱藏在用戶單擊按鈕時沒有需要重定向?無論如何,代碼會用於重定向如果我需要一個?謝謝 – Cameron 2010-08-19 21:37:27

+0

增加了一個重定向的例子 – 2010-08-20 08:08:46

1

Sessions代替Cookies,因爲c ookies可以被用戶禁用。 和會話比cookie

設置會話使用更安全:

session_start(); 
$_SESSION['session_exists'] = 1; 

,並檢查使用此:

if($_SESSION['session_exists'] != 1) { include('warning.php'); exit; } 

如果您有任何問題,讓我知道我將修改。

+0

會議最常存儲在餅乾,雖然......總之,如果是這樣的案例 - 餅乾被打亂 - 然後他們不會看到該網站,除非他們啓用了cookie。所以,效果會和沒有cookie /表示不同意一樣。 – JAL 2010-08-19 21:04:42

+0

你能告訴我如何做到以上使用會話?謝謝。 – Cameron 2010-08-19 21:06:31

+0

會話使用cookie來存儲會話ID。如果cookie被禁用,會話只能在會話ID附加到_every_ url時使會話不可用 – 2010-08-19 21:06:47

1

這是服務器端方法。

您必須在設置Cookie後重新加載頁面以使其生效 - 因此使用位置進行重定向。對於POST表單來說,這是一個很好的做法,無論如何,使用HTTP 303來避免'你想重新提交嗎?'如果用戶重新加載頁面。

<?php 
    $redir=false; 
    if($_POST['agreed']){ setcookie('allow','yes',time()+3600); $redir=true;} 
    elseif($_POST['refused']) { setcookie('allow','no',time()+3600); $redir=true;} 
    if($redir){ header("Location: thispage.php",303); } 
?> 


<form method='post' action='thispage.php'> 
<p>Do you agree to our voluminous and vast list of preconditions?</p> 
<input type="submit" name='agreed' value="I agree" /> 
<input type="submit" name='refused' value="I disagree" /> 
</form> 

<?php 

if($_COOKIE['allow']=='no'){ echo 'Not authorized'; exit; } 
elseif($_COOKIE['allow']=='yes'){ echo 'Welcome to my amazing site - thanks for bein$ 
else{ echo 'Please read our terms and select your choice to continue'; exit; } 

PHP setcookie文檔和cookie main section。 Cookies通過'$ _COOKIE超全球'進入。

1

我會去的東西,如:

<form> 
    <p>INSERT MESSAGE HERE (User must agree)</p> 
    <input type="submit" name="conditional_access_agree" value="I agree" /> 
    <input type="button" name="conditional_access_disagree" value="I disagree" /> 
</form> 

然後

if(($_COOKIE['agreed'] != 'true') 
    && ($_POST['conditional_access_agree'] != "I agree")) { 
    include('warning.php'); 
    exit; 
} elseif (($_COOKIE['agreed'] != 'true') 
    && ($_POST['conditional_access_agree'] == "I agree")) { 
    setcookie('agreed', 'true', time()+60*60*24*30, '/'); 
} 

C.