Reply
how to create "keep me logged in" feature?
Old 07-31-2010, 12:55 AM how to create "keep me logged in" feature?
ddk
ddk's Avatar
Novice Talker

Posts: 14
Name: devikumar
Trades: 0
i created a login page....with a check box "keep me logged in" label...

when i check that n logged in.....it is setting cookies....fine upto there...

and when i close the browser n reopening it.....with login page...

itzz not working.....plss anyone help me!!!
ddk is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 07-31-2010, 02:02 AM Re: how to create "keep me logged in" feature?
Marik's Avatar
Skilled Talker

Posts: 68
Trades: 0
You should always post your code, that way we can look at it to see where you went wrong. Anyway, use this as an example to fix your own code:
PHP Code:
<?php

if (isset($_POST['submit'])) {

    
$username strip_tags($_POST['username']);
    
$password strip_tags($_POST['password']);
    
$rememberMe strip_tags($_POST['rememberMe']);

    if (
$username == "me" && $password == "123") {
        if (
$rememberMe) {
            
setcookie("loggedIn""yes"time()+3600);
        }
        echo 
"You are Logged in!<hr />";
    } else {
        echo 
"Username and/or password is incorrect.";
    }
}

if (
$_COOKIE['loggedIn'] == "yes") {
    echo 
"You are Still Logged In due to the Cookie!<hr />";
    die();
}

?>
Code:
<form action="" method="post">
    Username: <input type="text" name="username" /><br />
    Password: <input type="password" name="password" /><br />
    Remember Me: <input type="checkbox" name="rememberMe" /><br />
    <input type="submit" name="submit" value="Log In" />
</form>
__________________
Music Videos
Marik is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 02:08 AM Re: how to create "keep me logged in" feature?
Lashtal's Avatar
Darth XAMPP

Posts: 554
Name: Lashtal
Trades: 0
if your cookie is setting, but the data is not being retained when you reopen your browser; then it *probably* has something to do with failure to use "time()" appropriately.

Let's see your script.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 02:57 AM Re: how to create "keep me logged in" feature?
ddk
ddk's Avatar
Novice Talker

