简体   繁体   English

我为我的域设置了一个全局cookie,当用户导航到一个页面时,我想检查该cookie是否存在; 如果没有那么它会给他们404

[英]ive set a global cookie for my domain and when user navigates to a page i want to check if that cookie exists; if not then it will give them a 404

Im trying to set a cookie for my domain via php so that once the user tries to navigate to another specific webpage that will require that cookie it will either allow them to view the html contents or block access. 我试图通过php为我的域设置一个cookie,这样一旦用户试图导航到另一个需要cookie的特定网页,它将允许他们查看html内容或阻止访问。

This is what ive put in the cookie.php file where i set the cookie 这是我在cookie.php文件中放置的内容,我在其中设置了cookie

`<?php
$cookie_name = 'jevans';
$cookie_value = "0042";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>`

This is the page i want protected and only accessible if the cookie is present 这是我想要保护的页面,只有cookie存在时才可访问

`<?php
if(!isset($_COOKIE[$cookie_name])) {
exit;
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?> <html>content to be displayed</html>`

I expect for the script to check if the cookie exists and either allow or block based on whether they have that cookie. 我希望脚本能够检查cookie是否存在,并根据是否有cookie来允许或阻止。

Your protected page should look like this 受保护的页面应如下所示

<?php 
    $cookie_name = $_GET['cookie_name'] or $_POST['cookie_name'];

    if(!isset($_COOKIE[$cookie_name])) {
         exit; 
    } else { 
         echo "Cookie '" . $cookie_name . "' is set!<br>";     
         echo "Value is: " . $_COOKIE[$cookie_name]; 
    } 
?> <html>content to be displayed</html>

You use the $_GET for a get request or the $_POST for a post request. 您将$ _GET用于获取请求,或使用$ _POST用于发布请求。 A sample get request should look like this http://localhost/test/protectedpage.php?cookie_name=jevans 示例获取请求应如下所示:http://localhost/test/protectedpage.php?cookie_name = jevans

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM