为什么我的会话没有工作?

时间:2013-07-02 00:31:21

标签: php session

嘿伙计们,我不知道为什么我在index.php上无法echo $_SESSION['name']

Undefined index: name in C:\xampp\htdocs\STT\index.php on line 52
You are logged as

PHP代码:

$username = (isset($_POST['username'])) ? trim($_POST['username']) : '' ;
$password = (isset($_POST['password'])) ? trim($_POST['password']) : '' ;


if (isset($_POST['submit']) && ($_POST['submit'] = 'Login')) {

   $query='SELECT * FROM user_info WHERE username = "'. $username .'" AND password = PASSWORD("'. $password .'") ';     
   $result = mysql_query($query) or die(mysql_error()) ;
   $count = mysql_num_rows($result) ;
   if ( $count == 1 ) {


    $_SESSION['logged'] = 1 ;
    $_SESSION['name'] = $_POST['username'] ;
    echo 'successfully logged.' ;
    header ('Refresh : 5 ; URL = index.php') ;

   }

   else {

    echo 'Invalid username or password' ;
    $_SESSION['logged'] = 0 ;

   }

}

和index.php

<?php 
    if ( $_SESSION['logged'] = 1) {

        echo 'You are logged as' . $_SESSION['name'] ;

        }

        else {

            echo 'WTF';
        }
?>

2 个答案:

答案 0 :(得分:1)

我在任何地方都看不到session_start()

  

开始新会话或恢复现有会话

请务必在之前将添加到页面的任何输出中(包括空格)。

答案 1 :(得分:1)

你有吗

session_start();

在您使用会话的每个页面中?