会话变量由会话数组中的下一个变量重写



好的,我有一个导航栏链接,单击后转到语言.php

我的导航栏代码位于单独的视图文件夹中 views/header/header2.php .

<?php 
    $_SESSION['from'] = $_SERVER['REQUEST_URI'];
?>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" style="position:top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" class="sr-only"><img src="images/dp.png"></button>
            <a class="navbar-brand" href="index.php">
            <img src="views/images/cp.png" title="###.###" alt="logo" width="90px" height="48px" style="padding-top:2px"></a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
            <li><a href="profile.php?id=<?php echo $_SESSION['user_id'] ?>"><span class="glyphicon glyphicon-user" ></span> <?php echo HEADER_PAGE; ?></a></li>
            <li><a href="search.php"><span class="glyphicon glyphicon-search"></span> <?php echo HEADER_SEARCH; ?></a></li>
            <li><a href="post.php"><span class="glyphicon glyphicon-plus"></span> <?php echo HEADER_POST; ?></a></li>   
            <li><a href="community.php"><span class="glyphicon glyphicon-edit"></span> <?php echo HEADER_COMMUNITY; ?></a></li>
            <li><a href="edit.php"><span class="glyphicon glyphicon-cog"></span> <?php echo HEADER_EDIT; ?></a></li>
            <li><a href="language.php"><span class="glyphicon glyphicon-flag"></span> <?php echo HEADER_LANGUAGE; ?></a></li>
            <li style="background-color:white"><a style="color:black" href="index.php?logout"><?php echo WORDING_LOGOUT; ?></a></li>
            </ul>
        </div>
        <!-- Collapse -->
    </div>
</nav>

我的语言的代码.php位于主目录中。

<?php
    session_start();
    if(isset($_SESSION['lang'])){
        $from = $_SESSION['from'];
        $langSession = $_SESSION['lang'];
        if($langSession == "th"){
            $_SESSION['lang'] = "en";
            //header('Location:http://www.###.###'.$from);
            echo $from;
        } elseif ($langSession == "en"){
            $_SESSION['lang'] = "th";
            //header('Location:http://www.###.###'.$from);
            echo $from;
        }
        if(!(isset($langSession))){
            $_SESSION['lang'] = "en";
            //header('Location:http://www.###.###'.$from);
            echo $from;
        }
    } 
?>

问题出在我的语言.php文件中: 我注释掉了标题(位置:"www.###.###".$from)并使用了echo $from ($from = $_SESSION['from'](为了找到变量正在读取的内容),一旦我在location.php上,redirect. $from读取为3。当我在执行位置之前检查.php时,index.php显示$from /index.php这是正确的。

问题:一旦我点击language.php的链接,$from更改为整数 3

通过使用header(),它将我发送到http://www.###.###3(数字 3 来自另一个会话变量用户ID $_SESSION['id'],它来自 $_SESSION 变量数组中的下一个索引)。

我尝试使用其他用户登录,它根据该用户的 ID 更改了数字,因此确认以下 $_SESSION 变量正在覆盖前一个变量。我只是不知道它为什么要这样做并更改$_SESSION['from']变量。

现在它正在处理除index.php以外的所有其他页面。

我的index.php代码

<?php
ob_start();
// check for minimum PHP version
if (version_compare(PHP_VERSION, '5.3.7', '<')) {
    exit('Sorry, this script does not run on a PHP version smaller than 5.3.7 !');
} else if (version_compare(PHP_VERSION, '5.5.0', '<')) {
    // if you are using PHP 5.3 or PHP 5.4 you have to include the password_api_compatibility_library.php
    // (this library adds the PHP 5.5 password hashing functions to older versions of PHP)
    require_once('libraries/password_compatibility_library.php');
}
// include the config
require_once('config/db/config.php');
// include translation
session_start();
if(isset($_SESSION['lang'])){
    $lang = $_SESSION['lang'];
} elseif(!(isset($_SESSION['lang']))) {
    $_SESSION['lang'] = "en";
    $lang = $_SESSION['lang'];
}
$_SESSION['from'] = $_SERVER['REQUEST_URI'];
require_once("translations/$lang.php");
// include the PHPMailer library
require_once('libraries/PHPMailer.php');
// load the login class
require_once('classes/Login.php');
// create a login object. when this object is created, it will do all login/logout stuff automatically
// so this single line handles the entire login process.
$login = new Login();
// ... ask if we are logged in here:
if ($login->isUserLoggedIn() == true){
    // the user is logged in. you can do whatever you want here.
    // for demonstration purposes, we simply show the "you are logged in" view.
    include("views/logged_in.php");
} else {
    // the user is not logged in. you can do whatever you want here.
    // for demonstration purposes, we simply show the "you are not logged in" view.
    include("views/landing.php");
}

附言。我已经通过 .htaccess 关闭了注册的全局变量

任何帮助将不胜感激,谢谢。

我通过将 $_SESSION['from'] 重命名为 $_SESSION['link'] 来解决此问题,它再次正常工作。我假设 $_SESSION['from'] 试图由其他东西设置。我已经浏览了这些操作中涉及的所有代码,但没有任何内容。任何人都能解释为什么它将 $_SESSION['from'] 的值更改为 $_SESSION['user_id'] 的值?

如 $from = $_SESSION['from']

,则为其分配了 $_SESSION['user_id']...

最新更新