Posts: 14
Name: devikumar
Trades: 0
PHP Code:
<?php if ($_COOKIE['loggedIn'] == "yes") {
    echo 
"<meta http-equiv='refresh' content='0;url=http://localhost/newstart/admin.php'>";
    die();exit;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>login page!</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="sheet">
<table border="0" align="center" cellpadding="0" cellspacing="1" style="padding:30px; margin-top:100px; margin-left:20px;">
<tr>
<form name="form1" method="post" action="verify.php">
<td>
<table width="100%" border="0" cellpadding="5" cellspacing="1">
<th colspan="3" align="left"><strong>Member Login </strong></th>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
</td><td>
<td><input type="checkbox" name="remember" value="remember"/></td>
<td><p>keep me logged in!</p></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login" ></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<div style="margin-left:100px;">
<p>not a member? <a href="registration.php" style="color:#CCCCCC;"> register here</a></p>
</div>
</div>
</body>
</html>

this is my login page!

thanx 4 ur suggestions n help

Last edited by chrishirst; 07-31-2010 at 08:57 AM..
ddk is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 02:58 AM Re: how to create "keep me logged in" feature?
ddk
ddk's Avatar
Novice Talker

Posts: 14
Name: devikumar
Trades: 0
PHP Code:
<div>
<?php
include 'includes/connection.php';
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$rememberMe strip_tags($_POST['remember']);
// To protect Myinjection (SQL more detail about MySQL injection)
$myusername stripslashes($myusername);
$mypassword stripslashes($mypassword);
$myusername mysql_real_escape_string($myusername);
$mypassword mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$fetch mysql_fetch_array($result);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
@session_start();
$_SESSION['uid'] = $fetch['ID'];



if(
$count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"

  
if ($myusername == 'admin' && $mypassword == 'admin')
  {
    if (
$rememberMe == "remember") {
            
setcookie("loggedIn""yes"time()+3600);
             }
   echo 
"<meta http-equiv='refresh' content='0;url=http://localhost/newstart/admin.php'>";
  }
  else
  {
   echo 
"<meta http-equiv='refresh' content='0;url=http://localhost/newstart/index.php'>";
  }
 }
else {
echo 
"<script>";
echo 
"location.href = 'wrong_login.php'";
echo 
"</script>";

}
?>

</div>
this is my verify.php

Last edited by chrishirst; 07-31-2010 at 08:58 AM..
ddk is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 03:05 AM Re: how to create "keep me logged in" feature?
ddk
ddk's Avatar
Novice Talker

Posts: 14
Name: devikumar
Trades: 0
Marik.....i didnt get u!

plss help me!!!
ddk is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 03:17 AM Re: how to create "keep me logged in" feature?
Marik's Avatar
Skilled Talker

Posts: 68
Trades: 0
First test to see if the cookie has in fact been set by printing it:

echo $_COOKIE['loggedIn'];

somewhere on the admin.php page. if it prints "yes" then there is no problem with the cookie.

What could be the problem is how you are checking to see if the user is logged in or not.

So if admin.php is the protected page I am assuming you have some sort of check at the top of that page to see if the cookie "loggedIn" is set to the value "yes" and if not then redirect the user to the login page immediately instead of loading the rest of that page.

This can be done using something like this:

PHP Code:
if (!$_COOKIE['loggedIn'] == "yes") {
   echo 
"<script type='text/javascript'>window.location=\"login.php\"</script>";
   die();


You still need to post the script that is on the protected page. The one that actually checks to see if the user is logged in or not.
__________________
Music Videos
Marik is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 04:09 AM Re: how to create "keep me logged in" feature?
ddk
ddk's Avatar
Novice Talker

Posts: 14
Name: devikumar
Trades: 0
im getting error at setcookie statement....

cookie is not being set.....wat may be the problem....

the code i wrote is:

$returnvalue = setcookie("user", "Alex Porter", time()+3600);



marik thanx 4 ur replies...
ddk is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 06:59 AM Re: how to create "keep me logged in" feature?
Lashtal's Avatar
Darth XAMPP

Posts: 554
Name: Lashtal
Trades: 0
when posting your code, please use the "[PHP]" brackets, and name your pages appropriately.

For example...

verify.php
PHP Code:
<?php
include 'includes/connection.php';
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$rememberMe strip_tags($_POST['remember']);
// To protect Myinjection (SQL more detail about MySQL injection)
$myusername stripslashes($myusername);
$mypassword stripslashes($mypassword);
$myusername mysql_real_escape_string($myusername);
$mypassword mysql_real_escape_string($mypassword);
 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$fetch mysql_fetch_array($result);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
@session_start();
$_SESSION['uid'] = $fetch['ID'];
 
 
 
if(
$count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
 
if ($myusername == 'admin' && $mypassword == 'admin')
{
if (
$rememberMe == "remember") {
setcookie("loggedIn""yes"time()+3600);
}
echo 
"<meta http-equiv='refresh' content='0;url=http://localhost/newstart/admin.php'>";
}
else
{
echo 
"<meta http-equiv='refresh' content='0;url=http://localhost/newstart/index.php'>";
}
}
else {
echo 
"<script>";
echo 
"location.href = 'wrong_login.php'";
echo 
"</script>";
 
}
?>

And so on, for each page.

Not only does it make it easier for people to help you, it's a little friendlier on the eyes as well.

Personally, (as i'm still quite the PHP newb myself) I like to plug each page in to my own server and try to solve it.

Last edited by Lashtal; 07-31-2010 at 10:30 PM..
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 08:59 AM Re: how to create "keep me logged in" feature?
chrishirst's Avatar
Super Moderator

Posts: 32,316
Location: Blackpool. UK
Trades: 0
http://www.webmaster-talk.com/php-fo...st-my-php.html
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Code Samples | Crowded Nightclub? | Random Ramblings
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-31-2010, 10:51 AM Re: how to create "keep me logged in" feature?
Marik's Avatar
Skilled Talker

Posts: 68
Trades: 0
Quote:
Originally Posted by ddk View Post
$returnvalue = setcookie("user", "Alex Porter", time()+3600);
That won't work, try this instead:

PHP Code:
setcookie("user""Alex Porter"time()+3600);
$returnvalue $_COOKIE['user']; 
You still haven't posted ALL your code including the contents of admin.php, so this is as much as I can help.
__________________
Music Videos
Marik is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 10:31 PM Re: how to create "keep me logged in" feature?
Lashtal's Avatar
Darth XAMPP

Posts: 554
Name: Lashtal
Trades: 0
Quote:
Originally Posted by Marik View Post
...including the contents of admin.php, so this is as much as I can help.
Righto.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 08-02-2010, 05:24 AM Re: how to create "keep me logged in" feature?
Junior Talker

Posts: 1
Name: ericsson
Trades: 0
Thanks for the informations. Its very useful and most helpful for our career growth and development.
timothyericson is offline
Reply With Quote
View Public Profile
 
Old 08-03-2010, 02:13 AM Re: how to create "keep me logged in" feature?
Novice Talker

Posts: 6
Name: Contacts Transferlo
Trades: 0
Thanks for the informations
lucyisssi is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how to create "keep me logged in" feature?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.18163 seconds with 13 queries