会话使用变量作为名称无法传递给其他php文件

时间:2016-02-13 13:22:46

标签: php session

我在php中有会话问题,我有一些代码

<?php
session_start();
include 'conn.php';
$groupID = $_GET['group'];
$pass = md5($_REQUEST['pass']);
$query = "select * from group g join user u on g.admin = u.nim where g.id_group = $groupID and g.password = '$pass'";
$res = mysqli_query($conn, $query)or die(mysqli_error($conn));
$num_rows = mysqli_num_rows($res);
if ($num_rows == '1') {
    $_SESSION[$groupID] = "yes";
    header("location: room.php?group=$groupID");
    //echo $groupID;
    //echo $_SESSION[$groupID];
}else{
    header("location: grup.php?group=$groupID");
}
?>

这是针对room.php

<?php 
session_start();
$group = $_GET['group'];
if (isset($_SESSION[$group])) {
    echo "Session active value is :" . $_SESSION[$groupID];
}else{
    echo "Session is empty";
}

?>

如果是,我想使用$_SESSION[$group]的值,但我不能.. 我不知道该怎么做..

2 个答案:

答案 0 :(得分:0)

Probalby你在两个脚本之间混合了一些东西。在您的脚本 room.php 中,当会话变量 $ _ SESSION [$ group] 为时,变量 $ groupID 未初始化。你不是说$ group?

<?php 
session_start();
$group = $_GET['group'];
if (isset($_SESSION[$group])) {
    echo "Session is active:" . $_SESSION[$group]; // 'yes'
    echo "Session value is :" . $group;            // value of 'group' parameter
}else{
    echo "Session is empty";
}
?>

答案 1 :(得分:-1)

if (isset($_SESSION['$group'])) {
    echo "Session active value is". "$_SESSION['$groupID'] " ;
}else{
    echo "Session is empty";
}

试试这个