cs1h Posted November 23, 2007 Share Posted November 23, 2007 Hi, I have a problem and no idea how to solve it. My site has a login facility and if the person is not logged in it tells them so and asks them to go to the login page. The bit of script that does this is this, <?php $username = $_COOKIE['loggedin']; if (!isset($_COOKIE['loggedin'])) die("You are not logged in, <a href=index.php>click here</a> to login."); ?> What I would like though is for it to automatically redirect them to a login/register (called log.php) page and once they have done this for it to take them back to the page that they originally wanted. But I don't know how to do this, I guess that I would have to capture the URL and send it to the log.php page when it automatically redirects you. Does anyone know how I could do this? Any help will be much appreciated, Colin Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 23, 2007 Share Posted November 23, 2007 You can make a function and call it in your page for e.g <?php function checkUser() { if($_COOKIE['loggedin']!="") { return true; } else { echo"<script> location.href='index.php'</script>"; } } // call this function if the user is not logged in, it will redirect him to index.php page checkUser(); ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted November 23, 2007 Share Posted November 23, 2007 From the manual: Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);. (check setcookie(), at common pitfalls) Orio. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 23, 2007 Share Posted November 23, 2007 Hmmm... So it means it is better to user $_SESSION. Am i right ? Quote Link to comment Share on other sites More sharing options...
boushley Posted November 23, 2007 Share Posted November 23, 2007 So you would be able to use something like... //however you want to check if they're logged in... //this is if they're not header('Location: log.php?dest='.urlencode($_SERVER['REQUEST_URI'])); Then on the log.php you could set a hidden form field to pass this along... and then upon a successful login, if this value exists... you can forward them there. Quote Link to comment 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.