Page 1 of 1
Remember User Login URL
Posted: Tue Jan 19, 2010 9:11 pm
by southasia
How to use cookie and session in following manner?
Let say we login to the devnetwork and left it open about thirty minutes, the next time we click on any menu link if the cookie is expire it will redirect to the login page for authentication again. After login it will redirect back to the page that user left the last time. How can I achieve that?

Re: Remember User Login URL
Posted: Tue Jan 19, 2010 9:30 pm
by requinix
On every page the code checks the logged-in user. Without a cookie the user isn't logged in.
If that's the case, the user's redirected to a login page.
Re: Remember User Login URL
Posted: Tue Jan 19, 2010 11:37 pm
by southasia
ok, but then how should I redirect to the previous page and how to store the page link into cookie? I should use cookie or session to store the page link?
if(!isset($_COOKIE['cookiename'])){
header("Location: login.php"); // Can I use cookie name instead of login.php ?
exit();
} else {
// Is it like this way ? if then how to open this page using header?
setcookie("cookiename", "currentpage.php", time()+1800, "/", "", 0);
}
Re: Remember User Login URL
Posted: Wed Jan 20, 2010 12:00 am
by requinix
southasia wrote:I should use cookie or session to store the page link?
I'd use the session. Easier to make sure the redirection URL wasn't forged.
Code: Select all
session_start();
if (!isset($_COOKIE["whatever"])) { // not logged in anymore
// set the redirection destination
if (!isset($_SESSION["login redirect"])) $_SESSION["login redirect"] = array();
$when = time(); $_SESSION["login redirect"][$when] = $_SERVER["REQUEST_URI"];
// redirect
session_write_close();
header("Location: /login.php?when={$when}");
exit;
}
Code: Select all
session_start();
if (/* login form submitted */) {
/* log in */
// there's a specific place to redirect to
if (isset($_GET["when"]) && ctype_digit($_GET["when"]) && isset($_SESSION["login redirect"][$_GET["when"]]) {
$url = $_SESSION["login redirect"][$_GET["when"]]; unset($_SESSION["login redirect"][$_GET["when"]]);
header("Location: {$url}");
exit;
// there wasn't
} else {
header("Location: /wherever");
exit;
}
}
Re: Remember User Login URL
Posted: Wed Jan 20, 2010 1:30 am
by southasia
The logic is closed with what I want but still figuring out where to put those code in my page.
Assume I have
1.Login/Index page
2.Main Page
3.Sub Page
Re: Remember User Login URL
Posted: Thu Jan 21, 2010 9:30 pm
by southasia
Before going into complicated application, I am giving a try with following code. Its turn out not suppose what I want. Could any one correct my error? Specially my home.php and deletecookie.php Thanks.
Cookie seems not deleted. I use the cookie value as a address link.
menu2.php
----------
Code: Select all
<?php
if (isset ($_COOKIE["cookiename"]))
{
$currentLocation = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
setcookie("cookiename", "$currentLocation", time()+180, "/", "", 0);
}
else {
$redirect="";
$redirect=$_COOKIE["cookiename"];
header("Location: $redirect");
exit;
}
echo $currentLocation;
include 'links.php';
echo 'Menu2';
?>
menu1.php
----------
Code: Select all
<?php
if (isset ($_COOKIE["cookiename"]))
{
$currentLocation = 'http://' . $_SERVER['HTTP_HOST'] .$_SERVER['PHP_SELF'];
setcookie("cookiename", "$currentLocation", time()+180, "/", "", 0);
}
else {
$redirect="";
$redirect=$_COOKIE["hris"];
header("Location: $redirect");
exit;
}
echo $currentLocation;
include 'links.php';
echo "Menu1";
?>
link.php
--------
Code: Select all
<ul>
<a href="home.php">Home</a>
<a href="menu1.php">Menu 1</a>
<a href="menu2.php">Menu 2</a>
<a href="deletecookie.php">Delete Cookie</a>
</ul>
home.php
--------
Code: Select all
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_COOKIE['cookiename']))
{
$redirect="";
$redirect=$_COOKIE["cookiename"];
echo $redirect;
header("Location: $redirect");
exit;
} else {
$currentLocation = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
setcookie('cookiename',$currentLocation,time()+180,"/","",0);
}
echo $currentLocation;
include 'links.php';
echo "This is a home page.";
?>
</body>
</html>