Kraven1
11-15-2010, 06:53 AM
I'm working on a Login script in which I'm trying to add a Remember Me check box. The form uses 'login' and the session uses 'login'.
The login is working just fine, but I can't seem to get the Remember Me feature to work.
The login form and login exe script are separate files. The login exe is added to the login script using an include.
I've tried adding the code for setting the cookies to both the login form, and the login exe script, without luck.
I'm not getting any error messages, it just doesn't set the cookies.
When I close the browser and come back, I have to login again.
I realize there isn't a header location, but that's intentional.
This is a private message app that displays in a popup window, and I've added some code to the login page that queries the db to see if the user has new messages. That's why no header location. I want it to stay on the login page for the new message alert and link. Then they click on the New Messages link and it takes them to their inbox. That works fine as well until I close the browser. When I come back and open the login page, the new message link is gone, so that means cookies haven't been set, other wise it would recognize the user and display the new message link. (I think)
I'm not much of a coder, so there may be something else here that I'm just not aware of.
I'm adding the code I used to try to set the cookie. If some one can see the error, if there is one, I'd appreciate some pointers.
The login form is a table, so maybe there's something I have to add to the remember me check box code as well. I'm at a loss.
Thanks.
<?php
function checkLogin(){
/* Check if user has been remembered */
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
$_SESSION['login'] = $_COOKIE['cookname'];
$_SESSION['password'] = $_COOKIE['cookpass'];
}
/* Username and password have been set */
if(isset($_SESSION['login']) && isset($_SESSION['password'])){
/* Confirm that username and password are valid */
if(confirmUser($_SESSION['login'], $_SESSION['password']) != 0){
/* Variables are incorrect, user not logged in */
unset($_SESSION['login']);
unset($_SESSION['password']);
return false;
}
return true;
}
/* User not logged in */
else{
return false;
}
if(isset($_POST['rememberme'])){
setcookie("cookname", $_SESSION['login'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}
/* Quick self-redirect to avoid resending data on refresh
echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
return;*/
}
/* Sets the value of the logged_in variable, which can be used in your code*/
$logged_in = checkLogin();
?>
If you spot the error, please explain, as I'm trying to learn as well as make the script function. I do have session start at the top of the page.
Kraven1
The login is working just fine, but I can't seem to get the Remember Me feature to work.
The login form and login exe script are separate files. The login exe is added to the login script using an include.
I've tried adding the code for setting the cookies to both the login form, and the login exe script, without luck.
I'm not getting any error messages, it just doesn't set the cookies.
When I close the browser and come back, I have to login again.
I realize there isn't a header location, but that's intentional.
This is a private message app that displays in a popup window, and I've added some code to the login page that queries the db to see if the user has new messages. That's why no header location. I want it to stay on the login page for the new message alert and link. Then they click on the New Messages link and it takes them to their inbox. That works fine as well until I close the browser. When I come back and open the login page, the new message link is gone, so that means cookies haven't been set, other wise it would recognize the user and display the new message link. (I think)
I'm not much of a coder, so there may be something else here that I'm just not aware of.
I'm adding the code I used to try to set the cookie. If some one can see the error, if there is one, I'd appreciate some pointers.
The login form is a table, so maybe there's something I have to add to the remember me check box code as well. I'm at a loss.
Thanks.
<?php
function checkLogin(){
/* Check if user has been remembered */
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
$_SESSION['login'] = $_COOKIE['cookname'];
$_SESSION['password'] = $_COOKIE['cookpass'];
}
/* Username and password have been set */
if(isset($_SESSION['login']) && isset($_SESSION['password'])){
/* Confirm that username and password are valid */
if(confirmUser($_SESSION['login'], $_SESSION['password']) != 0){
/* Variables are incorrect, user not logged in */
unset($_SESSION['login']);
unset($_SESSION['password']);
return false;
}
return true;
}
/* User not logged in */
else{
return false;
}
if(isset($_POST['rememberme'])){
setcookie("cookname", $_SESSION['login'], time()+60*60*24*100, "/");
setcookie("cookpass", $_SESSION['password'], time()+60*60*24*100, "/");
}
/* Quick self-redirect to avoid resending data on refresh
echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
return;*/
}
/* Sets the value of the logged_in variable, which can be used in your code*/
$logged_in = checkLogin();
?>
If you spot the error, please explain, as I'm trying to learn as well as make the script function. I do have session start at the top of the page.
Kraven1