php 中session问题 isset()不能判断
login.php
<?php
session_start();
include_once 'fuctions.php';
$account=$_REQUEST["account"];
$password=$_REQUEST["password"];
$con = connectDB();
$query="select * from im where account='".$account."' and password='".$password."';";
$result=mysql_query($query);
if($row=mysql_fetch_array($result)){
echo("true");
$_SESSION['$row["id"]']=true;
}
else{
echo("false");
}
mysql_close($con);
?>
contact.php
<?php
session_start();
include_once 'fuctions.php';
$con = connectDB();
$query = "select * from im";
$result = mysql_query($query);
$count = 0;
while ($row = mysql_fetch_array($result)) {
$contact[$count] = array("id" => $row["id"], "name" => $row["name"], "group" => $row["group"], "phone" => $row["phone"],"online"=>1);
$count++;
}
for($i=0;$i<$count;$i++){
if(!isset($_SESSION['$contact[$i]["id"]'])){
$contact[$i]["online"]=0;
}
}
foreach ($contact as $key => $array) {
if (is_array($array)){
foreach ($array as $key => $value) {
echo($key."=>".$value." ");
}
}
}
$json=array();
for ($i = 0; $i < $count; $i++) {
["group"],"phone"=>$contact[$i]["phone"]);
}
mysql_close($con);
?>
为什么每次isset($_SESSION['$contact[$i]["id"]'])都判断不出来呢?
------解决方案--------------------C/C++ code
[User:root Time:12:01:13 Path:/home/liangdong/php]$ php dot.php
Array
(
[$b] => 1
)
[User:root Time:12:01:16 Path:/home/liangdong/php]$ cat dot.php
<?php
$a['$b'] = 1;
print_r($a);
?>
[User:root Time:12:01:18 Path:/home/liangdong/php]$
------解决方案--------------------
把
$_SESSION['$row["id"]']=true;和isset($_SESSION['$contact[$i]["id"]'])
改成
$_SESSION["$row['id']"]=true;和isset($_SESSION["$contact[$i]['id']"])
引号用反了,这种IDE应该会提示的吧
------解决方案--------------------
if(!isset($_SESSION[$contact[$i]["id"]])){
$contact[$i]["online"]=0;
}
去掉那对倒霉的单引号
------解决方案--------------------
$_SESSION[$row['id']]=true;
数组内键值不用加引号