I am getting seriously frustrated that I can't get this to work. I've tried a few different approaches to this, and from all the examples and code I've read this SHOULD be working, but it isn't.
2012-10-19 19:24:04.533 192.168.1.62 "-" "WEBMATRIX" "-" 192.168.1.62 POST /Actions/NewTicket.php - 500 0 6149 688 45 -11640 "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4" "QCUserID=1" "http://192.168.1.62:53896/NewTicketPage.html" "-" "192.168.1.62:53896" 0
WebMatrix/Actions/login.php <- This works. I can see it in my cookies in Chrome. and it looks like it is in the log error.
<?php
//set vars
require_once(__DIR__ . "/GlobalCookies.php");
GC_ClearCookie("QCUserID");
$user = $_POST['username'];
$pass = $_POST['password'];
require_once(__DIR__ . "/db.php");
$query = sprintf("SELECT * FROM users WHERE UserInitials='%s' AND UserPassword='%s'", mysql_real_escape_string($user),mysql_real_escape_string($pass));
$result = mysql_query($query);
$rows = mysql_num_rows($result);
if($rows!=0)
{
$row = mysql_fetch_row($result);
GC_SetCookie("QCUserID", $row[0]);
header("refresh:0;url=../DashboardPage.php");
}
else
{
echo("User Not Found, redirecting to login page in 3 seconds");
header("refresh:3;url=../index.php");
}
?>
WebMatrix/Actions/GlobalCookies.php <- strange that it doesn't get the ?> at the end of the file.. I didn't look up how to do a functions file, I just copied some of it from Mantisbt. but it throws a error 500 if i have it and try to use it in login.php
<?php
function GC_GetCookie( $p_var_name, $p_default = null )
{
if( isset( $_COOKIE[$p_var_name] ) )
{
$t_result = gpc_strip_slashes( $_COOKIE[$p_var_name] );
}
else if( func_num_args() > 1 )
{
//check for a default passed in (allowing null)
$t_result = $p_default;
}
else
{
error_parameters( $p_var_name );
trigger_error( ERROR_GPC_VAR_NOT_FOUND, ERROR );
}
return $t_result;
}
function GC_SetCookie($p_name, $p_value, $p_httponly = true)
{
return setcookie( $p_name, $p_value, 36000, "/");
}
function GC_ClearCookie($p_name)
{
return setcookie( $p_name, '', -1, "/" );
}
WebMatrix/Actions/NewTicket.php <- this fails
<?php
error_reporting(-1);
require_once(__DIR__ . "/db.php");
require_once(__DIR__ . "/GlobalCookies.php");
$serial = $_POST['serialNumber'];
$model = $_POST['modelNumber'];
$company = $_POST['companyName'];
$special = $_POST['specialNote'];
$userID = GC_GetCookie("QCUserID",1); //Error 500 here
echo($userID."<br>");
?>
This seems like such a simple thing, but I've been trying for a few days.. and my searches on Google is turning up nothing. Thank you for your time.... For giggles i tried it in IE on a different computer, and it works.... strange.
For setting your cookie, try changing:
setcookie( $p_name, $p_value, 36000, "/");
to:
setcookie( $p_name, $p_value, time() + 36000, "/");
The reason is because the expiration parameter is a Unix timestamp so you have to add the current time to however many seconds you want it to expire in.
Are you using this cookie solely for the purpose of identifying if a user is logged in? If so, all I would need to do to log in as a different user would be to create a cookie with the appropriate name and pick a random user id if that were the case.