$ _SESSION似乎不起作用

时间:2014-01-30 23:10:40

标签: php session

我有一个PHP脚本应该检测$_SESSION['Username']并将其转换为txt文件。我有一个类似的脚本在我的网站上运行,但用于不同的用途,它工作得很好。如你所见,我有

$nickname = $_SESSION['Username']; 

fwrite(fopen('chat.txt', 'a'), "". $nickname . ": " . $message = str_replace("\n", " ", $message) . "\n"); 

我认为这是失败的地方。 不知道为什么这个失败,任何和所有的帮助将不胜感激!

<?php

    $function = $_POST['function'];

    $log = array();

    switch($function) {

         case('getState'):
             if(file_exists('chat.txt')){
               $lines = file('chat.txt');
             }
             $log['state'] = count($lines); 
             break; 

         case('update'):
            $state = $_POST['state'];
            if(file_exists('chat.txt')){
               $lines = file('chat.txt');
             }
             $count =  count($lines);
             if($state == $count){
                 $log['state'] = $state;
                 $log['text'] = false;

                 }
                 else{
                     $text= array();
                     $log['state'] = $state + count($lines) - $state;
                     foreach ($lines as $line_num => $line)
                       {
                           if($line_num >= $state){
                         $text[] =  $line = str_replace("\n", "", $line);
                           }

                        }
                     $log['text'] = $text; 
                 }

             break;

         case('send'):
          $nickname = $_SESSION['Username'];
             $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
              $message =($_POST['message']);
         if(($message) != "\n"){

             if(preg_match($reg_exUrl, $message, $url)) {
                $message = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message);
                } 


             fwrite(fopen('chat.txt', 'a'), "". $nickname . ": " . $message = str_replace("\n", " ", $message) . "\n"); 
         }
             break;

    }

    echo json_encode($log);

?>

启动会话的登录脚本

<?php
}  
elseif(!empty($_POST['username']) && !empty($_POST['password']))  
{
$username = mysql_real_escape_string($_POST['username']);  
    $password = md5(mysql_real_escape_string($_POST['password']));  

    $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");  

    if(mysql_num_rows($checklogin) == 1)  
    {  
        $row = mysql_fetch_array($checklogin);  
        $email = $row['EmailAddress'];  

        $_SESSION['Username'] = $username;  
        $_SESSION['EmailAddress'] = $email;  
        $_SESSION['LoggedIn'] = 1;  

        echo "<h1>Success</h1>";  
        echo "<p>We are now redirecting you to the member area.</p>";  
        echo "<meta http-equiv='refresh' content='=10;index.php' />";  
    }  
else  
    {  
        echo "<h1>Error</h1>";  
        echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";  
    }  
}  
else  
{  
    ?>  

1 个答案:

答案 0 :(得分:1)

每次打算使用它时都必须启动会话。 (最好在页面的开头)

<?php session_start();?>