简体   繁体   English

试图设置会话变量

[英]trying to set session variable

if(isset($_SESSION['admin'])) {
echo "<li><b>Admin</b></li>";
}

<?php
session_name('MYSESSION');
session_set_cookie_params(0, '/~cgreenheld/');
session_start();

$conn = blah blah
$query2 = 'Select Type from User WHERE Username = "'.$_SESSION['user'].'" AND Type =\'Admin\'';
$result2 = $conn->query($query2);
if($result2->num_rows==1) {
$_SESSION['admin'] = $result2;
}
?>

Hi, I'm trying to set this session variable but it doesn't seem to be setting, and i'm wondering if anyone can help. 嗨,我正在尝试设置此会话变量,但似乎没有设置,我想知道是否有人可以提供帮助。 If session['admin'] isset it should echo the admin button. 如果设置了session ['admin'],则应回显admin按钮。 But i'm not quite sure why? 但是我不太确定为什么? (I do have session start and everything on everypage, it's not a problem with that or any of the "You don't have php tags" I have checked the mysql query, and it does return something from my table. Any ideas please? (我确实在每个页面上都有会话启动和所有内容,这不是问题,也不是我检查了mysql查询的任何“您没有php标记”问题,并且确实从表中返回了一些东西。请问有什么想法吗?

Your session_start(); 您的session_start(); should be at the top of the page before anything to do with the session variables. 应该与页面变量无关的页面顶部。

From the docs : 文档

When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. 当调用session_start()或自动启动会话时,PHP将调用打开并读取会话保存处理程序。

Edit from comments: 编辑评论:

<?php
    session_name('MYSESSION');
    session_set_cookie_params(0, '/~cgreenheld/');
    session_start();
    // Moved to start after answer was accepted for better readability
    // You had the <?php after this if statement? Was that by mistake?
    if(isset($_SESSION['admin'])) 
    {
        echo "<li><b>Admin</b></li>";
    }

    // If you have already started the session in a file above, why do it again here?

    $conn = blah blah;
    $query2 = 'Select Type from User WHERE Username = "'.$_SESSION['user'].'" AND Type =\'Admin\'';
    // Could you echo out the above statement for me, just to 
    // make sure there aren't any problems with your sessions at this point?
    $result2 = $conn->query($query2);
    if($result2->num_rows==1) 
    {
        $_SESSION['admin'] = $result2;
        // It seems you are trying to assign the database connection object to it here.
        // perhaps try simply doing this:
        $_SESSION['admin'] = true;
    }
?>

Edit 2 from further comments: 从进一步的评论中编辑2:

You have to actually fetch the fetch the data like this - snipped from this tutorial which might help you out some more: 您实际上必须像这样提取数据-从本教程中摘录可能会帮助您一些更多的事情:

$query  = "SELECT name, subject, message FROM contact";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "Name :{$row['name']} <br>" .
     "Subject : {$row['subject']} <br>" .
     "Message : {$row['message']} <br><br>";
} 

But having said that, while we are talking about it, you would be better off moving away from the old mysql_* functions and move to PDO which is much better. 但是,尽管如此,在我们谈论它的同时,最好不要使用旧的mysql_*函数,而要使用更好的PDO

Move session_start(); 移动session_start(); to the top of the page. 到页面顶部。 You are trying to retrieve sessions, where it's not loaded. 您正在尝试检索未加载的会话。

EDIT: Try echoing $_SESSION['admin'] , if it even contains something. 编辑:尝试回显$_SESSION['admin'] ,即使它包含某些内容。 Also try debugging your if($result2->num_rows==1) code by adding echo('its working'); 还可以尝试通过添加echo('its working');调试if($result2->num_rows==1)代码echo('its working'); or die('its working'); die('its working'); inside it, to check if $result2 contains exactly 1 row, since currently it seems $result2 contains either more than 1 row or no rows at all. 在其中检查$ result2是否正好包含1行,因为当前看来$ result2包含多于1行或根本没有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM