2010-08-19 37 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.