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.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?