douchi0471 2012-10-19 19:41
浏览 361
已采纳

获取cookie导致错误500

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.

  • 写回答

1条回答 默认 最新

  • dr9379 2012-10-19 20:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分