I've spent all day on this and am getting no where...
I have a script that uses the switch command.
If anyone has any insight as to why this isn't working, please elighten me!
Before I fall into the switches, first I check to see if it's been through the login case. If it has, I check to see if the cookie has been set. If it hasn't been through the Login or the cookie is either non-existant or incorrect, then I check the database to see if the username/password is valid and then write the cookie.
I'm added to echo commands to see what the values are. It will let me login and take me to the next case, BUT, when I click the submit button on that next page, it comes back and says my cookie isn't set and displays the login page. Here's the code:
$form =& $_POST;
$error = "";
echo "<br>1loggedin: ".$loggedin." - ".$form['caseIN']."\n";
if ($form['caseIN']) {
echo "<br>1: ".$form['caseIN']." - if casein\n";
include "calldb.inc";
if(isset($_COOKIE['mycookie'])) {
$array = explode(":", $_COOKIE['mycookie']);
echo "<br>user: ".$form['user']."\n";
echo "<br>array0: ".$array[0]."\n";
echo "<br>array1: ".$array[1]."\n";
if(!isset($array[1])) { $array[1] = ""; }
if(crypt($array[0], $salt) == $array[1]) { $loggedin = 1; }
else { $loggedin = 0;
echo "<br>cookie doesnot match\n"; }
} else {
echo "<br>2: ".$form['caseIN']." - cookie not set\n";
$loggedin = 0;
} // end if(isset($_COOKIE['macademy']))
echo "<br>2loggedin: ".$loggedin."\n";
if ($loggedin == 0) {
echo "<br>not logged in\n";
$user = $form['user'];
$pass = $form['pass'];
include "calldb.inc";
$dblink = mysql_connect($hostname,$username,$password) or
die(mysql_errno().":".mysql_error()."<br>ERROR: mySQL connection error\n");
mysql_select_db($dbname,$dblink) or
die(mysql_errno().":".mysql_error()."<br>ERROR: mySQL database error\n");
$sql = "SELECT username
FROM usertable
WHERE username = '".$user."'
AND password = '".crypt($pass, $salt)."'";
$result = mysql_query($sql,$dblink) or
die(mysql_errno().":".mysql_error()."<br>ERROR: query failed\n");
echo "<br>sql: ".$sql."\n";
if(mysql_num_rows($result) > 0) {
$secret = crypt($user,$salt);
$cookievalue = $user.":".$secret;
$timeout = time() + 60 *15;
setcookie("mycookie", $cookievalue, $timeout);
echo "<br><b>set cookie</b> ".$cookievalue."\n";
$match = 1;
} else {
$match = 0;
$form['caseIN'] = "Login";
setcookie("mycookie");
echo "<SCRIPT LANGUAGE='JavaScript'>\n";
echo "alert (\"Invalid Login. Try Again.\");\n";
echo "</script>\n";
} // end if(mysql_num_rows($result) > 0)
} // end if ($loggedin == 0)
mysql_close ($dblink);
} // end if ($form['caseIN'])
Please ignore all the echo statements. I even tried setting a timeout. Actually I want it to timeout when they close the browser window.
The structure for the switch commands is as follows:
switch ($caseCheck) {
// ***** Login ************************************** //
case 'Login':
default:
break;
// ***** EnterPreferences ***************************** //
case 'EnterPreferences':
break;
// ***** EnterMemo ********************************** //
case 'EnterMemo':
switch ($prefCheck) {
// ***** DeleteWeek ******************************** //
case 'DeleteMemo':
break;
// ***** NewWeek ********************************** //
case 'NewMemo':
default:
switch ($newCheck) {
// ***** AddText ********************************* //
case 'AddNewText':
default:
break;
// ***** FinishedAddingNew ************************ //
case 'FinishedAddingNew':
break;
}
break;
// ***** EditWeek ********************************** //
case 'EditMemo':
switch ($editCheck) {
// ***** SelectEditText **************************** //
case 'SelectEditText':
default:
break;
// ***** EditText ********************************* //
case 'EditText':
break;
// ***** FinishedEditing **************************** //
case 'FinishedEditing':
break;
}
break;
}
break;
}