Jump to content

Puzzled


cs1h

Recommended Posts

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

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.