num_rows==1) { $_SESSIO-6ren">
gpt4 book ai didi

php - 试图设置 session 变量

转载 作者:行者123 更新时间:2023-12-01 00:54:46 25 4
gpt4 key购买 nike

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;
}
?>

您好,我正在尝试设置此 session 变量,但它似乎没有设置,我想知道是否有人可以提供帮助。如果 session['admin'] isset 它应该回显管理按钮。但我不太确定为什么? (我确实有 session 启动和每个页面上的所有内容,这不是那个或任何“你没有 php 标签”的问题我已经检查了 mysql 查询,它确实从我的表中返回了一些东西。有什么想法吗?

最佳答案

您的 session_start(); 应该位于页面顶部,然后再与 session 变量相关。

来自 the docs :

当调用 session_start() 或 session 自动启动时,PHP 将调用打开和读取 session 保存处理程序。

根据评论编辑:

<?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;
}
?>

根据更多评论编辑 2:

您必须像这样实际获取数据 - 从 this tutorial 中截取这可能会帮助你更多:

$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>";
}

但话虽如此,当我们讨论它时,您最好还是离开旧的 mysql_* 函数并移动到 PDO哪个更好。

关于php - 试图设置 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com