steviez Posted April 15, 2008 Share Posted April 15, 2008 Hi, I need my users to be logged in to access some pages on my site, So i have made this simple code to do it: <?php function loggedin() { if(!isset($_COOKIE['loggedin'])) { header("location: $secure_url/login.php"); } ?> But it will not work, I am using PHP5 so im not sure if thats a problem. Please can someone help! Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/ Share on other sites More sharing options...
kenrbnsn Posted April 15, 2008 Share Posted April 15, 2008 What do you mean by "it will not work". Please explain your problem better. Do you get any errors? Ken Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517605 Share on other sites More sharing options...
craygo Posted April 15, 2008 Share Posted April 15, 2008 first off it won't work cause your function does not know $secure_url. You would have to pass the variable to the function or make it global pass it function loggedin($secure_url) { if(!isset($_COOKIE['loggedin'])) { header("location: $secure_url/login.php"); } } loggedin("https://mysiite.com"); Or use global function loggedin() { global $secure_url; if(!isset($_COOKIE['loggedin'])) { header("location: $secure_url/login.php"); } } Ray Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517610 Share on other sites More sharing options...
steviez Posted April 15, 2008 Author Share Posted April 15, 2008 Thanks for your help so far, the problem is that it is acting as thought the cookie is set... in other words it is not redirecting the user to the login page even though there is no cookie set. Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517730 Share on other sites More sharing options...
benphp Posted April 15, 2008 Share Posted April 15, 2008 Or try pulling the code out of the function and getting it to work on a page. Then put it back in a function once you get it working. It won't redirect visitors if it doesn't know where to redirect them. You need to define $secure_url Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517737 Share on other sites More sharing options...
craygo Posted April 15, 2008 Share Posted April 15, 2008 Have you tried from other machines. I have seen cookie stay set forever. Maybe in your code somewhere you should make sure your named cookie is unset before you set it again. can you post code on how you are setting the cookie Ray Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517740 Share on other sites More sharing options...
steviez Posted April 15, 2008 Author Share Posted April 15, 2008 even if i take the code out of the function and place it at the top of my page it still just gives access to it. Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517751 Share on other sites More sharing options...
craygo Posted April 15, 2008 Share Posted April 15, 2008 instead of isset maybe you should check the value. if(!isset($_COOKIE['loggedin']) || $_COOKIE['loggedin'] IS NULL || $_COOKIE['loggedin'] == " ") have you tried to echo the cookie and see what it is set at so you can compare it?? Ray Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517755 Share on other sites More sharing options...
steviez Posted April 15, 2008 Author Share Posted April 15, 2008 something even more strange is that if i do this: <?php function loggedin() { if(!isset($_COOKIE['loggedin'])) { print("You are not logged in"); #header("location: $secure_url/login.php"); } ?> It works and prints it out for all that are not logged in. Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517762 Share on other sites More sharing options...
craygo Posted April 15, 2008 Share Posted April 15, 2008 Do you have your errors suppressed?? Cause if you are outputting anything to the browser first, even just a tag, header will not work and will not error if you have them suppressed. Ray Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-517804 Share on other sites More sharing options...
steviez Posted April 16, 2008 Author Share Posted April 16, 2008 Thanks for everyones help, i have solved it by putting the code at the top of each page before the header Quote Link to comment https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/#findComment-518758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.