I want my cookie not to be reset when i close the browser. The way i have it right now it expires whenever i close the browser... I REALLY REALLY need a solution to this so please if someone could give me some time id REALLY appriciate it!! I dont know how to set it this way.... i want it to expire like a week after you login not very time you close the browser. Im doing this because im an iPhone developer and i have a web app that uses this but i dont want my customers to have to login every time they open the app! Theyv been complaining about it and i need to fix it.

This is my login.php file

<?php

/**
* login.php
*
* @version 1.0
* @copyright 2008 by ?????? for 

XNova
*/

define('INSIDE'  , true);
define('INSTALL' , false);

$InLogin = true;

$ugamela_root_path = './';
include($ugamela_root_path . 'extension.inc');
include

($ugamela_root_path . 'common.' . $phpEx);

    includeLang('login');

    if ($_POST) 

{
        $login = doquery("SELECT * FROM {{table}} WHERE 

`username` = '" . mysql_escape_string($_POST['username']) . "' LIMIT 1", 

"users", true);

        if ($login) {
            if ($login

['password'] == md5($_POST['password'])) {
                if     



(isset($_POST["rememberme"])) {

                        

                    }

                

@include('config.php');
                $cookie = $login["id"] 

. "/%/" . $login["username"] . "/%/" . md5($login["password"] . "--" . $dbsettings

["secretword"]) . "/%/" . $rememberme;
                

setcookie($game_config['COOKIE_NAME'], $cookie, $expiretime, "/", "", 0);

        

        unset($dbsettings);
                if ($login

['urlaubs_modus'] == 1) {
                    $time_out = 

((($login['urlaubs_modus_time'] + VOCATION_TIME)-time())/3600);
            

        if (time() >= $login['urlaubs_modus_time'] + VOCATION_TIME ){
    

                    header("Location: 

./options.php");
                        exit;
        

            } else {
                            

message($lang['vacation_mode'].floor($time_out).$lang['hours'], $lang['vacations']);
    

                }
                } else {
        

        header("Location: ./overview.php");
                

}
                exit;
            } else {
        

        message($lang['Login_FailPassword'], $lang['Login_Error']);
        

    }
        } else {
            message($lang

['Login_FailUser'], $lang['Login_Error']);
        }

    } elseif(!empty

($_COOKIE[$game_config['COOKIE_NAME']])) {
        $cookie = explode

('/%/',$_COOKIE[$game_config['COOKIE_NAME']]);
        $login = 

doquery("SELECT * FROM {{table}} WHERE `username` = '" . 

mysql_escape_string($cookie[1]) . "' LIMIT 1", "users", true);
        if 

($login) {
            @include('config.php');
            if 

(md5($login["password"] . "--" . $dbsettings["secretword"]) == $cookie[2]) {
        

        unset($dbsettings);
                header

("Location: ./overview.php");
                exit;
            

} 
        }
    } 

        $parse = $lang;
        

$query = doquery('SELECT username FROM {{table}} ORDER BY 

register_time DESC', 'users', true);
        $parse['last_user'] = $query

['username'];
        $query = doquery("SELECT COUNT(DISTINCT(id)) 

FROM {{table}} WHERE onlinetime>" . (time()-900), 'users', true);
        

$parse['online_users'] = $query[0];
        $parse['users_amount'] = 

$game_config['users_amount'];
        $parse['servername'] = 

$game_config['game_name'];
        $parse['forum_url'] = $game_config

['forum_url'];
        $parse['PasswordLost'] = $lang['PasswordLost'];

        

$page = parsetemplate(gettemplate('login_body'), $parse);
        display

($page, $lang['Login']);



// ----------------------------------------------------------------------------------------------------

-------
// History version
?>

Recommended Answers

All 16 Replies

<?php

/**
* login.php
*
* @version 1.0
* @copyright 2008 by ?????? for 

XNova
*/

define('INSIDE'  , true);
define('INSTALL' , false);

$InLogin = true;

$ugamela_root_path = './';
include($ugamela_root_path . 'extension.inc');
include

($ugamela_root_path . 'common.' . $phpEx);

    includeLang('login');

    if ($_POST) 

{
        $login = doquery("SELECT * FROM {{table}} WHERE 

`username` = '" . mysql_escape_string($_POST['username']) . "' LIMIT 1", 

"users", true);

        if ($login) {
            if ($login

['password'] == md5($_POST['password'])) {
                if     



(isset($_POST["rememberme"])) {

                        

                    }

                

@include('config.php');
                $cookie = $login["id"] 

. "/%/" . $login["username"] . "/%/" . md5($login["password"] . "--" . $dbsettings

["secretword"]) . "/%/" . $rememberme;
                
$expiretime = time()+3600*24*30; // 30 days.
setcookie($game_config['COOKIE_NAME'], $cookie, $expiretime, "/", "", 0);

        

        unset($dbsettings);
                if ($login

['urlaubs_modus'] == 1) {
                    $time_out = 

((($login['urlaubs_modus_time'] + VOCATION_TIME)-time())/3600);
            

        if (time() >= $login['urlaubs_modus_time'] + VOCATION_TIME ){
    

                    header("Location: 

./options.php");
                        exit;
        

            } else {
                            

message($lang['vacation_mode'].floor($time_out).$lang['hours'], $lang['vacations']);
    

                }
                } else {
        

        header("Location: ./overview.php");
                

}
                exit;
            } else {
        

        message($lang['Login_FailPassword'], $lang['Login_Error']);
        

    }
        } else {
            message($lang

['Login_FailUser'], $lang['Login_Error']);
        }

    } elseif(!empty

($_COOKIE[$game_config['COOKIE_NAME']])) {
        $cookie = explode

('/%/',$_COOKIE[$game_config['COOKIE_NAME']]);
        $login = 

doquery("SELECT * FROM {{table}} WHERE `username` = '" . 

mysql_escape_string($cookie[1]) . "' LIMIT 1", "users", true);
        if 

($login) {
            @include('config.php');
            if 

(md5($login["password"] . "--" . $dbsettings["secretword"]) == $cookie[2]) {
        

        unset($dbsettings);
                header

("Location: ./overview.php");
                exit;
            

} 
        }
    } 

        $parse = $lang;
        

$query = doquery('SELECT username FROM {{table}} ORDER BY 

register_time DESC', 'users', true);
        $parse['last_user'] = $query

['username'];
        $query = doquery("SELECT COUNT(DISTINCT(id)) 

FROM {{table}} WHERE onlinetime>" . (time()-900), 'users', true);
        

$parse['online_users'] = $query[0];
        $parse['users_amount'] = 

$game_config['users_amount'];
        $parse['servername'] = 

$game_config['game_name'];
        $parse['forum_url'] = $game_config

['forum_url'];
        $parse['PasswordLost'] = $lang['PasswordLost'];

        

$page = parsetemplate(gettemplate('login_body'), $parse);
        display

($page, $lang['Login']);



// ----------------------------------------------------------------------------------------------------

-------
// History version
?>

Hmm.. I'm not sure, but I think this will work. I dont see the $expiretime get any value, so I added one.

That didnt work... when i went to the page that you need to be logged into to view it just redirected me to login.php thanks for trying though....

That didnt work... when i went to the page that you need to be logged into to view it just redirected me to login.php thanks for trying though....

Have you set your browser to delete your cookies when you close your browser. I would check your cookie preferences in your web browser and make sure cookies do not get deleted by the browser. I think that is the key to the problem.

Yea but it remembers when I login to facebook and other sites

Do you only have the one cookieset function because the code in Muazam's post looks very correct.

Well iv tested it on firefox and safari and they both do the same thing. I go into view the cookies on safari and where it shoes the date it expires its just blank like no date or anything.

Try setting line 57 of Mauzam's script to the following

$expiretime = (time()+((3600*24)*30))

No it didnt work... Let me explain how the login works a little better... When you login it gives you the right to view pages that you couldnt view if you werent logged in. Like if you log in you can go to overview.php but if your not logged in then it redirects you back to login. I tried to go to overview with the code you gave me but it didnt work... Is it possible it uses something else to set the cookie?

No it didnt work... Let me explain how the login works a little better... When you login it gives you the right to view pages that you couldnt view if you werent logged in. Like if you log in you can go to overview.php but if your not logged in then it redirects you back to login. I tried to go to overview with the code you gave me but it didnt work... Is it possible it uses something else to set the cookie?

I have heard that it is possible to use javascript or even java to set cookies but I wouldn't recommend it because if a user has javascript disabled then the cookie will not be set. This is an unusual bug and may I ask what version of php you have?

Version 5.1 i think thats what its called lol i know its the newest one. I have a .com and thats what it uses

This is some code i just found. Its called checkcookies.php

<?php

/**
 * CheckCookies.php
 *
 * @version 1.1
 * @copyright 2008 By 

Chlorel for XNova
 */
// TheCookie[0] = `id`
// TheCookie[1] = `username`
// 

TheCookie[2] = Password + Hashcode
// TheCookie[3] = 1rst Connexion time + 

365 J

function CheckCookies ( $IsUserChecked ) {
	global $lang, 

$game_config, $ugamela_root_path, $phpEx;

	includeLang('cookies');

	

$UserRow = array();

	include($ugamela_root_path . 'config.' . $phpEx);

	

 if (isset($_COOKIE[$game_config['COOKIE_NAME']])) {
		$TheCookie 

 = explode("/%/", $_COOKIE[$game_config['COOKIE_NAME']]);
		

$UserResult = doquery("SELECT * FROM `{{table}}` WHERE `username` = '". 

$TheCookie[1]. "';", 'users');

		// On verifie s'il y a qu'un seul 

enregistrement pour ce nom
		if (mysql_num_rows($UserResult) != 1) {
	

		message( $lang['cookies']['Error1'] );
		}

		

$UserRow    = mysql_fetch_array($UserResult);

		// On teste 

si on a bien le bon UserID
		if ($UserRow["id"] != $TheCookie[0]) {
		

	message( $lang['cookies']['Error2'] );
		}

		// 

On teste si le mot de passe est correct !
		if (md5($UserRow

["password"] . "--" . $dbsettings["secretword"]) !== $TheCookie[2]) {
			

message( $lang['cookies']['Error3'] );
		}

		$NextCookie 

= implode("/%/", $TheCookie);
		// Au cas ou dans l'ancien 

cookie il etait question de se souvenir de moi
		// 3600 = 1 Heure // 

86400 = 1 Jour // 31536000 = 365 Jours
		// on ajoute au 

compteur!
		if ($TheCookie[3] == 1) {
			$ExpireTime 

= time() + 31536000;
		} else {
			$ExpireTime 

= 0;
		}

		if ($IsUserChecked == false) {
		

	setcookie ($game_config['COOKIE_NAME'], $NextCookie, $ExpireTime, 

"/", "", 0);
			$QryUpdateUser  = "UPDATE `{{table}}` SET ";
		

	$QryUpdateUser .= "`onlinetime` = '". time() ."', ";
			

$QryUpdateUser .= "`user_lastip` = '". $_SERVER['REMOTE_ADDR'] ."', ";
		

	$QryUpdateUser .= "`user_agent` = '". $_SERVER

['HTTP_USER_AGENT'] ."' ";
			$QryUpdateUser .= "WHERE ";
	

		$QryUpdateUser .= "`id` = '". $TheCookie[0] ."' LIMIT 1;";
		

	doquery( $QryUpdateUser, 'users');
			

$IsUserChecked = true;
		} else {
			

$QryUpdateUser  = "UPDATE `{{table}}` SET ";
			

$QryUpdateUser .= "`onlinetime` = '". time() ."', ";
			

$QryUpdateUser .= "`user_lastip` = '". $_SERVER['REMOTE_ADDR'] ."', ";
		

	$QryUpdateUser .= "`user_agent` = '". $_SERVER

['HTTP_USER_AGENT'] ."' ";
			$QryUpdateUser .= "WHERE ";
	

		$QryUpdateUser .= "`id` = '". $TheCookie[0] ."' LIMIT 1;";
		

	doquery( $QryUpdateUser, 'users');
			

$IsUserChecked = true;
		}
	}

	unset($dbsettings);

	

$Return['state']  = $IsUserChecked;
	$Return['record'] = $UserRow;

	

return $Return;
}
?>
if ($TheCookie[3] == 1) {
			$ExpireTime 
 
= time() + 31536000;
		} else {
			$ExpireTime 
 
= 0;
		}

The latest version of php is 5.3 although I would recommend version 5.2. Also try replacing the above with the below.

$ExpireTime = (time() + 31536000);

So to completely remove the if statement and just place that line there to see if that will set the cookie.

OMFG!!! DUDE!!!!! IT WORKED THANKYOU SO MUCH!!!! The 800 players of my iPhone game and me especially owe you quite a thanks!!! They will be so happy! I cant wait to tell them all haha i danced around the room when it worked :) thanks again

Oh and just one more question. How could i make that cookie basically last forever? Thx

Then you would set it for a really big number like

$ExpireTime = (time() + 999999999);

That is about as long as you can set a cookie before hitting the Unix timestamp bug and will last till the year 2037.

Ok thx again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.