字符串未更新 [英] String not updating

查看:107
本文介绍了字符串未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC以及PDO.我试图根据会话是否为空来更新字符串,所以在我的index.phtml中,我有:

I am using MVC and also PDO. I am trying to update a string depending on whether the session is empty or not, so in my index.phtml I have:

<?php if (!empty($_SESSION)) :?>
    <p>
        Welcome back, <a href="logout.php"><?php echo $_SESSION["info"]["users_name"];?></a>
    </p>
<?php endif; ?>
<?php if(empty($_SESSION)) : ?>
    <p>
        Hello, would you like to <a href="" data-toggle="modal" data-target="#myModal">sign in</a>
    </p>
<?php endif; ?>

因此,如果会话为空,则通过单击登录"显示您好,您想登录吗",它将打开登录模式;如果会话不为空,则显示欢迎回来用户",并将链接更改为logout.php.我唯一的问题是,当会话不为空时,它不会更新字符串.我的登录模式可以正常工作,就像用户输入了错误的密码后将其重定向到index.php并显示"Wrong pass",但是,如果用户登录了该模式,则会重定向到index.php但不会更新字符串.

So if the session is empty then show "Hello would you like to sign in" by clicking sign in, it opens the login modal, and if the session is not empty it shows "Welcome back user" and changes the link to logout.php. The only problem I am having is that it doesn't update the string, when the session is not empty. My login modal works perfectly as if the user types in the wrong password it redirects to index.php and says "Wrong pass", however if the user logs in it redirects to index.php but does not update the string.

如果有帮助,这就是我的index.php:

If it helps this is my index.php:

<?php
require("Models/UserData.php");
$view = new stdClass();
$view->pageTitle = 'Login';

if(isset($_POST["email"]) && isset($_POST["password"])) {
    $userVeri = new UserData();

    $result = $userVeri->login($_POST["email"],$_POST["password"]);

    if (!empty($result)) {
        session_start();

        $_SESSION["info"] = $result;

        header("Location: index.php");

        exit;
    } else {
        echo "Wrong pass";
    }
}

 // Then verify it


 require("Views/index.phtml");

在我的index.phtml中,我有名为"email"和"password"的文本输入,因此所有内容都可以完美地链接,似乎字符串没有更新.有人知道我在做什么错吗?

In my index.phtml I have text inputs named "email" and "password" so everythings linking perfectly, it just seems that the string is not updating. Does anyone understand what I am doing wrong?

推荐答案

移动

session_start();

在文件顶部,它必须是文件中执行的第一件事.

to the top of your file, it has to be the first thing executing in your file.

在使用会话变量进行任何写入或读取或将任何输出写入浏览器之前,会话必须正在运行.重要的是,您不希望会话存在,这取决于它们是否具有具有适当值的适当密钥.如果您可能在代码中的某个时候需要会话密钥中的会话值,那么您总是希望进行一个正在进行的会话,即从一开始就调用它,以便在需要时可以使用它,而无需了解什么打破它.

The session must be running before any writing or reading is done with the session variables or any output is written to the browser. What matters is that you don't want sessions to exist depending on whether they have a proper key with a proper value or not; if you may at some point in your code require a session value from a session key then you always want to have an ongoing session, i.e. by calling it from the beginning, so that it's available when you need it, without you having to understand what's breaking it.

同样重要的是要知道,除非在b.php中也调用session_start(),否则在a.php中调用session_start()不会使会话在b.php中进行.或者,如果将a.php重定向到a.php(刷新),则除非文件开头有session_start(),否则该会话将无法进行,因此,使用if语句确定会话是否应该继续进行不会如果可以的话,它们之间的价值就会丢失.

It is also important to know that calling session_start() in a.php will not make the session live in b.php unless session_start() is called in b.php aswell. Or, if you redirect a.php to a.php (refresh) then the session will not live unless you have session_start() at the beginning of the file, so using if statements to determine whether a session should be ongoing is not going to work, values will be lost in between, if it works to begin with.

这篇关于字符串未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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