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
https://forums.phpfreaks.com/topic/78556-puzzled/
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
https://forums.phpfreaks.com/topic/78556-puzzled/#findComment-397536
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
https://forums.phpfreaks.com/topic/78556-puzzled/#findComment-397541
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
https://forums.phpfreaks.com/topic/78556-puzzled/#findComment-397664
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.