Connecting Tech Pros Worldwide Help | Site Map

Autologin using cookie

Newbie
 
Join Date: Feb 2008
Posts: 8
#1: Mar 3 '08
Hello all,
I have an autologin feature in the login form.So if autologin selected once,it should work using cookies then.How to use it.Please help???
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#2: Mar 3 '08

re: Autologin using cookie


[php]<?php
//file name: index.php
if isset($_COOKIE['name_of_the_cookie']) //COOKIE YOU ALREADY SET
{
$p1 = $_COOKIE['saved_password_in_the_cookie']; //PASSWORD IN THE COOKIE
/*
FETCH FROM DATABASE, THE ROW (SAY $db_row)HAVING THAT ID
$sql = _____________________________ ;
*/
$p2 = $db_row['password']; //PASSWORD FETCHED FROM DB FOR THAT ID

if ($p1 == $p2) //COMPARE PASSWORDS
{
header ("location:home.php"); //REDIRECT IF COOKIE VERIFIED
}
}
else
{
header ("location:login.php"); //REDIRECT TO LOGIN PAGE
}
?>[/PHP]
Is this what you need?
Newbie
 
Join Date: Feb 2008
Posts: 8
#3: Mar 3 '08

re: Autologin using cookie


Thanks,but an error comes,cannot modify header..
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#4: Mar 3 '08

re: Autologin using cookie


Quote:

Originally Posted by vashafnas

Thanks,but an error comes,cannot modify header..

Are you using it as it is?....

Remove any space or line break before <?php if there.
Newbie
 
Join Date: Feb 2008
Posts: 8
#5: Mar 4 '08

re: Autologin using cookie


Sorry for the late reply.
I used[php]
if(isset($_POST["autologin"]))
{
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('username',$_POST["Password"], $inTwoMonths);
setcookie('password',$_POST["Password"], $inTwoMonths);
}
in login page and
if(isset($_COOKIE['username']) AND isset($_COOKIE['password']))
{
$_SESSION["ww_2"]=23;
$redir=$_GET["redir"]?$_GET["redir"]:$_POST["redir"];
$redir=($redir?$redir:"./?p=myaccount&s=dayplanner");
header ("location:home.php");
//echo "<script language='javascript'>window.location='$redir'</script>";
exit();

} [/php]
in index page.
Please tell wat is wrong??Again i have to login..
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Mar 4 '08

re: Autologin using cookie


Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator
Reply