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.
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>’;
}
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…?