$ _SESSION保持旧值

时间:2019-03-13 15:28:11

标签: php mysql

我正在编码PHP,我的$ _SESSION变量有问题。它应该获得新的价值,但仍然保持他的旧价值代替新价值。

<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
include("templates/header.inc.php");// simple the header, nothing important ;)

?>

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen");
$result = $statement->execute();
while($row = $statement->fetch()) {
        echo '<a class="kein" href="forum_forum.php"><div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subt  itel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "br><br>",'<div>'.$row['fragen_id'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div></a>',
        $chosen_one = $row['fragen_id'];    //the variable $chosen_one should get the value of the id   
        $_SESSION['chosen_one'] = $chosen_one; // and here I want to make this variable global for the next site.
}
?>
<?php 
include("templates/footer.inc.php");
?>

输出(新站点)应该是fragen_id与我的$ _SESSION ['chosen_one']相同的内容,但它需要工作

<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
include("templates/header.inc.php");
$chosen_one = $_SESSION['chosen_one'];
echo $chosen_one;
?>

<?php
$statement = $pdo2->prepare("SELECT * FROM fragen WHERE fragen_id =$chosen_one");
$result = $statement->execute();
while($row = $statement->fetch()) {
        echo '<div class="forum"><div class="forum_Titel">' ,"Titel:  ".$row['fragen_title'].'</div>',
        "<br><br>",'<div class="forum_Subitel">', "Subtitel:  " .$row['fragen_subtitle'].'</div>',
        "<br><br>",'<div class="forum_Content">',"Text:  ".$row['fragen_content'].'</div>',
        "<br><br>",'<div class="forum_Bild"><img class="pic" alt="Ein Bild" title="Eine Pflanzen" src="'.$row['fragen_picture'].'">',
        "<br><br>","Bildlink:  ".$row['fragen_picture'].'</div>',
        "<br><br>",'<div class="forum_Upload-date">',"Upload-Datum:  ".$row['fragen_date'].'</div>',
        '</div>';

}
?>
<?php 
include("templates/footer.inc.php");
?>

这是我的功能:

<?php
/**
 * A complete login script with registration and members area.
 *
 * @author: Nils Reimers / http://www.php-einfach.de/experte/php-codebeispiele/loginscript/
 * @license: GNU GPLv3
 */
include_once("password.inc.php");

/**
 * Checks that the user is logged in. 
 * @return Returns the row of the logged in user
 */
function check_user() {
    global $pdo;

    if(!isset($_SESSION['userid']) && isset($_COOKIE['identifier']) && isset($_COOKIE['securitytoken'])) {
        $identifier = $_COOKIE['identifier'];
        $securitytoken = $_COOKIE['securitytoken'];

        $statement = $pdo->prepare("SELECT * FROM securitytokens WHERE identifier = ?");
        $result = $statement->execute(array($identifier));
        $securitytoken_row = $statement->fetch();

        if(sha1($securitytoken) !== $securitytoken_row['securitytoken']) {
            //Vermutlich wurde der Security Token gestohlen
            //Hier ggf. eine Warnung o.ä. anzeigen

        } else { //Token war korrekt
            //Setze neuen Token
            $neuer_securitytoken = random_string();
            $insert = $pdo->prepare("UPDATE securitytokens SET securitytoken = :securitytoken WHERE identifier = :identifier");
            $insert->execute(array('securitytoken' => sha1($neuer_securitytoken), 'identifier' => $identifier));
            setcookie("identifier",$identifier,time()+(3600*24*365)); //1 Jahr Gültigkeit
            setcookie("securitytoken",$neuer_securitytoken,time()+(3600*24*365)); //1 Jahr Gültigkeit

            //Logge den Benutzer ein
            $_SESSION['userid'] = $securitytoken_row['user_id'];
        }
    }


    if(!isset($_SESSION['userid'])) {
        die('Bitte zuerst <a href="login.php">einloggen</a>');
    }


    $statement = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $result = $statement->execute(array('id' => $_SESSION['userid']));
    $user = $statement->fetch();
    return $user;


}

/*
Returns true when the user is checked in, else false
*/
function is_checked_in() {
    return isset($_SESSION['userid']);
}

/**
 * Returns a random string
 */
function random_string() {
    if(function_exists('openssl_random_pseudo_bytes')) {
        $bytes = openssl_random_pseudo_bytes(16);
        $str = bin2hex($bytes); 
    } else if(function_exists('mcrypt_create_iv')) {
        $bytes = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
        $str = bin2hex($bytes); 
    } else {
        //Replace your_secret_string with a string of your choice (>12 characters)
        $str = md5(uniqid('your_secret_string', true));
    }   
    return $str;
}

/**
 * Returns the URL to the site without the script name
 */
function getSiteURL() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    return $protocol.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/';
}

/**
 * Outputs an error message and stops the further exectution of the script.
 */
function error($error_msg) {
    include("templates/header.inc.php");
    include("templates/error.inc.php");
    include("templates/footer.inc.php");
    exit();
}

希望您能帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

问题出在您的风格上。 如果$ _SESSION ['chosen_one'] == NULL会发生什么?

您将获得所有记录

$statement = $pdo2->prepare("SELECT * FROM fragen");

然后在while循环中,您将当前元素的行值设置为会话变量

$chosen_one = $row['fragen_id'];    //the variable $chosen_one should get the value of the id   
    $_SESSION['chosen_one'] = $chosen_one; // and here I want to make this variable global for the next site.

结果是,您将始终在会话变量中保留结果集的最后一项

这里的问题是如何获取被选择的ID?

如果有帮助,请标记为答案

答案 1 :(得分:-1)

尝试在开始之前将其添加到注销页面或此页面顶部。

 $_SESSION['xxx'] = ''; //for each variable you want to unset.
session_unset();
session_destroy();