Can anyone help me with another silly newbie question? I am calling the following code to check/set cookies and sessions, but I'm having two problems:
1) If I go to a form that calls this code when I already have cookies set, and then I change a field's contents (such as the company name), the code doesn't change the cookie to the new contents as expected.
2) If I open a new browser session, the code does check for cookies properly and then deals with them accordingly (i.e. inserting them into the correct fields on a form), but then if I go to another form on the site, within the same session, usually the form fields are blank rather than being auto-filled wtih the cookie data as expected.
Here's my code:
function cookieCheck($cookiename) {
if (isset($_COOKIE[$cookiename])) {
if (isset($_POST[$cookiename])) {
if ($_COOKIE[$cookiename] != $_POST[$cookiename]) {
setcookie($cookiename,'',time() + 60*60*24*365);
setcookie($cookiename, $_POST[$cookiename], time() + 60*60*24*365);
} else {
}
if (!session_name('Softrak')) {
session_name('Softrak');
session_start('Softrak');
$_SESSION[$cookiename] = $_POST[$cookiename];
} else {
if (session_name('Softrak')) {
$_SESSION[$cookiename] = NULL;
$_SESSION[$cookiename] = $_POST[$cookiename];
}
}
}
} else {
if (isset($_SESSION[$cookiename])) {
if (isset($_POST[$cookiename])) {
if ($_SESSION[$cookiename] != $_POST[$cookiename]) {
$_SESSION[$cookiename] = NULL;
$_SESSION[$cookiename] = $_POST[$cookiename];
}
if ($_COOKIE[$cookiename] != $_POST[$cookiename]) {
setcookie($cookiename);
setcookie($cookiename, $_POST[$cookiename], time() + 60*60*24*365);
}
}
} else {
if (isset($_POST[$cookiename])) {
setcookie($cookiename, $_POST[$cookiename], time() + 60*60*24*365);
if (!session_name('Softrak')) {
session_name('Softrak');
session_start('Softrak');
$_SESSION[$cookiename] = $_POST[$cookiename];
} else {
if (session_name('Softrak')) {
$_SESSION[$cookiename] = NULL;
$_SESSION[$cookiename] = $_POST[$cookiename];
}
}
}
}
}
}
I'm sure there's some real obvious mistakes here, but I'm stuck! I've worked on this code for hours and hours on end and can't quite get it to work!
Thanks in advance for your help. This newbie is very grateful! 🙂