【问题标题】:html/php no cache but keep cookieshtml/php 没有缓存但保留 cookie
【发布时间】:2011-04-03 15:09:19
【问题描述】:

我有一个简单的登录页面,但无法显示登录页面。提交表单时,会显示相同的登录页面。我必须单击刷新或 F5 才能看到登录页面。我尝试了无缓存(元标记),但我的问题是 cookie 也消失了(我无法存储状态)。

顺便说一句,我的登录使用重定向。表单提交调用不同的页面进行验证,然后重定向回同一页面,但据说内容不同(登录表单不应该再存在了)。

我相信这是基本的,但很遗憾在其他地方找不到答案。

谢谢。

更新: 以下是一些代码: 登录页面有带提交的 ExtJs 表单:

login.getForm().getEl().dom.action='bridge.php/login';
login.getForm().getEl().dom.submit();

bridge.php 是另一个服务器的休息客户端: sn-p:

<?php
//echo $HTTP_SERVER_VARS['PATH_INFO'];


require_once "RESTclient.php";
require_once "http_request.php";

$rest = new RESTclient();
$http_req = new httpRequest();

//$headers = $http_req->headers();
$headers = apache_request_headers();

foreach($headers as $key => $value) {
    if($key != "Cookie" && $key != "Content-Type"){
        unset($headers[$key]);
    }
}

//$headers["Content-Type"] = "";

$inputs = array_merge($_GET, $_POST);
//$inputs = array_merge($inputs, $_);

$url = "http://another_server/rest_service_here";
$path = $HTTP_SERVER_VARS['PATH_INFO'];
$server = $url.$path;

$rest->createRequest("$server",$http_req->method(),$inputs);
$rest->addHeaders($headers);
$rest->setBody($http_req->body());
$rest->sendRequest();

// get the headers now
$responseheaders = $rest->getResponseHeaders();
$responsecookies = $rest->getResponseCookies();

if ($responseheaders != null) {
    foreach ($responseheaders as $key => $value) {
        header($key.': '.$value);
    }
}
if ($responsecookies != null) {
    foreach ($responsecookies as $key => $value) {

        $name = $value['name'];
        $val = $value['value'];
        setcookie($name, $val);
    }
}
if($path=='/login') {
    ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Logging in</title>
<meta HTTP-EQUIV="REFRESH" content="0; url=/new_index.php">
</HEAD>
<BODY>
Redirecting to homepage...
</BODY>
</HTML>
    <?php 
} else {
    $output = $rest->getResponse();
    //$output = $output." ".$http_req->method();

    // start doing something about the output.
    //echo $http_req->body();
    //echo $http_req->raw();
    echo $output;
    //var_dump($headers);
}

?>

【问题讨论】:

  • 您可能希望继续显示登录页面的代码,尤其是选择显示内容的分支。
  • 我会尝试,但我会先花一点时间总结一下。我会在一段时间内回来。我的登录表单使用标准提交样式的 ExtJs。操作 url 是一个 php,它将显示类似 的内容。如果使用缓存,index.php 会重新显示,登录表单仍然存在。 F5 将显示正确的显示。如果没有缓存,cookie 就会消失,服务器无法知道用户已经登录(意味着 F5 一直显示登录表单)。
  • 您应该使用 header 函数直接从 PHP 重定向。请参阅 PHP 手册页上的位置重定向示例。

标签: php html cookies


【解决方案1】:

只要你在做以下...

  1. 在输出之前始终设置/删除登录 cookie任何内容

  2. 总是在您设置 cookie 之后 重定向。理想情况下,这应该是一个具有不同 URL 的页面(即使它只是一个不同的查询字符串),但是如果没有缓存的那个页面应该正常工作。

  3. 一旦您通过exit 重定向(通过header('Location: ...'); 调用)结束脚本处理。

..那么一切都会好起来的。也就是说,正如@Jon 所说,发布一些代码,我们可以看看。

【讨论】:

  • 谢谢 middaparka。我得先消化你的回答,然后再试一试。
  • @demotics2002 需要注意的是,如果没有退出,重定向之后的代码仍然会执行——只是浏览器不会必要看到它.
  • @demotics2002 没问题。希望一切顺利。 :-)
猜你喜欢
  • 2014-11-07
  • 1970-01-01
  • 2010-11-24
  • 2013-03-17
  • 1970-01-01
  • 2017-11-26
  • 2010-12-13
  • 2012-09-17
  • 1970-01-01
相关资源
最近更新 更多