/    Sign up×
Community /Pin to ProfileBookmark

one year session

I am having some difficulty with a login in script that uses sessions and cookies. I am truing to provide a check box to enable users to bypass the login on their machine.

For some bizarre reason the script works sometimes. I know it will be under certain conditions, bur I can not nail those conditions, and consequentiality can not fix the problem.

I am using “session_set_cookie_params()” and have found this:

[quote]

Set cookie parameters defined in the php.ini file. The effect of this function only lasts for the duration of the script. Thus, you need to call session_set_cookie_params() for every request and before session_start() is called.

[/quote]

from:
[URL=http://]http://uk.php.net/manual/en/function.session-set-cookie-params.php[/URL]

Does this comment “function only lasts for the duration of the script” mean that the session variables will only be available for that session and if I close the browser and re open the browser, I will loose the session variables.

Please forgive me for posting all the code, I try not to do this normally, but I don’t know where the problem is.

[code]
if ($row)// $row is the result from an mysql query
// that pulls the username and pasword
// from the database
{
if ($checkbox == yes) // checkbox == yes
{
session_name (‘YourVisitID’);
session_set_cookie_params (31536000, ‘/’, ‘www.mydomain.co.uk’);

session_start();

//================
$_SESSION[‘first_name’] = $row[1];
$_SESSION[‘user_id’] = $row[0];
$_SESSION[‘tests_in_this_session’] = 0;
//================

header (“Location: [url]http://[/url]” . $_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘PHP_SELF’]) . “/loggedin.php”);

exit();
}

else // checkbox == no
{

session_name (‘YourVisitID’);

session_set_cookie_params (900, ‘/’, ‘***.co.uk’); //900 sec = 15 min

session_start();

//======
$_SESSION[‘first_name’] = $row[1];
$_SESSION[‘user_id’] = $row[0];
$_SESSION[‘tests_in_this_session’] = 0;
//=======

header (“Location: [url]http://[/url]” . $_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘PHP_SELF’]) . “/loggedin.php”);

exit();
}

}
else
{
$message = ‘<p>The username and password entered do not match those on file.</p>’;
}
[/code]

This script doesn’t always write a cookie to my machine. When it writes one the script works fine.

I don’t know why it doesn’t always write the cookie to my machine.

Please help,

it’s driving me mad..

Dom…?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@domukauthorAug 27.2004 — I have solved it.

I was calling the session_start(); on another page and re directing back to this page.


and thus did not comply with this:

need to call session_set_cookie_params() for every request and before session_start() is called.[/quote]


Sorry for your inconvenience,

Dom.
×

Success!

Help @domuk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...