开发者

Check session and cookie not working in PHP

开发者 https://www.devze.com 2023-01-08 21:17 出处:网络
Ihave this code that makes sure your are logged in, an开发者_如何转开发d then making sure you are on the right page by checking a cookie set at login. This code works on a page in a directory undernea

I have this code that makes sure your are logged in, an开发者_如何转开发d then making sure you are on the right page by checking a cookie set at login. This code works on a page in a directory underneath the login in script, however in a page in a directory below that it always takes you to accessdenied. Any ideas?

<?php

session_start();

if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
    header("location: http://mywebsite.com/member/accessdenied.html");
    exit();
}


 $_COOKIE["verify"] = $verify;
 if( $verify != file_get_contents("name.txt")) { 
  header("location: http://mywebsite.com/member/accessdenied.html");
 } else {  }

 ?>

And it seems like just the bottom part, the part that checks the cookie, isn't working. Again, any ideas?


I think you have your cookie assignment backwards:

$_COOKIE["verify"] = $verify;

Should be

$verify = $_COOKIE["verify"];

And that should be:

$verify = isset($_COOKIE["verify"])?$_COOKIE["verify"]:false;

As if the cookie was not previously set, well it would give a notice error.


<?php

 $verify = $_COOKIE["verify"];

 if( $verify == file_get_contents("name.txt")) { 
     echo $verify . 'is equal to the content of name.txt'
 } else {  
     echo $verify . 'is NOT equal to the content of name.txt'
 }

 ?>

Try debugging the code with this. See if the content of your variable is what you want. But I find it unusual that a variable would be a file.


are you sure you always get the content from file_get_contents? I could imagine it's found in one directory but not in the other!


antoher idea: cookies can be set to be relevant for a particular directory only. I just realize, what we're missing here, is the part where you set the cookie in the first place.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号