0

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

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

2 回答 2

1

我哪里都看不到session_start()

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

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

于 2013-07-02T00:34:07.373 回答
1

你有吗

session_start();

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

于 2013-07-02T00:34:12.780 回答