- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请查看 this Stackoverflow 帖子。
我和 bob_cobb 有同样的 PHP 问题。这是布拉德·克里斯特的回答:
Order of operations.
Place your session creation and test-for-validity check at the very top of the page so the rest of the page can make judgment calls off the existence of $_SESSION['username']
(Chances are you're trying to validate them inside the content area so your "yay" or "ney" message appears in the desired section of the document. Pretty, yes, but the whole top-half of the page can't see that it's [potentially] a valid session.)
<?php
session_start();
if ($_SESSION['username'])
//User is already logged in, echo the log out button.
...
else
//User is not logged in, echo the log in form & button.
...
//Login form validation if user is not logged in and submitted form.
//At the end, create session variable ($_SESSION['username'])
//Destroy session if user pressed log out button.
session_destroy();
?>
最佳答案
在整个控制流完成之前不要回显任何内容。我的意思是你应该努力将逻辑与显示分开(更好的是:使用像 Model-View-Controller 这样的模式)。在您的情况下,也许您可以执行以下操作:
<?php
/* Place all your control logic at the beginning.
Do not echo anything in this block. */
session_start();
if ($_SESSION['username']) {
$loggedin = true;
} else {
$loggedin = false;
...
//Login form validation if user is not logged in and submitted form.
//If login succeeded, set $loggedin to true.
//At the end, create session variable.
}
//Destroy session if user pressed log out button.
session_destroy();
/* Only display logic below, we do not change any state here */
if($loggedin) {
echo logout button
} else {
echo login form
}
?>
关于PHP- 修复 session 刷新后才生效的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567388/
我希望在通过 POST 方法发送数据后文本框消失。但我无法让 Javascript 工作。页面不断重新加载,因此文本框始终存在。 function puff() { if (document.get
我无法让 expand_aliases 在 bash 中生效。我尝试了很多不同的方法,但没有任何效果。 这是一个简单的测试用例: /bin/bash -c 'shopt -s expand_alias
我正在尝试查找 #include 位于 extern C block 内的所有位置。是否可以使用预处理器进行检查?我想在我的头文件中添加类似这样的内容: #ifdef EXTERN_C_IS_IN_E
我们扩展了 UILabel,以便能够为我们应用程序中给定标签类型的所有用途应用标准字体和颜色。例如。 @interface UILabelHeadingBold : UILabel @end 在我们的
案例1 我们正在尝试将自定义样式应用于渲染的 vuetify 元素: .input-group__input { background: red; } 但是没有任何变化。 案
我正在使用 symfony 1.4 和 sfGuardDoctrinePlugin,我已经安装并设置好了,但我遇到以下问题: 如果我以管理员身份登录并更新用户的权限,该用户必须注销然后重新登录才能获得
getElementsByTagName() 有 2 个很棒的特性:速度快且实时。但是,如果我想获得 p strong 怎么办。当然,我可以再次使用 getElementsByTagName() 优化
我有三个文件: spark_mock_dependency.py 提供了一个user() 方法来读取/etc/user, spark_mock.py 用于创建一个 Env 类,它使用 user() 方
我从 samples/bpf/pare_simple.c(来自 Linux 内核树)编译了 BPF 示例,做了非常简单的更改: SEC("simple") int handle_ingress(str
我有一个基本的树结构。容器可以水平滚动。我在所有 上都有正确的填充元素。但是,正确的填充没有生效。我该如何修复它才能生效? ul { height: 100%; margin: 0; p
我目前在使用 Ruby on Rails 上的 Assets 管道时遇到了一些问题。 我正在使用电子商务解决方案 (Spree),在文档中,文档中有这个: [...] you can improve
在我们非常庞大且非常复杂的 AngularJS 应用程序中,我注意到(偶然!)我的主模块设置中有这样一行... application.run(function($rootScope) { w
我发现重写getResources()后app第一次运行的activity也会对后面运行的activity生效,前提是你的手机字体特别大。 例如:执行以下步骤 将手机字体设置为巨无霸 创建两个名为 A
我正在尝试使用 KVO 来观察在我的页面 View Controller 的子内容 View Controller 的 ScrollView 中使用拖动时的更新变化,但是当应用程序启动时,它崩溃了说:
我是一名优秀的程序员,十分优秀!