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

时间:2015-01-19 03:50:29

标签: php session

好的我有一个导航栏链接,点击后会转到 language.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>

我的language.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;
        }
    } 
?>

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

问题:点击language.php的链接后,$from会更改为 3 整数。

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

我尝试使用其他用户登录,并根据该用户的ID更改了号码,以便确认以下$_SESSION变量正在覆盖之前的$_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");
}

PS。我通过 .htaccess

关闭了注册的全局广告

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我通过将$ _SESSION [&#39;]从&#39;重命名为$ _SESSION [&#39; link&#39;]来修复此问题,并且它再次正常工作。我假设$ _SESSION [&#39;来自&#39;]试图通过别的东西设置。我浏览了所有涉及这些操作的代码,但没有任何内容。任何人都有解释为什么它将$ _SESSION [&#39;从&#39;]的值更改为$ _SESSION [&#39; user_id&#39;]的值?

在$ from = $ _SESSION [&#39; from&#39;]中,然后为其分配$ _SESSION [&#39; user_id&#39;] ...

的值
相关问题