为什么我的SESSION数据在发布到同一页面后没有更新? [英] Why isn't my SESSION data being updated after POSTing to the same page?

查看:87
本文介绍了为什么我的SESSION数据在发布到同一页面后没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么会发生这种情况。我将让用户成为@ login.php,并输入他们的登录凭证,如下面的面板(1)所示。

然后,在成功地将他们的凭证与存储的凭证进行匹配后,

在数据库中,我将分配一个$ _SESSION ['用户名']变量,并将页面发送给自己(action =),然后我会提醒用户会话变量已设置。



理想情况下,我不想提醒用户会话变量已设置,并且在点击登录后立即自动开始使用$ _SESSION ['username']变量(并验证成功)。



我该如何做到这一点?我试图通过放置类似于

 < ;? if(isset($ _ SESSION ['username'])){
header(Location:index.php);
exit();
?>

但是我不断收到一个头文件已经发送错误,因为一些输出(像我的CSS,JS文件)在login.php页面上的表单验证逻辑之前。



解决方案

操作顺序。



会话创建和测试有效性检查在页面的最顶部,以便页面的其余部分可以判断是否存在 $ _ SESSION ['username']



有机会尝试在内容区域中验证它们,以便您的yay或ney消息显示在文档,漂亮,是的,但整个页面的上半部分看不到它是[潜在]有效的会话。)


I'm trying to figure out why this is happening. I'll have the user be @ login.php and enter their login credentials as seen in panel (1) below.

Then after successfully matching their credentials to the ones stored in the database, I'll assign a $_SESSION['username'] variable and the page is POSTed to itself (action=""), and I'll alert the user that the session variable has been set.

Ideally, I'd like to not have to alert the user the session variable has been set, and just automatically start using that $_SESSION['username'] variable immediately after they click login (and are verified successfully).

How can I achieve this? I've tried to mess around with headers by putting something like

<? if (isset($_SESSION['username'])) {
header("Location: index.php");
exit();
?>

But I keep getting a headers already sent error because some of the output (like my CSS, JS files) precede the form validation logic on the login.php page.

解决方案

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.)

这篇关于为什么我的SESSION数据在发布到同一页面后没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